Vous êtes sur la page 1sur 14

1 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ inta=5,b=10,c=1 if(a&&b>c){ printf("cquestionbank") } else{ break } }

Chooseallthatapply:

(A) (B) (C) (D) (E)


cquestionbank n j k l m Itwillprintnothing n j k l m Runtimeerror n j k l m Compilationerror n j k l m Noneoftheabove n j k l m

Answer

E x p l a n a t i o n :

Keywordbreakisnotsyntacticalpartofifelsestatement.Sowecannotusebreakkeyword inifelsestatement.Thiskeywordcanbeuseincaseoflooporswitchcasestatement. Hencewhenyouwillcompileabovecodecompilerwillshowanerrormessage:Misplaced break.

2 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#definePRINTprintf("StarWars")printf("Psycho") #include<stdio.h> voidmain(){ intx=1 if(x) PRINT else printf("TheShawshankRedemption") }

Chooseallthatapply:

(A) (B) (C) (D) (E)


StarsWarsPsycho n j k l m TheShawshankRedemption n j k l m Warning:Conditionisalwaystrue n j k l m Warning:Conditionisalwaysfalse n j k l m Compilationerror n j k l m

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")

Itisifstatementwithoutanyelse.Itisok. (ii) printf("Psycho")

Itisafunctioncall.Itisalsook (iii) else printf("TheShawshankRedemption")

Youcannotwriteelseclausewithoutanyifclause.Itiscauseofcompilationerror.Hence compilerwillshowanerrormessage:Misplacedelse

3 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#defineTrue5==5 #include<stdio.h> voidmain(){ if(.0010.1f) printf("DavidBeckham") elseif(True) printf("Ronaldinho") else printf("CristianoRonaldo") }

Chooseallthatapply:

(A) (B) (C) (D) (E)


DavidBeckham n j k l m Ronaldinho n j k l m CristianoRonaldo n j k l m Warning:Conditionisalwaystrue n j k l m Warning:Unreachablecode n j k l m

Answer

E x p l a n a t i o n :

Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Sointhe abovecode:

(0.001 0.1f)isnotzerosoitrepresentstrue.Soonlyifclausewillexecuteandit willprint:DavidBeckhamonconsole. Butitisbadprogrammingpracticetowriteconstantasaconditioninifclause.Hence compilerwillshowawarningmessage:Conditionisalwaystrue

Sinceconditionisalwaystrue,soelseclausewillneverexecute.Programcontrolcannot reachatelsepart.Socompilerwillshowanotherwarningmessage:

Unreachablecode

4 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ inta=100 if(a>10) printf("M.S.Dhoni") elseif(a>20) printf("M.E.KHussey") elseif(a>30) printf("A.B.devilliers") }

Chooseallthatapply:

j k l m (A) M.S.Dhoni n j k l m (B) A.B.devilliers n

(C) M.SDhoni M.E.KHussey A.B.deVilliers n j k l m (D)


Compilationerror:Morethanone

conditionsaretrue n j k l m j k l m (E) Noneoftheabove n



Answer

E x p l a n a t i o n :

IncaseofififelseifelseStatementiffirstifclauseistruethecompilerwill nevercheckrestoftheifelseclauseandsoon.

5 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ intx=1,y=1 if(++x=++y) printf("R.T.Ponting") else printf("C.H.Gayle") }

Chooseallthatapply:

j k l m (A) R.TPonting n j k l m (B) C.H.Gayle n

(C)

Warning:xandyareassigneda

valuethatisneverused n j k l m j k l m (D) Warning:Conditionisalwaystrue n j k l m (E) Compilationerror n



Answer

E x p l a n a t i o n :

Considerfollowingstatement: ++x=++y Asweknow++ispreincrementoperatorintheabovestatement.Thisoperatorincrements thevalueofanyintegralvariablebyoneandreturnthatvalue.Afterperformingpre incrementsabovestatementwillbe:

