Anda di halaman 1dari 4

CS12ComputerProgramming1

intanother(inta,intb){
inti,j;
j=0;
for(i=a;i<=b;i++)
j=j+i;
returnj;
}
Whatistheoutputofeachofthefollowingprogramsegments?Assumethatx,y,
andkareintvariables.
a.x=10;
printf(%i\n,secret(x));
b.x=5;y=8;
printf(%i\n,another(x,y));
c.x=10;k=secret(x);
printf(%i%i\n,x,k);
d.x=5;y=8;
printf(%i\n,another(y,x));
C.[14pts]Considerthefollowingfunctionprototypes:
inttest(int,char,double,int);
doubletwo(double,double);
charthree(int,int,char,double);
Answerthefollowingquestions.
a) Howmanyparametersdoesthefunctiontesthave?Whatisthetypeofthefunction
test?_____________________________________________________
b) Howmanyparametersdoesfunctiontwohave?Whatisthetypeoffunctiontwo?
_____________________________________________________
c) Howmanyparametersdoesfunctionthreehave?Whatisthetypeoffunction
three?_____________________________________________________
d) Howmanyactualparametersareneededtocallthefunctiontest?
_____________________________________________________

MidtermExamination

I.

Fillintheblanks.
__________________1) Therearetwotypesofuserdefinedfunctions:the
__________and
__________________2) __________
__________________3) [2pts]Thegeneralsyntaxforauserdefinedfunction
prototypeis:

__________________4) Thefunctionheadingandthebodyofthefunctionare
calledthe________________________
__________________5) A____________isthefunctionheadingwithoutthebody
ofthefunction
__________________6) [2pts]Thegeneralsyntaxforauserdefinedfunction
definitionis:

__________________7) Inanarray,iftherearefewerinitialvaluesthanthearray
size,theexcesselementsareinitializedto___________.
__________________8) Anullcharacterisrepresentedas___________.
__________________9) Asanalternativetoassignmentoperatorininitializing
valuestoastringidentifier,thestringfunction
__________isused.
__________________10) [2pts]Thegeneralsyntaxforauserdefinedfunctioncall:

__________________11) _Comparingstringsusingrelationaloperatorsisnot
allowed.Thestringfunction__________isusedasan
alternative
__________________12) Thestringfunctionthatreturnsthenumberofcharacters
inastringiscalled__________

CS12ComputerProgramming1

II. True/False.

_____1) Evenifafunctionhasnoparameters,youstillneedtheempty
parenthesesinboththefunctionheadingandthefunctioncall.
_____2) Afunctioncannotreturnavalueoftypearray
_____3) Areturnstatementreturnsonlyonevalue
_____4) Aggregateoperationsarenotallowedonarraysandstrings
_____5) Arraysubscriptshouldbealwaysoftypeintandcannothaveanyother
variabletype
_____6) Asparameterstofunctions,arraysarepassedbyreferenceonly
_____7) Elementsofaonedimensionalarrayarearrangedintheformofalist
_____8)
Identifiers,arraynamesandfunctionnamesshouldfollowthesame
namingconventions
_____9)
Inafunctioncallstatement,whenpassinganarrayasanactual
parameter,youuseonlyitsname
_____10) Inafunctioncall,youspecifyonlytheactualparameter,notitsdata
type.
_____11) InC,anarrayindexalwaysstartswith0
_____12) Individualarraycomponentscanbepassedasparameterstofunctions
_____13) StringsandCharacterarraysarenullterminated
_____14) Theassociativityruleapplieswhentwooperationsareonthesame
precedencelevel
_____15) Theinitializationofavariableinadeclarationisoptional
_____16) Thereturnstatementcanappearanywhereinafunction
_____17) Userdefinedfunctionswillnotbeexecutedunlesscalled.
_____18) Whentheprogramexecutes,theexecutionalwaysbeginswiththefirst
statementinthefunctionmain.
_____19) Whenwritingthefunctionprototype,youdonothavetospecifythe
variablenameintheparameterlist.
_____20) Youcanplaceadeclarationstatementinanypartoftheprogrambefore
thelinethatusestheidentifieryouaregoingtodeclare.

