Anda di halaman 1dari 4

M518 Mixed Signal Processors

CKnowledgeAssessmentTestSolutions

Question1.Brieflyexplainwhatistheline #include <stdio.h> atthetopofaCsourcefilefor?


#includeisacompilerdirectiverelatedtoCpreprocessor.InthecaseofstandardCcompilers(e.g.MS
VisualC++),itletsthecompilerknowimportantinformationaboutfunctionsfromthe``StandardI/O
library''.ItalsoprovidesafewI/Orelateddefinitions.
Question2.WhyisindentationimportantwhenwritingaCprogram?Whatdifferencedoesitmakefor
compilationprocess?
Indentation is important to show the logical structure of source code, in particular, to show which
partsthe``bodies``ofloops,if/elsestatements,andothercontrolflowconstructsaresubsidiaryto
whichotherparts.
Indentationisnotspeciallyobservedbythecompiler;ittreatsall``whitespace''thesame.Codewhich
isimproperlyindentedwillgiveahumanreaderamistakenimpressionofthestructureofthecode,an
impression potentially completely different from the compiler's. Therefore, it's important that the
punctuationwhichdescribestheblockstructureofcodetothecompilermatchestheindentation.
Question3.Whatisthedifferencebetweentheconstants123and"123"?
Theconstant123istheinteger123.Theconstant"123"isastringconstantcontainingthecharacters
1,2,and3.
Question4.WhatisthefunctionofthesemicoloninaCstatement?
Itisthestatementterminator.
Question5.Whatarethedefinitionsofthe``Boolean''valuestrueandfalseinClanguage?
False is always represented by a zero value. Any nonzero value is considered true. The builtin
operators<,<=,>,>=,==,!=,&&,||,and!alwaysgenerate0forfalseand1fortrue.
Question6.NamedifferentusesforthesemicoloninClanguage(max.three).
Terminating declarations, terminating statements, and separating the three control expressions in a
forloop.
Question7.Writetheequivalentofthecodebelow,usingawhileloop

for(i = 0; i < 10; i = i + 1)


printf("i is %d\n", i);

Justmovethefirst(initialization)expressiontobeforetheloop,andthethird(increment)expression
tothebodyoftheloop:

Branislav Vuksanovic, February 2012

M518 Mixed Signal Processors

i = 0;
while(i < 10)
{
printf("i is %d\n", i);
i = i + 1;
}

Question8.Whatisthenumericvalueoftheexpression3<4inClanguage?
1(or``true''),because3isinfactlessthan4.
Question9.Underwhatconditionswillthecodebelowprint``water''?

if(T < 32)


printf("ice\n");
else if(T < 212)
printf("water\n");
else printf("steam\n");

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

Branislav Vuksanovic, February 2012

M518 Mixed Signal Processors

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?

Branislav Vuksanovic, February 2012

M518 Mixed Signal Processors

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];

Branislav Vuksanovic, February 2012

c)intarray[20,20];

Anda mungkin juga menyukai