0=0 InClanguageitisillegaltoassignaconstantvaluetoanotherconstant.Leftsideof= operatormustbeacontaineri.e.avariable.Socompilerwillshowanerrormessage: Lvaluerequired

Incifyouassignanyvaluetovariablebutyoudontperformanyoperatororperform operationonlyusingunaryoperatoronthevariablethecomplierwillshowawarning message:Variableisassignedavaluethatisnever

6 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ if(sizeof(void)) printf("M.Muralilidaran") else printf("HarbhajanSingh") }

Chooseallthatapply:

(A) (B) (C) (D) (E)


M.Muralilidaran n j k l m HarbhajanSingh n j k l m Warning:Conditionisalwaysfalse n j k l m Compilationerror n j k l m Noneoftheabove n j k l m

Answer

E x p l a n a t i o n :

Itillegaltofindsizeofvoiddatatypeusingsizeofoperator.Becausesizeofvoiddata typeismeaningless.

7 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ intm=5,n=10,q=20 if(q/n*m) printf("WilliamGates") else printf("WarrenBuffet") printf("CarlosSlimHelu") }


Chooseallthatapply: j k l m (A) WilliamGates n j k l m (B) WarrenBuffetCarlosSlimHelu n

j k l m (C) Runtimeerror n j k l m (D) Compilationerror n j k l m (E) Noneoftheabove n


Answer

E x p l a n a t i o n :

Considerthefollowingexpression: q/n*m

Inthisexpressiontherearetwooperators.Theyare: /:Divisionoperator *:Multiplicationoperator Precedenceandassociateofeachoperatorisasfollow:

Precedence 1

Operator /,*

Associate Lefttoright

Precedenceofbothoperatorsissame.Henceassociatewilldecidewhichoperatorwill executefirst.SinceAssociateislefttoright.So/operatorwillexecutethen*operator willexecute. =q/n*m =20/10*5 =2*5 =10

Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Since10is nonzeronumbersoifclausewillexecuteandprint:WilliamGates

Sinceinelseclausethereisnotanyopeningandclosingcurlybracket.Socompilerwill treatonlyonestatementasaelsepart.Hencelaststatementi.e. printf("CarlosSlimHelu")

isnotpartofifelsestatement.Soattheendcompilerwillalsoprint:CarlosSlim Helu Sooutputofabovecodewillbe: WilliamGatesCarlosSlimHelu


8 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ if(!printf("MukeshAmbani")) if(printf("LakashmiMittal")) }

Chooseallthatapply:

(A) (B) (C) (D) (E)


MukeshAmbani n j k l m LakashmiMittal n j k l m Itwillprintnothing n j k l m MukeshAmbaniLakashmiMittal n j k l m Compilationerror:ifstatementwithoutbody n j k l m

Answer

E x p l a n a t i o n :

Returntypeofprintffunctionisint.Thisfunctionreturnaintegralvaluewhichisequal tonumberofcharactersaprintffunctionwillprintonconsole.Firstofallprintf functionwill:MukeshAmbani.Sinceitisprinting13charactersoitwillreturn13.So,

!printf("MukeshAmbani") =!13 =0 Inclanguagezerorepresentsfalse.Soif(0)isfalsesonextstatementwhichinsidethe bodyoffirstifstatementwillnotexecute.


9 .

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ if("ABC")printf("BarackObama\n") if(1)printf("HuJintao\n") if(.92L)printf("NicolasSarkozy\n") if(0)printf("BenBernanke\n") if('W')printf("VladimirPutin\n") }

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 :

ABC:Itisstringconstantanditwillalwaysreturnanonzeromemoryaddress. 0.92L:Itislongdoubleconstant. W:ItischaracterconstantanditsASCIIvalueis

Asweknowinclanguagezerorepresentsfalseandanynonzeronumberrepresentstrue.In thisprogramconditionoffirst,second,thirdandfifthifstatementsaretrue.

10.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){

if(0xA) if(052) if('\xeb') if('\012') printf("Tomhanks") else else else else }

Chooseallthatapply:

(A) (B) (C) (D) (E)