MidtermExamination

III. Applications
A.[8pts]

voidOne(intA,intB,int&C){
intD;
A=10;B=11;C=12;D=13;
}
voidTwo(intA,intB,int&D){
intC=0;
}
voidmain(){
intA,B,C,D;
A=5;B=6;C=7;D=8;
One(A,B,D);
Two(A,B,C);
}
a) Whatarethevaluesofvariablesa,b,canddinfunctionone?
b) Whatarethevaluesofvariablesa,b,canddinMainafter
execution
c) Whatarethevaluesofvariablesa,b,canddinfunctiontwo?
d) Whatarethevaluesofvariablesa,b,canddinMainafter
executingfunctiontwo?
B.[8pts]Considerthefollowingfunctions:
intsecret(intx){
inti,j;
i=2*x;
if(i>10)
j=x/2;
else
j=x/3;
returnj1;
}

A B C D

CS12ComputerProgramming1

MidtermExamination

e) Whatisthetypeofeachactualparameter,andinwhatordershouldyouusethese
parametersinacalltothefunctiontest?
_____________________________________________________
f) [3pts]Writeastatementthatprintsthevaluereturnedbythefunctiontestwiththe
actualparameters5,5,7.3,and'z'.
_____________________________________________________
g) [3pts]Writeastatementthatprintsthevaluereturnedbyfunctiontwowiththe
actualparameters17.5and18.3,respectively.
_____________________________________________________
h) [3pts]Writeastatementthatprintsthenextcharacterreturnedbyfunctionthree.
(Useyourownparameters.)
_____________________________________________________
D.[5pts]Showtheoutputofthefollowingprogram:
#include<stdio.h>
intmystery(int);
intmain(){
intn;
for(n=1;n<=5;n++)
printf(%i\n,mystery(n));
return0;
}
intmystery(intk)
{
intx,y;
y=k;
for(x=1;x<=(k1);x++)
y=y*(kx);
returny;

CS12ComputerProgramming1

MidtermExamination

E.[10pts]Considerthefunctionheadings:

voidfuncOne(intalpha[],intsize);
IntfuncSum(intx,inty);
voidfuncTwo(constintalpha[],intbeta[]);
andthedeclarations:
intlist[50];
intaList[60];
intnum;
WriteCstatementsthatdothefollowing:
a) [3pts]CallthefunctionfuncOnewiththeactualparameters,listand50,
respectively.
_____________________________________________________
b) [2pts]PrintthevaluereturnedbythefunctionfuncSumwiththeactual
parameters,50andthefourthcomponentoflist,respectively.
_____________________________________________________
c) [2pts]PrintthevaluereturnedbythefunctionfuncSumwiththeactual
parameters,thethirtiethandtenthcomponentsoflist,respectively.
_____________________________________________________
d) [3pts]CallthefunctionfuncTwowiththeactualparameters,listandaList,
respectively.
_____________________________________________________
F.[10pts]Giventhedeclaration:
charstr1[21];
charstr2[21];
a) [2pts]Writeastatementthatstores"SunnyDay"instr1.
_____________________________________________________
b) [2pts]Writeastatementthatstoresthelengthofstr1intotheintvariablelength.
_____________________________________________________
c) [2pts]Writeastatementthatcopiesthevalueofnameintostr2.
_____________________________________________________
d) [4pts]Writecodethatoutputsstr1ifstr1islessthanorequaltostr2,and
otherwiseoutputsstr2.
_____________________________________________________

CS12ComputerProgramming1

MIDTERMEXAMINATION
2ndSemester20112012

Name:
Course:
Year:

Block:

Date of Examination: _____________


SCORES/number of it ems
I.

__________ out of 15

II.

__________ out of 20

III.

A. __________ out of 8
B. __________ out of 8
C. __________ out of 14
D. __________ out of 5
E. __________ out of 10
F. __________ out of 10

Tot al Score:

out of 80

Anda mungkin juga menyukai