CKnowledgeAssessmentTestSolutions
Justmovethefirst(initialization)expressiontobeforetheloop,andthethird(increment)expression
tothebodyoftheloop:
i = 0;
while(i < 10)
{
printf("i is %d\n", i);
i = i + 1;
}
Question8.Whatisthenumericvalueoftheexpression3<4inClanguage?
1(or``true''),because3isinfactlessthan4.
Question9.Underwhatconditionswillthecodebelowprint``water''?
IfTisgreaterthanorequalto32andlessthan212.(Ifyousaid``greaterthan32andlessthan212'',
you weren't quite right, and this kind of distinctionpaying attention to the difference between
``greaterthan''and``greaterthanorequal''isoftenextremelyimportantinprogramming.)
Question10.Whatwouldthiscodeprint?
int x = 3;
if(x)
printf("yes\n");
else printf("no\n");
Itwouldprint``yes'',sincexisnonzero.
Question11.Whatwouldthiscodeprint?
int i;
for(i = 0; i < 3; i = i + 1)
printf("a\n");
printf("b\n");
printf("c\n");
Itwouldprint
a
a
a
b
c
The indentation of the statement printf("b\n"); is (deliberately, for the sake of the question)
misleading.Itlookslikeit'spartofthebodyoftheforstatement,butthebodyoftheforstatementis
alwaysasinglestatement,oralistofstatementsenclosedinbraces{}.Inthiscase,thebodyofthe
loop is the call printf("a\n");. The two statements printf("b\n"); and printf("c\n"); are normal
statementsfollowingtheloop.
The code would be much clearer if the printf("b\n"); line were indented to line up with the
printf("c\n");line.Iftheintentwasthat``b''beprinted3times,alongwith``a'',itwouldbenecessary
toputbraces{}aroundthepairoflinesprintf("a\n");andprintf("b\n");.
Question12.WriteaCprogramtoprintthenumbersfrom1to10andtheirsquares.
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 10; i = i + 1)
printf("%d %d\n", i, i * i);
return 0;
}
Question13.Whichisavalidtypecast?
a)a(char);
b)char:a;
c)(char)a;
d)to(char,a);
c)(char)a;
Question14.Whichofthefollowingistheproperdeclarationofapointer?
a)intx;
b)int&x;
c)ptrx;
d)int*x;
d)int*x;
Question15.Whichofthefollowinggivesthememoryaddressofvariablea?
a)*a;
b)a;
c)&a;
d)address(a);
c)&a;
Question16.Whichofthefollowingisavalidfunctioncall(assumingthefunctionexists)?
a)funct;
b)functx,y;
c)funct();
d)intfunct();
c)funct();
Question17.Whichofthefollowinggivesthevaluestoredattheaddresspointedtobythepointera?
a)a;
b)val(a);
c)*a;
d)&a;
c)*a
Question18.Whichofthefollowinggivesthememoryaddressofthefirstelementinarrayfoo,an
arraywith100elements?
a)foo[0];
b)foo;
c)&foo;
d)foo[1];
b)foo
Question19.Whatistheindexnumberofthelastelementofanarraywith29elements?
a)29
b)28
c)0
d)Programmerdefined
b)28
Question20.Whichofthefollowingisavaliddeclarationofatwodimensionalarray?
a)arrayanarray[20][20];
b)intanarray[20][20];
b)intanarray[20][20];
c)intarray[20,20];