Tomhanks n j k l m Compilationerror:Misplacedelse n j k l m Compilationerror:Ifwithoutanybody n j k l m Compilationerror:Undefinedsymbol n j k l m Warning:Conditionisalwaystrue n j k l m

Answer

E x p l a n a t i o n :

oxA:Itishexadecimalintegerconstant. 052:Itoctalintegerconstant. \xeb:Itishexadecimalcharacterconstant. \012:Itisoctalcharacterconstant.

Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Allofthe aboveconstantsreturnanonzerovalue.Soallifconditionsintheaboveprogramare true.

Incitispossibletowriteelseclausewithoutanybody.

11.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ inta=10 if(printf("%d",a>=10)10) for() break else }

Chooseallthatapply:

(A) (B) (C) (D) (E)


Itwillprintnothing n j k l m 0 n j k l m 1 n j k l m Compilationerror:Misplacedelse n j k l m Infiniteloop n j k l m

Answer

E x p l a n a t i o n :

Returntypeofprintffunctionisint.Thisfunctionreturnaintegralvaluewhichisequal

tonumberofcharctersprintffunctionwillprintonconsole.

Operator>=willreturn1ifbothoperandsareeitherequalorfirstoperandisgraterthan secondoperand.Soa>=10willreturn1sinceaisequalto10.Thusprintffunctionwill print1.Sincethisfunctionisprintingonlyonecharactersoitwillalsoreturn1. So,printf("%d",a>=10)10 =110 =9

Since9isnonzeronumbersoif(9)istrueconditionhenceifclausewillexecute whichcontainsaninfiniteloopbutduetobreakkeyworditwillcomeoutofloop.

12.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ inta=5,b=10 if(++a||++b) printf("%d%d",a,b) else printf("JohnTerry") }

Chooseallthatapply:

(A) (B) (C) (D) (E)


510 n j k l m 611 n j k l m 610 n j k l m 511 n j k l m JohnTerry n j k l m

Answer

E x p l a n a t i o n :

Considerthefollowingexpression: ++a||++b

Inthisexpression||isLogicalORoperator.Twoimportantpropertiesofthisoperator are: Property1: (Expression1)||(Expression2) ||operatorreturns0ifandonlyifbothexpressionsreturnazerootherwiseit|| operatorreturns1.

Property2:

Tooptimizetheexecutiontimethereisrule,Expression2willonlyevaluateifandonlyif Expression1returnzero.

Inthisprograminitialvalueofais5.So++awillbe6.Since++aisreturninganon zeroso++bwillnotexecuteandifconditionwillbetrueandifclausewillbeexecuted.

13.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ staticinti for() if(i+++"TheMatrix") printf("Memento")

else break }

Chooseallthatapply:

(A) (B) (C) (D) (E)


ItwillprintMementoatonetime n j k l m ItwillprintMementoatthreetimes n j k l m ItwillprintMementoattentimes n j k l m ItwillprintMementoatinfinitetimes n j k l m Compilationerror:Unknownoperator+++ n j k l m

Answer

E x p l a n a t i o n :

Thinkyourself

14.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ intx=1 if(x) printf("TheGodfather") x else printf("%d",x) }

Chooseallthatapply:

(A) (B) (C) (D) (E)


TheGodfather n j k l m 1 n j k l m 0 n j k l m Compilationerror n j k l m Noneoftheabove n j k l m

Answer

E x p l a n a t i o n :

Ifyouarenotusing{and}inifclausethenyoucanwriteonlyonestatement.Otherwise itwillcauseofcompilationerror:Misplaceelse

15.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ if('\0') elseif(NULL) printf("cquestionbank") else }

Chooseallthatapply:

(A) (B) (C) (D) (E)


cquestionbank n j k l m Itwillprintnothing n j k l m Warning:Conditionisalwaystrue n j k l m Warning:Unreachablecode n j k l m Compilationerror:ifstatementwithoutanybody n j k l m

Answer

E x p l a n a t i o n :

