Académique Documents
Professionnel Documents
Culture Documents
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
2 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
PRINTismacroconstant.MacroPRINTwillbereplacedbyitsdefinedstatementjustbefore theactualcompilationstarts.Abovecodeisconvertedas:
voidmain(){ intx=1 if(x) printf("StarWars") printf("Psycho") else printf("TheShawshankRedemption") } Ifyouarenotusingopeningandclosingcurlybracketinifclause,thenyoucanwrite onlyonestatementintheifclause.Socompilerwillthink: (i) if(x) printf("StarWars")
Youcannotwriteelseclausewithoutanyifclause.Itiscauseofcompilationerror.Hence compilerwillshowanerrormessage:Misplacedelse
3 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Sointhe abovecode:
Sinceconditionisalwaystrue,soelseclausewillneverexecute.Programcontrolcannot reachatelsepart.Socompilerwillshowanotherwarningmessage:
Unreachablecode
4 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
E x p l a n a t i o n :
IncaseofififelseifelseStatementiffirstifclauseistruethecompilerwill nevercheckrestoftheifelseclauseandsoon.
5 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
(C)
Warning:xandyareassigneda
E x p l a n a t i o n :
6 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Itillegaltofindsizeofvoiddatatypeusingsizeofoperator.Becausesizeofvoiddata typeismeaningless.
7 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Answer
E x p l a n a t i o n :
Considerthefollowingexpression: q/n*m
Precedence 1
Operator /,*
Associate Lefttoright
Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Since10is nonzeronumbersoifclausewillexecuteandprint:WilliamGates
8 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
9 .
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
(A) Itwillprintnothing n j k l m
(B) BarackObama HuJintao NicolasSarkozy VladimirPutin n j k l m (C) BarackObama HuJintao NicolasSarkozy BenBernanke VladimirPutin n j k l m (D) HuJintao NicolasSarkozy VladimirPutin n j k l m (E) Compilationerror n j k l m
Answer
E x p l a n a t i o n :
Asweknowinclanguagezerorepresentsfalseandanynonzeronumberrepresentstrue.In thisprogramconditionoffirst,second,thirdandfifthifstatementsaretrue.
10.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
#include<stdio.h> voidmain(){
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Incitispossibletowriteelseclausewithoutanybody.
11.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Returntypeofprintffunctionisint.Thisfunctionreturnaintegralvaluewhichisequal
tonumberofcharctersprintffunctionwillprintonconsole.
Since9isnonzeronumbersoif(9)istrueconditionhenceifclausewillexecute whichcontainsaninfiniteloopbutduetobreakkeyworditwillcomeoutofloop.
12.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Considerthefollowingexpression: ++a||++b
Property2:
Tooptimizetheexecutiontimethereisrule,Expression2willonlyevaluateifandonlyif Expression1returnzero.
Inthisprograminitialvalueofais5.So++awillbe6.Since++aisreturninganon zeroso++bwillnotexecuteandifconditionwillbetrueandifclausewillbeexecuted.
13.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
else break }
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Thinkyourself
14.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Ifyouarenotusing{and}inifclausethenyoucanwriteonlyonestatement.Otherwise itwillcauseofcompilationerror:Misplaceelse
15.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
16.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Considerthefollowingexpression:
a<++a||b<++b
Precedence 1 2
Operator ++ <
Fromtableitisclearfirst++operatorwillperformtheoperationthen<operator. Oneimportantpropertyofpreincrement(++)operatoris:Inanyexpressionfirstpre incrementincrementsthevalueofvariablethenitassignssamefinalvalueofthevariable toallthatvariables.Sointheexpression:a<++a Initialvalueofvariableais5. Step1:Incrementthevalueofvariableainwholeexpression.Finalvalueofais6. Step2:Nowstartassigningvaluetoallaintheexpression.Afterassigning6expression willbe: 6<6 Sinceconditionisfalse.Sosecondexpressioni.e.b<++bwillbeevaluated.Again11<11 isfalse.So||willoperatorwillreturnzeroandelseclausewillexecute.
17.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
12 n j k l m x=1y=2 n j k l m 02 n j k l m x=0y=1 n j k l m 01 n j k l m
Answer
E x p l a n a t i o n :
Considerthefollowingexpression: x&&y
Property2:
Tooptimizetheexecutiontimethereisrule,Expression2willonlyevaluateifandonlyif Expression1returnanonzerovalue.
Inthisprograminitialvalueofxis1.Soxwillbezero.Sincexisreturningzeroso ywillnotexecuteandifconditionwillbefalse.Henceelsepartwillbeexecuted.
18.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
printf("AmericanBeauty") }
Chooseallthatapply:
E x p l a n a t i o n :
Readfollowingtutorial: Datatypetutorial
19.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
#include<stdio.h> voidmain(){ charc=256 char*ptr="Leon" if(c==0) while(!c) if(*ptr++) printf("%+u",c) else break }
Chooseallthatapply:
Answer
E x p l a n a t i o n :
Since256ispositivenumbermovefromzeroinclockwisedirection.Youwillgetfinal valueofciszero.
if(c==0) Itistruesincevalueofciszero. Negationoperatori.e.!isalwaysreturneitherzerooroneaccordingtofollowingrule: !0=1 !(Nonzeronumber)=0 So, !c=!0=1 Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.So while(!c)i.e.while(1)isalwaystrue. Intheaboveprogramprtischaracterpointer.Itispointingtofirstcharacterofstring Leonaccordingtofollowingdiagram: Intheabovefigurevaluein circlerepresentsASCIIvalue ofcorrespondingcharacter. Initially*ptrmeansL. So *ptrwillreturnASCIIvalue o f c h a r a c t e r c o n s t a n t L i.e.76 if(*ptr++)isequivalentto: if(L) isequivalentto:if (76).Itistruesoinfirst iterationitwillprint+0. Dueto++operationinsecond iterationptrwillpointto characterconstanteandso on.Whenptrwillpoint\0 i.e.nullcharacter.Itwill returnitsASCIIvaluei.e. 0.Soif(0)isfalse.Henceelsepartwillexecute.
20.
Whatwillbeoutputwhenyouwillexecutefollowingccode?
Chooseallthatapply:
E x p l a n a t i o n :
Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Henceelse partwillexecute.