=========================preview======================
(COMP251)COMP251_01F-mid.pdf
Back to COMP251 Login to download
======================================================
COMP251:2001FallSemesterMidterm
Name: Question Mark
Student ID : 1.
2.
3.
4.
5.
6.
7.

Total:
Instruction:(a)TherearetotalSevenquestions. (b)Thetimethatyouhavefortheexamis75minutes. Thefullmarkfortheexamis75points. Soyoucanusethepointsofaproblemasaroughguideofthenumber ofminutesthatyouwouldneedonaveragetosolvetheproblem. (c)AttemptALLquestions. (Youmaygetpartialcreditsforyourincompleteanswers!) (d)WriteALLanswersintheexambook.Donotuseanyotherpapers.
Nowtakeaminutetowritedownyournameand yourstudentID.
1.Context-FreeGrammasWhatisthesyntaticambiguityofaBNFgrammar.
Whyisitnotdesirable.
2.StructuredProgramming.Givesomereasonsaswhycontrolcontructswitha single-entrypointandasingle-exitpointaregood.
3.Types.Whataretherestrictionsthatwemustputontheindexofanarrayif wewanttodothelayoutofthearrayatthecompiletime.
{inti.
for(i.1.i.3.i++)S}

alwayshavethesamebehaviourasthefollowingone.
{inti.1.
Si++.Si++.}

Ifyouranswerisyes,brie.yexplainwhy.Ifyouranswerisno,giveanexample statementSforwhichthetwoarenotthesame.
Problem3(6pts)ParameterPassingI.Giveanexamplewherecall-by-valueand call-by-referencearedi.erent.
andcall-by-value-resultaredi.erent.
Problem5(8pts)ParameterPassingIII.Giveanexamplewherecall-by-nameunder lexicalscopeandcall-by-nameunderdynamicscopearedi.erent.
intP(intx){
if(x.1)returnQ(x-1).elsereturnx.
}
intQ(intx){
if(x.1)returnP(x-1).elsereturnx.
}

(a)OutlinetheactivationrecordsforPandQ. (b)Drawthe.owoftheactivationrecordswhenthefollowingmain()programis executed:
intmain(){
intx.3.
x.P(x).
returnx.
}

(Cont'd)
.S.::.(.S.)|[.S.]|.S..S.|()|[]
GiveanunambiguousBNFthatwillgeneratethesamelanguageastheaboveone.