\0 isnullcharacterconstant.ItsASCIIvalueiszero.if(0)meansfalsesoprogram controlwillcheckitelseifclause. NULLismacroconstantwhichhasbeendefinedinstdio.hwhichalsoreturnszero.


16.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ inta=5,b=10 clrscr() if(a<++a||b<++b) printf("%d%d",a,b) else printf("JohnTerry") }

Chooseallthatapply:

(A) (B) (C) (D) (E)


510 n j k l m 611 n j k l m 610 n j k l m Compilationerror n j k l m JohnTerry n j k l m

Answer

E x p l a n a t i o n :

Considerthefollowingexpression:

a<++a||b<++b

Intheaboveexpression||islogicalORoperator.Itdividesanyexpressioninthesub expressions.Inthiswaywehavetwosubexpressions: (1)a<++a (2)b<++b Intheexpression:a<++a Therearetwooperators.Thereprecedenceandassociateare:


Precedence 1 2

Operator ++ <

Associate Righttoleft Lefttoright

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?

#include<stdio.h> voidmain(){ intx=1,y=2 if(x&&y) printf("x=%dy=%d",x,y) else printf("%d%d",x,y) }

Chooseallthatapply:

(A) (B) (C) (D) (E)


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

Inthisexpression&&isLogicalANDoperator.Twoimportantpropertiesofthisoperator are: Property1: (Expression1)&&(Expression2) &&operatorreturns1ifandonlyifbothexpressionsreturnanonzerovalueotherwiseit &&operatorreturns0.

Property2:

Tooptimizetheexecutiontimethereisrule,Expression2willonlyevaluateifandonlyif Expression1returnanonzerovalue.

Inthisprograminitialvalueofxis1.Soxwillbezero.Sincexisreturningzeroso ywillnotexecuteandifconditionwillbefalse.Henceelsepartwillbeexecuted.

18.

Whatwillbeoutputwhenyouwillexecutefollowingccode?

#include<stdio.h> voidmain(){ signedinta=1 unsignedintb=1u if(a==b) printf("TheLordoftheRings") else

printf("AmericanBeauty") }

Chooseallthatapply:

j k l m (A) TheLordoftheRings n j k l m (B) AmericanBeauty n

(C) Compilationerror:Cannotcomparesigned numberwithunsignednumber n j k l m j k l m (D) Compilationerror:Undefinedsymbol1u n j k l m (E) Warning:Illegaloperation n



Answer

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:

(A) (B) (C) (D) (E)


+256+256+256+256 n j k l m 0000 n j k l m +0+0+0+0 n j k l m Itwillprint+256atinfinitetimes n j k l m Compilationerror n j k l m

Answer

E x p l a n a t i o n :

Intheaboveprogramcissigned(default)charvariable.Rangeofsignedcharvariablein Turbocisfrom128to127.Butweareassigning256whichisbeyondtherangeofvariable c.Hencevariablecwillstorecorrespondingcyclicvalueaccordingtofollowingdiagram:

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?

#include<stdio.h> voidmain(){ inta=2 if(a,a,a) printf("TheDalaiLama") else printf("JimRogers") }

Chooseallthatapply:

j k l m (A) TheDalaiLama n j k l m (B) JimRogers n j k l m (C) Runtimeerror n

(D) Compilationerror:Multipleparametersin ifstatement n j k l m j k l m (E) Noneoftheabove n



Answer

E x p l a n a t i o n :

Considerthefollowingexpression: a,a,a Inccommaisbehavesasseparatoraswellasoperator.Intheaboveexpressioncommais behavingasoperator.Commaoperatorenjoylestprecedenceinprecedencetableandits associativelyislefttoright.Sofirstofallleftmostcommaoperatorwillperform operationthenrightmostcommawilloperatorintheaboveexpression.

Afterperforminga:awillbe2 Afterperforminga:awillbe0 a=0

Asweknowinczerorepresentsfalseandanynonzeronumberrepresentstrue.Henceelse partwillexecute.

Vous aimerez peut-être aussi