Sei sulla pagina 1di 64

First Job. Dream Job. Freshersworld.

com

C Questions
Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS en ironment, The underl!ing machine is an "#$ s!stem, Program is compiled using Turbo C/C++ compiler. The program output ma! depend on the in%ormation based on this assumptions &%or e"ample si'eo%&int( )) * ma! be assumed(. Predict the output or error&s( %or the %ollo+ing, 1. void main() { int const * p=5; printf("%d",++(*p)); } Answer: Compiler error, Cannot modi%! a constant alue. Explanation, p is a pointer to a -constant integer-. .ut +e tried to change the alue o% the -constant integer-. 2. main() { char s[ ="man"; int i; for(i=!;s[ i ;i++) printf(""n%c%c%c%c",s[ i ,*(s+i),*(i+s),i[s ); } Answer: mmmm aaaa nnnn Explanation, s/i0, 1&i+s(, 1&s+i(, i/s0 are all di%%erent +a!s o% e"pressing the same idea. 2enerall! arra! name is the base address %or that arra!. 3ere s is the base address. i is the inde" number/displacement %rom the base address. So, indirecting it +ith 1 is same as s/i0. i/s0 ma! be surprising. .ut in the case o% C it is same as s/i0. #. main() { f$oat m% = 1.1; do&'$% (o& = 1.1; if(m%==(o&) printf(") $ov% *");
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com %$s% printf(") hat% *"); } Answer: I hate 5 Explanation, 6or %loating point numbers (%loat, double, long double) the alues cannot be predicted e"actl!. Depending on the number o% b!tes, the precession +ith o% the alue represented aries. 6loat ta7es 8 b!tes and long double ta7es 49 b!tes. So %loat stores 9.: +ith less precision than long double. Rule of Thumb: ;e er compare or at<least be cautious +hen using %loating point numbers +ith relational operators ()) , =, >, >), =),?) ) . +. main() { static int var = 5; printf("%d ",var,,); if(var) main(); } Answer: @8A*4 Explanation: Bhen static storage class is gi en, it is initiali'ed once. The change in the alue o% a static ariable is retained e en bet+een the %unction calls. Cain is also treated li7e an! other ordinar! %unction, +hich can be called recursi el!. 5. main() { int c[ ={2.-,#.+,+,../,5}; int 0,*p=c,*1=c; for(0=!;025;0++) { printf(" %d ",*c); ++1; } for(0=!;025;0++){ printf(" %d ",*p); ++p; } } Answer: ******A8$@ Explanation: Initiall! pointer c is assigned to both p and q. In the %irst loop, since onl! q is incremented and not c , the alue * +ill be printed @ times. In second loop p itsel% is incremented. So the alues * A 8 $ @ +ill be printed.
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com .. main() { %3t%rn int i; i=2!; printf("%d",i); } Answer: 4in5%r 6rror , 5nde%ined s!mbol DEiD Explanation: e"tern storage class in the %ollo+ing declaration, extern int i; speci%ies to the compiler that the memor! %or i is allocated in some other program and that address +ill be gi en to the current program at the time o% lin7ing. .ut lin7er %inds that no other ariable o% name i is a ailable in an! other program +ith memor! space allocated %or it. 3ence a lin7er error has occurred . /. main() { int i=,1,0=,1,5=!,$=2,m; m=i++770++775++88$++; printf("%d %d %d %d %d",i,0,5,$,m); } Answer: 994A4 Explanation : Fogical operations al+a!s gi e a result o% 1 or 0 . And also the logical A;D &GG( operator has higher priorit! o er the logical OH &II( operator. So the e"pression Ji++ && j++ && k++ is e"ecuted %irst. The result o% this e"pression is 9 &<4 GG <4 GG 9 ) 9(. ;o+ the e"pression is 9 II * +hich e aluates to 4 &because OH operator al+a!s gi es 4 e"cept %or J9 II 9K combination< %or +hich it gi es 9(. So the alue o% m is 4. The alues o% other ariables are also incremented b! 4. -. main() { char *p; printf("%d %d ",si9%of(*p),si9%of(p)); } Answer: 4* Explanation: The si'eo%&( operator gi es the number o% b!tes ta7en b! its operand. P is a character pointer, +hich needs one b!te %or storing its alue &a character(. 3ence si'eo%&1p( gi es a alue o% 4. Since it needs t+o b!tes to store the address o% the character pointer si'eo%&p( gi es *.
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

:. main() { int i=#; s;itch(i) { d%fa&$t<printf("9%ro"); cas% 1< printf("on%"); 'r%a5; cas% 2<printf("t;o"); 'r%a5; cas% #< printf("thr%%"); 'r%a5; } } Answer : three Explanation : The de%ault case can be placed an!+here inside the loop. It is e"ecuted onl! +hen all other cases doesnDt match. 1!. main() { printf("%3",,122+); } Answer: %%%9 Explanation : <4 is internall! represented as all 4Ds. Bhen le%t shi%ted %our times the least signi%icant 8 bits are %illed +ith 9Ds.The L" %ormat speci%ier speci%ies that the integer alue be printed as a he"adecimal alue. 11. main() { char strin=[ =">%$$o ?or$d"; disp$a((strin=); } void disp$a((char *strin=) { printf("%s",strin=); } Answer: @ompi$%r 6rror < T!pe mismatch in redeclaration o% %unction displa! Explanation : In third line, +hen the %unction displ ! is encountered, the compiler doesnDt 7no+ an!thing about the %unction displa!. It assumes the arguments and
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com return t!pes to be integers, &+hich is the de%ault t!pe(. Bhen it sees the actual %unction displ !" the arguments and t!pe contradicts +ith +hat it has assumed pre iousl!. 3ence a compile time error occurs. 12. main() { int c=, ,2; printf("c=%d",c); } Answer: c)*M Explanation: 3ere unar! minus &or negation( operator is used t+ice. Same maths rules applies, ie. minus 1 minus) plus. #ote: 3o+e er !ou cannot gi e li7e <<*. .ecause << operator can onl! be applied to ariables as a de$rement operator &eg., i<<(. * is a constant and not a ariable. 1#. Ad%fin% int char main() { int i=.5; printf("si9%of(i)=%d",si9%of(i)); } Answer: si'eo%&i()4 Explanation: Since the Nde%ine replaces the string int b! the macro $h r 1+. main() { int i=1!; i=BiC1+; Drintf ("i=%d",i); } Answer: i)9 Explanation: In the e"pression %i&1' , ;OT &?( operator has more precedence than J =K s!mbol. % is a unar! logical operator. ?i &?49( is 9 &not o% true is %alse(. 9=48 is %alse &'ero(. 15. Ainc$&d%2stdio.hC
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com main() { char s[ ={EaE,E'E,EcE,E"nE,EcE,E"!E}; char *p,*str,*str1; p=7s[# ; str=p; str1=s; printf("%d",++*p + ++*str1,#2); } Answer: OO Explanation: p is pointing to character DPnD. str4 is pointing to character DaD ++1p. -p is pointing to DPnD and that is incremented b! one.- the ASCII alue o% DPnD is 49, +hich is then incremented to 44. The alue o% ++1p is 44. ++1str4, str4 is pointing to DaD that is incremented b! 4 and it becomes DbD. ASCII alue o% DbD is :#. ;o+ per%orming &44 + :# Q A*(, +e get OO&-C-(M So +e get the output OO ,, -C- &Ascii is OO(. 1.. Ainc$&d%2stdio.hC main() { int a[2 [2 [2 = { {1!,2,#,+}, {5,.,/,-} }; int *p,*1; p=7a[2 [2 [2 ; *1=***a; printf("%d,,,,%d",*p,*1); } Answer: Some2arbageRalue<<<4 Explanation: p)Ga/*0/*0/*0 !ou declare onl! t+o *D arra!s, but !ou are tr!ing to access the third *D&+hich !ou are not declared( it +ill print garbage alues. 1S)111a starting address o% a is assigned integer pointer. ;o+ S is pointing to starting address o% a. I% !ou print 1S, it +ill print %irst element o% AD arra!. 1/. Ainc$&d%2stdio.hC main() { str&ct 33 { int 3=#; char nam%[ ="h%$$o"; }; str&ct 33 *s; printf("%d",s,C3);
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com printf("%s",s,Cnam%); } Answer: Compiler Trror Explanation: Uou should not initiali'e ariables in declaration 1-. Ainc$&d%2stdio.hC main() { str&ct 33 { int 3; str&ct (( { char s; str&ct 33 *p; }; str&ct (( *1; }; } Answer: Compiler Trror Explanation: The structure !! is nested +ithin structure "". 3ence, the elements are o% !! are to be accessed through the instance o% structure "", +hich needs an instance o% !! to be 7no+n. I% the instance is created a%ter de%ining the structure the compiler +ill not 7no+ about the instance relati e to "". 3ence %or nested structure !! !ou ha e to declare member. 1:. main() { printf(""na'"); printf(""'si"); printf(""rha"); } Answer: hai Explanation: Pn < ne+line Pb < bac7space Pr < line%eed 2!. main() { int i=5;
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com printf("%d%d%d%d%d%d",i++,i,,,++i,,,i,i); } Answer: 8@@8@ Explanation: The arguments in a %unction call are pushed into the stac7 %rom le%t to right. The e aluation is b! popping out %rom the stac7. and the e aluation is %rom right to le%t, hence the result. 21. Ad%fin% s1&ar%(3) 3*3 main() { int i; i = .+Fs1&ar%(+); printf("%d",i); } Answer: $8 Explanation: the macro call sSuare&8( +ill substituted b! 818 so the e"pression becomes i ) $8/818 . Since / and 1 has eSual priorit! the e"pression +ill be e aluated as &$8/8(18 i.e. 4$18 ) $8 22. main() { char *p="hai fri%nds",*p1; p1=p; ;hi$%(*pB=E"!E) ++*p++; printf("%s %s",p,p1); } Answer: ibV?gsV%oet Explanation: ++1p++ +ill be parse in the gi en order 1p that is alue at the location currentl! pointed b! p +ill be ta7en ++1p the retrie ed alue +ill be incremented +hen M is encountered the location +ill be incremented that is p++ +ill be e"ecuted 3ence, in the +hile loop initial alue pointed b! p is JhK, +hich is changed to JiK b! e"ecuting ++1p and pointer mo es to point, JaK +hich is similarl! changed to JbK and so on. Similarl! blan7 space is con erted to J?K. Thus, +e obtain alue in p becomes WibV?gsV%oetX and since p reaches JP9K and p4 points to p thus p4doesnot print an!thing. 2#. Ainc$&d% 2stdio.hC Ad%fin% a 1! main()
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com { Ad%fin% a 5! printf("%d",a); } Answer: @9 Explanation: The preprocessor directi es can be rede%ined an!+here in the program. So the most recentl! assigned alue +ill be ta7en. 2+. Ad%fin% c$rscr() 1!! main() { c$rscr(); printf("%d"n",c$rscr()); } Answer: 499 Explanation: Preprocessor e"ecutes as a seperate pass be%ore the e"ecution o% the compiler. So te"tual replacement o% clrscr&( to 499 occurs.The input program to compiler loo7s li7e this , main&( Y 499M print%&-LdPn-,499(M Z #ote: 499M is an e"ecutable statement but +ith no action. So it doesnDt gi e an! problem 25. main() { printf("%p",main); } Answer: Some address +ill be printed. Explanation: 6unction names are Vust addresses &Vust li7e arra! names are addresses(. main&( is also a %unction. So the address o% %unction main +ill be printed. Lp in print% speci%ies that the argument is an address. The! are printed as he"adecimal numbers. *O( main&( Y clrscr&(M
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com Z clrscr&(M Answer: ;o output/error Explanation: The %irst clrscr&( occurs inside a %unction. So it becomes a %unction call. In the second clrscr&(M is a %unction declaration &because it is not inside an! %unction(. *#( enum colors Y.FAC[,.F5T,2HTT;Z main&( Y print%&-Ld..Ld..Ld-,.FAC[,.F5T,2HTT;(M return&4(M Z Answer: 9..4..* Explanation: enum assigns numbers starting %rom 9, i% not e"plicitl! de%ined. *:( oid main&( Y char %ar 1%arther,1%arthestM print%&-Ld..Ld-,si'eo%&%arther(,si'eo%&%arthest((M Z Answer: 8..* Explanation: the second pointer is o% char t!pe and not a %ar pointer A9( main&( Y int i)899,V)A99M print%&-Ld..Ld-(M Z Answer: 899..A99 Explanation: print% ta7es the alues o% the %irst t+o assignments o% the program. An! number o% print%Ds ma! be gi en. All o% them ta7e onl! the %irst t+o

Freshersworld.com Resource Center

49

First Job. Dream Job. Freshersworld.com alues. I% more number o% assignments gi en in the program,then print% +ill ta7e garbage alues. A4( main&( Y char 1pM p)-3ello-M print%&-LcPn-,1G1p(M Z Answer: 3 Explanation: 1 is a dere%erence operator G is a re%erence operator. The! can be applied an! number o% times pro ided it is meaning%ul. 3ere p points to the %irst character in the string -3ello-. 1p dere%erences it and so its alue is 3. Again G re%erences it to an address and 1 dere%erences it to the alue 3. main&( Y int i)4M +hile &i>)@( Y print%&-Ld-,i(M i% &i=*( goto hereM i++M Z Z %un&( Y here, print%&-PP-(M Z Answer: Compiler error, 5nde%ined label DhereD in %unction main Explanation: Fabels ha e %unctions scope, in other +ords The scope o% the labels is limited to %unctions . The label DhereD is a ailable in %unction %un&( 3ence it is not isible in %unction main. main&( Y static char names/@0/*90)Y-pascal-,-ada-,-cobol-,-%ortran-,-perl-ZM int iM char 1tM
Freshersworld.com Resource Center

A*(

AA(

44

First Job. Dream Job. Freshersworld.com t)names/A0M names/A0)names/80M names/80)tM %or &i)9Mi>)8Mi++( print%&-Ls-,names/i0(M Z Answer: Compiler error, F alue reSuired in %unction main Explanation: Arra! names are pointer constants. So it cannot be modi%ied. A8( Y int i)@M print%&-Ld-,i++ + ++i(M Z Answer: Output Cannot be predicted e"actl!. Explanation: Side e%%ects are in ol ed in the e aluation o% i A@( Y int i)@M print%&-Ld-,i+++++i(M Z Answer: Compiler Trror Explanation: The e"pression i+++++i is parsed as i ++ ++ + i +hich is an illegal combination o% operators. A$( Ninclude>stdio.h= main&( Y int i)4,V)*M s+itch&i( Y case 4, print%&-2OOD-(M brea7M case V, print%&-.AD-(M brea7M Z Z Answer: Compiler Trror, Constant e"pression reSuired in %unction main.
Freshersworld.com Resource Center

oid main&(

oid main&(

4*

First Job. Dream Job. Freshersworld.com Explanation: The case statement can ha e onl! constant e"pressions &this implies that +e cannot use ariable names directl! so an error(. #ote: Tnumerated t!pes can be used in case statements. AO( main&( Y int iM print%&-Ld-,scan%&-Ld-,Gi((M // alue 49 is gi en as input here Z Answer: 4 Explanation: Scan% returns number o% items success%ull! read and not 4/9. 3ere 49 is gi en as input +hich should ha e been scanned success%ull!. So number o% items read is 4. Nde%ine %&g,g*( gNNg* main&( Y int ar4*)499M print%&-Ld-,%& ar,4*((M Z Answer: 499 main&( Y int i)9M %or&Mi++Mprint%&-Ld-,i(( M print%&-Ld-,i(M Z Answer: 4 Explanation: be%ore entering into the %or loop the chec7ing condition is -e aluated-. 3ere it e aluates to 9 &%alse( and comes out o% the loop, and i is incremented &note the semicolon a%ter the %or loop(. 89( Ninclude>stdio.h= main&( Y char s/0)YDaD,DbD,DcD,DPnD,DcD,DP9DZM char 1p,1str,1str4M
Freshersworld.com Resource Center

A#(

A:(

4A

First Job. Dream Job. Freshersworld.com p)Gs/A0M str)pM str4)sM print%&-Ld-,++1p + ++1str4<A*(M Z Answer: C Explanation: p is pointing to character DPnD.str4 is pointing to character DaD ++1p meAns+er,-p is pointing to DPnD and that is incremented b! one.- the ASCII alue o% DPnD is 49. then it is incremented to 44. the alue o% + +1p is 44. ++1str4 meAns+er,-str4 is pointing to DaD that is incremented b! 4 and it becomes DbD. ASCII alue o% DbD is :#. both 44 and :# is added and result is subtracted %rom A*. i.e. &44+:#<A*()OO&-C-(M 84( Ninclude>stdio.h= main&( Y struct "" Y int ")AM char name/0)-hello-M ZM struct "" 1s)malloc&si'eo%&struct ""((M print%&-Ld-,s<="(M print%&-Ls-,s<=name(M Z Answer: Compiler Trror Explanation: Initiali'ation should not be done %or structure members inside the structure declaration Ninclude>stdio.h= main&( Y struct "" Y int "M struct !! Y char sM struct "" 1pM ZM struct !! 1SM
Freshersworld.com Resource Center

8*(

48

First Job. Dream Job. Freshersworld.com ZM Z Answer: Compiler Trror Explanation: in the end o% nested structure !! a member ha e to be declared. 8A( main&( Y e"tern int iM i)*9M print%&-Ld-,si'eo%&i((M Z Answer: Fin7er error, unde%ined s!mbol DEiD. Explanation: e"tern declaration speci%ies that the ariable i is de%ined some+here else. The compiler passes the e"ternal ariable to be resol ed b! the lin7er. So compiler doesnDt %ind an error. During lin7ing the lin7er searches %or the de%inition o% i. Since it is not %ound the lin7er %lags an error. main&( Y print%&-Ld-, out(M Z int out)499M Answer: Compiler error, unde%ined s!mbol out in %unction main. Explanation: The rule is that a ariable is a ailable %or use %rom the point o% declaration. T en though a is a global ariable, it is not a ailable %or main. 3ence an error. main&( Y e"tern outM print%&-Ld-, out(M Z int out)499M Answer: 499 Explanation: This is the correct +a! o% +riting the pre ious program. main&(
Freshersworld.com Resource Center

88(

8@(

8$(

4@

First Job. Dream Job. Freshersworld.com Y sho+&(M Z oid sho+&( Y print%&-IDm the greatest-(M Z Answer: Compier error, T!pe mismatch in redeclaration o% sho+. Explanation: Bhen the compiler sees the %unction sho+ it doesnDt 7no+ an!thing about it. So the de%ault return t!pe &ie, int( is assumed. .ut +hen compiler sees the actual de%inition o% sho+ mismatch occurs since it is declared as oid. 3ence the error. The solutions are as %ollo+s, 4. declare oid sho+&( in main&( . *. de%ine sho+&( be%ore main&(. A. declare e"tern oid sho+&( be%ore the use o% sho+&(. 8O( main& ( Y int a/*0/A0/*0 ) YYY*,8Z,YO,#Z,YA,8ZZ,YY*,*Z,Y*,AZ,YA,8ZZZM print%&WLu Lu Lu Ld PnX,a,1a,11a,111a(M print%&WLu Lu Lu Ld PnX,a+4,1a+4,11a+4,111a+4(M Z Answer: 499, 499, 499, * 448, 498, 49*, A Explanation: The gi en arra! is a A<D one. It can also be ie+ed as a 4<D arra!. * 8 O # A 8 * * * A A 8 499 49* 498 49$ 49# 449 44* 448 44$ 44# 4*9 4** thus, %or the %irst print% statement a, 1a, 11a gi e address o% %irst element . since the indirection 111a gi es the alue. 3ence, the %irst line o% the output. %or the second print% a+4 increases in the third dimension thus points to alue at 448, 1a+4 increments in second dimension thus points to 498, 11a +4 increments the %irst dimension thus points to 49* and 111a+4 %irst gets the alue at %irst location and then increments it b! 4. 3ence, the output. 8#( main& ( Y
Freshersworld.com Resource Center

4$

First Job. Dream Job. Freshersworld.com int a/ 0 ) Y49,*9,A9,89,@9Z,V,1pM %or&V)9M V>@M V++( Y print%&WLdX ,1a(M a++M Z p ) aM %or&V)9M V>@M V++( Y print%&WLd X ,1p(M p++M Z Z Answer: Compiler error, l alue reSuired. Explanation: Trror is in line +ith statement a++. The operand must be an l alue and ma! be o% an! o% scalar t!pe %or the an! operator, arra! name onl! +hen subscripted is an l alue. Simpl! arra! name is a non<modi%iable l alue. 8:( main& ( Y static int a/ 0 ) Y9,4,*,A,8ZM int 1p/ 0 ) Ya,a+4,a+*,a+A,a+8ZM int 11ptr ) pM ptr++M print%&WPn Ld Ld LdX, ptr<p, 1ptr<a, 11ptr(M 1ptr++M print%&WPn Ld Ld LdX, ptr<p, 1ptr<a, 11ptr(M 1++ptrM print%&WPn Ld Ld LdX, ptr<p, 1ptr<a, 11ptr(M ++1ptrM print%&WPn Ld Ld LdX, ptr<p, 1ptr<a, 11ptr(M Z Answer: 111 222 ### #++ Explanation: Fet us consider the arra! and the t+o pointers +ith some address 9 499 4 49* * 498 A 49$ 8 49#

Freshersworld.com Resource Center

4O

First Job. Dream Job. Freshersworld.com p 499 49* 498 49$ 49# 4999 499* 4998 499$ 499# ptr 4999 *999 A%ter e"ecution o% the instruction ptr++ alue in ptr becomes 499*, i% scaling %actor %or integer is * b!tes. ;o+ ptr Q p is alue in ptr Q starting location o% arra! p, &499* Q 4999( / &scaling %actor( ) 4, 1ptr Q a ) alue at address pointed b! ptr Q starting alue o% arra! a, 499* has a alue 49* so the alue is &49* Q 499(/&scaling %actor( ) 4, 11ptr is the alue stored in the location pointed b! the pointer o% ptr ) alue pointed b! alue pointed b! 499* ) alue pointed b! 49* ) 4. 3ence the output o% the %irs print% is 4, 4, 4. A%ter e"ecution o% 1ptr++ increments alue o% the alue in ptr b! scaling %actor, so it becomes4998. 3ence, the outputs %or the second print% are ptr Q p ) *, 1ptr Q a ) *, 11ptr ) *. A%ter e"ecution o% 1++ptr increments alue o% the alue in ptr b! scaling %actor, so it becomes4998. 3ence, the outputs %or the third print% are ptr Q p ) A, 1ptr Q a ) A, 11ptr ) A. A%ter e"ecution o% ++1ptr alue in ptr remains the same, the alue pointed b! the alue is incremented b! the scaling %actor. So the alue in arra! p at location 499$ changes %rom 49$ 49 49#,. 3ence, the outputs %or the %ourth print% are ptr Q p ) 499$ Q 4999 ) A, 1ptr Q a ) 49# Q 499 ) 8, 11ptr ) 8. @9( main& ( Y char 1SM int VM %or &V)9M V>AM V++( scan%&WLsX ,&S+V((M %or &V)9M V>AM V++( print%&WLcX ,1&S+V((M %or &V)9M V>AM V++( print%&WLsX ,&S+V((M Z Explanation: 3ere +e ha e onl! one pointer to t!pe char and since +e ta7e input in the same pointer thus +e 7eep +riting o er in the same location, each time shi%ting the pointer alue b! 4. Suppose the inputs are CO5ST, THAC[ and RIHT5AF. Then %or the %irst input suppose the pointer starts at location 499 then the input one is stored as C O 5 S T P9 Bhen the second input is gi en the pointer is incremented as V alue becomes 4, so the input is %illed in memor! starting %rom 494. C T H A C [ P9 The third input starts %illing %rom the location 49*

Freshersworld.com Resource Center

4#

First Job. Dream Job. Freshersworld.com C T R I H T 5 A F P9 This is the %inal alue stored . The %irst print% prints the alues at the position S, S+4 and S+* ) C T R The second print% prints three strings starting %rom locations S, S+4, S+* i.e CTRIHT5AF, TRIHT5AF and RIHT5AF. @4( main& ( Y oid 1 pM char ch ) JgK, 1cp ) Wgoo%!XM int V ) *9M p ) GchM print%&WLcX, 1&char 1( p(M p ) GVM print%&WLdX,1&int 1( p(M p ) cpM print%&WLsX,&char 1( p + A(M Z Answer: g*9%! Explanation: Since a oid pointer is used it can be t!pe casted to an! other t!pe pointer. p ) Gch stores address o% char ch and the ne"t statement prints the alue stored in p a%ter t!pe casting it to the proper data t!pe pointer. the output is JgK. Similarl! the output %rom second print% is J*9K. The third print% statement t!pe casts it to print the string %rom the 8th alue hence the output is J%!K. main & ( Y static char 1s/ 0 ) YWblac7X, W+hiteX, W!ello+X, W ioletXZM char 11ptr/ 0 ) Ys+A, s+*, s+4, sZ, 111pM p ) ptrM 11++pM print%&WLsX,1<<1++p + A(M Z Answer: c7 Explanation: In this problem +e ha e an arra! o% char pointers pointing to start o% 8 strings. Then +e ha e ptr +hich is a pointer to a pointer o% t!pe char and a ariable p +hich is a pointer to a pointer to a pointer o% t!pe char. p hold the initial alue o% ptr, i.e. p ) s+A. The ne"t statement increment alue in p b! 4 , thus no+ alue o% p ) s+*. In the print%
Freshersworld.com Resource Center

@*(

4:

First Job. Dream Job. Freshersworld.com statement the e"pression is e aluated 1++p causes gets alue s+4 then the pre decrement is e"ecuted and +e get s+4 Q 4 ) s . the indirection operator no+ gets the alue %rom the arra! o% s and adds A to the starting address. The string is printed starting %rom this position. Thus, the output is Jc7K. @A( main&( Y int i, nM char 1" ) WgirlXM n ) strlen&"(M 1" ) "/n0M %or&i)9M i>nM ++i( Y print%&WLsPnX,"(M "++M Z Z Answer: &blan7 space( irl rl l Explanation: 3ere a string &a pointer to char( is initiali'ed +ith a alue WgirlX. The strlen %unction returns the length o% the string, thus n has a alue 8. The ne"t statement assigns alue at the nth location &JP9K( to the %irst location. ;o+ the string becomes WP9irlX . ;o+ the print% statement prints the string a%ter each iteration it increments it starting position. Foop starts %rom 9 to 8. The %irst time "/90 ) JP9K hence it prints nothing and pointer alue is incremented. The second time it prints %rom "/40 i.e WirlX and the third time it prints WrlX and the last time it prints WlX and the loop terminates. int i,VM %or&i)9Mi>)49Mi++( Y V+)@M assert&i>@(M Z Answer: Huntime error, Abnormal program termination. assert %ailed &i>@(, >%ile name=,>line number= Explanation:

@8(

Freshersworld.com Resource Center

*9

First Job. Dream Job. Freshersworld.com asserts are used during debugging to ma7e sure that certain conditions are satis%ied. I% assertion %ails, the program +ill terminate reporting the same. A%ter debugging use, Nunde% ;DT.52 and this +ill disable all the assertions %rom the source code. Assertion is a good debugging tool to ma7e use o%. @@( main&( Y int i)<4M +iM print%&-i ) Ld, +i ) Ld Pn-,i,+i(M Z Answer: i ) <4, +i ) <4 Explanation: 5nar! + is the onl! dumm! operator in C. Bhere<e er it comes !ou can Vust ignore it Vust because it has no e%%ect in the e"pressions &hence the name dumm! operator(. Bhat are the %iles +hich are automaticall! opened +hen a C %ile is e"ecuted\ Answer: stdin, stdout, stderr &standard input,standard output,standard error(.

@$(

@O( +hat +ill be the position o% the %ile mar7er\ a, %see7&ptr,9,STT[ESTT(M b, %see7&ptr,9,STT[EC5H(M Answer : a, The STT[ESTT sets the %ile position mar7er to the starting o% the %ile. b, The STT[EC5H sets the %ile position mar7er to the current position o% the %ile. @#( main&( Y char name/490,s/4*0M scan%&- P-L/]P-0P--,s(M Z 3o+ scan% +ill e"ecute\ Answer: 6irst it chec7s %or the leading +hite space and discards it.Then it matches +ith a Suotation mar7 and then it reads all character upto another Suotation mar7. Bhat is the problem +ith the %ollo+ing code segment\
Freshersworld.com Resource Center

@:(

*4

First Job. Dream Job. Freshersworld.com +hile &&%gets&recei ing arra!,@9,%ileEptr(( ?) TO6( M Answer & Explanation: %gets returns a pointer. So the correct end o% %ile chec7 is chec7ing %or ? ) ;5FF. $9( main&( Y main&(M Z Answer: Huntime error , Stac7 o er%lo+. Explanation: main %unction calls itsel% again and again. Tach time the %unction is called its return address is stored in the call stac7. Since there is no condition to terminate the %unction call, the call stac7 o er%lo+s at runtime. So it terminates the program and results in an error. main&( Y char 1cptr,cM oid 1 ptr, M c)49M )9M cptr)GcM ptr)G M print%&-LcL -,c, (M Z Answer: Compiler error &at line number 8(, si'e o% is 5n7no+n. Explanation: Uou can create a ariable o% t!pe oid 1 but not o% t!pe oid, since oid is an empt! t!pe. In the second line !ou are creating ariable ptr o% t!pe oid 1 and o% t!pe oid hence an error. main&( Y char 1str4)-abcd-M char str*/0)-abcd-M print%&-Ld Ld Ld-,si'eo%&str4(,si'eo%&str*(,si'eo%&-abcd-((M Z Answer: *@@ Explanation: In %irst si'eo%, str4 is a character pointer so it gi es !ou the si'e o% the pointer ariable. In second si'eo% the name str* indicates the name o% the arra! +hose si'e is @ &including the DP9D termination character(. The third si'eo% is similar to the second one.
Freshersworld.com Resource Center

$4(

$*(

**

First Job. Dream Job. Freshersworld.com

$A(

main&( Y char notM not)?*M print%&-Ld-,not(M Z Answer: 9 Explanation: ? is a logical operator. In C the alue 9 is considered to be the boolean alue 6AFST, and an! non<'ero alue is considered to be the boolean alue TH5T. 3ere * is a non<'ero alue so TH5T. ?TH5T is 6AFST &9( so it prints 9. Nde%ine 6AFST <4 Nde%ine TH5T 4 Nde%ine ;5FF 9 main&( Y i%&;5FF( puts&-;5FF-(M else i%&6AFST( puts&-TH5T-(M else puts&-6AFST-(M Z Answer: TH5T Explanation, The input program to the compiler a%ter processing b! the preprocessor is, main&(Y i%&9( puts&-;5FF-(M else i%&<4( puts&-TH5T-(M else puts&-6AFST-(M Z Preprocessor doesnDt replace the alues gi en inside the double Suotes. The chec7 b! i% condition is boolean alue %alse so it goes to else. In second i% <4 is boolean alue true hence -TH5T- is printed. main&( Y int 7)4M
Freshersworld.com Resource Center

$8(

$@(

*A

First Job. Dream Job. Freshersworld.com print%&-Ld))4 is --Ls-,7,7))4\-TH5T-,-6AFST-(M Z Answer: 4))4 is TH5T Explanation: Bhen t+o strings are placed together &or separated b! +hite<space( the! are concatenated &this is called as -stringi'ation- operation(. So the string is as i% it is gi en as -Ld))4 is Ls-. The conditional operator& \, ( e aluates to -TH5T-. $$( main&( Y int !M scan%&-Ld-,G!(M // input gi en is *999 i%& &!L8))9 GG !L499 ?) 9( II !L499 )) 9 ( print%&-Ld is a leap !ear-(M else print%&-Ld is not a leap !ear-(M Z Answer: *999 is a leap !ear Explanation: An ordinar! program to chec7 i% leap !ear or not. Nde%ine ma" @ Nde%ine int arr4/ma"0 main&( Y t!pede% char arr*/ma"0M arr4 list)Y9,4,*,A,8ZM arr* name)-name-M print%&-Ld Ls-,list/90,name(M Z Answer: Compiler error &in the line arr4 list ) Y9,4,*,A,8Z( Explanation: arr* is declared o% t!pe arra! o% si'e @ o% characters. So it can be used to declare the ariable name o% the t!pe arr*. .ut it is not the case o% arr4. 3ence an error. Rule of Thumb: Nde%ines are used %or te"tual replacement +hereas t!pede%s are used %or declaring ne+ t!pes. int i)49M main&( Y
Freshersworld.com Resource Center

$O(

$#(

*8

First Job. Dream Job. Freshersworld.com e"tern int iM Y int i)*9M Y const olatile unsigned i)A9M print%&-Ld-,i(M Z print%&-Ld-,i(M Z print%&-Ld-,i(M Z Answer: A9,*9,49 Explanation: DYD introduces ne+ bloc7 and thus ne+ scope. In the innermost bloc7 i is declared as, const olatile unsigned +hich is a alid declaration. i is assumed o% t!pe int. So print% prints A9. In the ne"t bloc7, i has alue *9 and so print% prints *9. In the outermost bloc7, i is declared as e"tern, so no storage space is allocated %or it. A%ter compilation is o er the lin7er resol es it to global ariable i &since it is the onl! ariable isible there(. So it prints iDs alue as 49. $:( main&( Y int 1VM Y int i)49M V)GiM Z print%&-Ld-,1V(M Z Answer: 49 Explanation, The ariable i is a bloc7 le el ariable and the isibilit! is inside that bloc7 onl!. .ut the li%etime o% i is li%etime o% the %unction so it li es upto the e"it o% main %unction. Since the i is still allocated space, 1V prints the alue stored in i since V points i. main&( Y int i)<4M <iM print%&-i ) Ld, <i ) Ld Pn-,i,<i(M
Freshersworld.com Resource Center

O9(

*@

First Job. Dream Job. Freshersworld.com Z Answer: i ) <4, <i ) 4 Explanation: <i is e"ecuted and this e"ecution doesnDt a%%ect the alue o% i. In print% %irst !ou Vust print the alue o% i. A%ter that the alue o% the e"pression <i ) <&<4( is printed. O4( Ninclude>stdio.h= main&( Y const int i)8M %loat VM V ) ++iM print%&-Ld L%-, i,++V(M Z Answer, Compiler error Explanation, i is a constant. !ou cannot change the alue o% constant Ninclude>stdio.h= main&( Y int a/*0/*0/*0 ) Y Y49,*,A,8Z, Y@,$,O,#Z ZM int 1p,1SM p)Ga/*0/*0/*0M 1S)111aM print%&-Ld..Ld-,1p,1S(M Z Answer: garbage alue..4 Explanation: p)Ga/*0/*0/*0 !ou declare onl! t+o *D arra!s. but !ou are tr!ing to access the third *D&+hich !ou are not declared( it +ill print garbage alues. 1S)111a starting address o% a is assigned integer pointer. no+ S is pointing to starting address o% a.i% !ou print 1S meAns+er,it +ill print %irst element o% AD arra!. Ninclude>stdio.h= main&( Y register i)@M char V/0) -hello-M print%&-Ls Ld-,V,i(M Z
Freshersworld.com Resource Center

O*(

OA(

*$

First Job. Dream Job. Freshersworld.com Answer: hello @ Explanation, i% !ou declare i as register compiler +ill treat it as ordinar! integer and it +ill ta7e integer alue. i alue ma! be stored either in register or in memor!. O8( main&( Y int i)@,V)$,'M print%&-Ld-,i+++V(M Z Answer: 44 Explanation: the e"pression i+++V is treated as &i++ + V( struct aaaY struct aaa 1pre M int iM struct aaa 1ne"tM ZM main&( Y struct aaa abc,de%,ghi,V7lM int ")499M abc.i)9Mabc.pre )GV7lM abc.ne"t)Gde%M de%.i)4Mde%.pre )GabcMde%.ne"t)GghiM ghi.i)*Mghi.pre )Gde%M ghi.ne"t)GV7lM V7l.i)AMV7l.pre )GghiMV7l.ne"t)GabcM ")abc.ne"t<=ne"t<=pre <=ne"t<=iM print%&-Ld-,"(M Z Answer: * Explanation: abo e all statements %orm a double circular lin7ed listM abc.ne"t<=ne"t<=pre <=ne"t<=i this one points to -ghi- node the alue o% at particular node is *. struct point Y int "M int !M
Freshersworld.com Resource Center

O$(

OO(

*O

First Job. Dream Job. Freshersworld.com ZM struct point origin,1ppM main&( Y pp)GoriginM print%&-origin is&LdLd(Pn-,&1pp(.",&1pp(.!(M print%&-origin is &LdLd(Pn-,pp<=",pp<=!(M Z Answer: origin is&9,9( origin is&9,9( Explanation, pp is a pointer to structure. +e can access the elements o% the structure either +ith arro+ mar7 or +ith indirection operator. #ote: Since structure point is globall! declared " G ! are initiali'ed as 'eroes O#( main&( Y int i)ElEabc&49(M print%&-LdPn-,<<i(M Z int ElEabc&int i( Y return&i++(M Z Answer: : Explanation: return&i++( it +ill %irst return i and then increments. i.e. 49 +ill be returned. main&( Y char 1pM int 1SM long 1rM p)S)r)9M p++M S++M r++M print%&-Lp...Lp...Lp-,p,S,r(M Z Answer:
Freshersworld.com Resource Center

O:(

*#

First Job. Dream Job. Freshersworld.com 9994...999*...9998 Explanation: ++ operator +hen applied to pointers increments address according to their corresponding data<t!pes. #9( main&( Y char c)D D,",con ert&'(M getc&c(M i%&&c=)DaD( GG &c>)D'D(( ")con ert&c(M print%&-Lc-,"(M Z con ert&'( Y return '<A*M Z Answer: Compiler error Explanation: declaration o% con ert and %ormat o% getc&( are +rong. main&int argc, char 11arg ( Y print%&-enter the character-(M getchar&(M sum&arg /40,arg /*0(M Z sum&num4,num*( int num4,num*M Y return num4+num*M Z Answer: Compiler error. Explanation: arg /40 G arg /*0 are strings. The! are passed to the %unction sum +ithout con erting it to integer alues. N include >stdio.h= int oneEd/0)Y4,*,AZM main&( Y int 1ptrM ptr)oneEdM ptr+)AM
Freshersworld.com Resource Center

#4(

#*(

*:

First Job. Dream Job. Freshersworld.com print%&-Ld-,1ptr(M Z Answer, garbage alue Explanation: ptr pointer is pointing to out o% the arra! range o% oneEd. #A( N include>stdio.h= aaa&( Y print%&-hi-(M Z bbb&(Y print%&-hello-(M Z ccc&(Y print%&-b!e-(M Z main&( Y int &1ptr/A0(&(M ptr/90)aaaM ptr/40)bbbM ptr/*0)cccM ptr/*0&(M Z Answer, b!e Explanation: ptr is arra! o% pointers to %unctions o% return t!pe int.ptr/90 is assigned to address o% the %unction aaa. Similarl! ptr/40 and ptr/*0 %or bbb and ccc respecti el!. ptr/*0&( is in e%%ect o% +riting ccc&(, since ptr/*0 points to ccc. Ninclude>stdio.h= main&( Y 6IFT 1ptrM char iM ptr)%open&-'''.c-,-r-(M +hile&&i)%getch&ptr((?)TO6( print%&-Lc-,i(M Z Answer: contents o% '''.c %ollo+ed b! an in%inite loop Explanation:

#@(

Freshersworld.com Resource Center

A9

First Job. Dream Job. Freshersworld.com The condition is chec7ed against TO6, it should be chec7ed against ;5FF. #$( main&( Y int i )9MV)9M i%&i GG V++( print%&-Ld..Ld-,i++,V(M print%&-Ld..Ld,i,V(M Z Answer: 9..9 Explanation: The alue o% i is 9. Since this in%ormation is enough to determine the truth alue o% the boolean e"pression. So the statement %ollo+ing the i% statement is not e"ecuted. The alues o% i and V remain unchanged and get printed. main&( Y int iM i ) abc&(M print%&-Ld-,i(M Z abc&( Y EA^ ) 4999M Z Answer: 4999 Explanation: ;ormall! the return alue %rom the %unction is through the in%ormation %rom the accumulator. 3ere EA3 is the pseudo global ariable denoting the accumulator. 3ence, the alue o% the accumulator is set 4999 so the %unction returns alue 4999. int iM main&(Y int tM %or & t)8Mscan%&-Ld-,Gi(<tMprint%&-LdPn-,i(( print%&-Ld<<-,t<<(M Z // I% the inputs are 9,4,*,A %ind the o/p Answer: 8<<9 A<<4
Freshersworld.com Resource Center

#O(

##(

A4

First Job. Dream Job. Freshersworld.com *<<* Explanation: Fet us assume some ") scan%&-Ld-,Gi(<t the alues during e"ecution +ill be, t i " 8 9 <8 A 4 <* * * 9 #:( main&(Y int a) 9Mint b ) *9Mchar " )4Mchar ! )49M i%&a,b,",!( print%&-hello-(M Z Answer: hello Explanation: The comma operator has associati it! %rom le%t to right. Onl! the rightmost alue is returned and the other alues are e aluated and ignored. Thus the alue o% last ariable ! is returned to chec7 in i%. Since it is a non 'ero alue i% becomes true so, -hello- +ill be printed. main&(Y unsigned int iM %or&i)4Mi=<*Mi<<( print%&-c aptitude-(M Z Explanation: i is an unsigned integer. It is compared +ith a signed alue. Since the both t!pes doesnDt match, signed is promoted to unsigned alue. The unsigned eSui alent o% <* is a huge alue so condition becomes %alse and control comes out o% the loop. In the %ollo+ing pgm add a stmt in the %unction %un such that the address o% DaD gets stored in DVD. main&(Y int 1 VM oid %un&int 11(M %un&GV(M Z oid %un&int 117( Y int a )9M /1 add a stmt here1/ Z Answer: 17 ) Ga
Freshersworld.com Resource Center

:9(

:4(

A*

First Job. Dream Job. Freshersworld.com Explanation: The argument o% the %unction is a pointer to a pointer. :*( Bhat are the %ollo+ing notations o% de%ining %unctions 7no+n as\ i. int abc&int a,%loat b( Y /1 some code 1/ Z ii. int abc&a,b( int aM %loat bM Y /1 some code1/ Z Answer: i. A;SI C notation ii. [ernighan G Hitche notation main&( Y char 1pM p)-LdPn-M p++M p++M print%&p<*,A99(M Z Answer: A99 Explanation: The pointer points to L since it is incremented t+ice and again decremented b! *, it points to DLdPnD and A99 is printed. main&(Y char a/4990M a/90)DaDMa/400)DbDMa/*0)DcDMa/80)DdDM abc&a(M Z abc&char a/0(Y a++M print%&-Lc-,1a(M a++M print%&-Lc-,1a(M Z Explanation: The base address is modi%ied onl! in %unction and as a result a points to DbD then a%ter incrementing to DcD so bc +ill be printed.

:A(

:8(

Freshersworld.com Resource Center

AA

First Job. Dream Job. Freshersworld.com :@( %unc&a,b( int a,bM Y return& a) &a))b( (M Z main&( Y int process&(,%unc&(M print%&-The alue o% process is Ld ?Pn -,process&%unc,A,$((M Z process&p%, al4, al*( int &1p%( &(M int al4, al*M Y return&&1p%( & al4, al*((M Z Answer: The alue i% process is 9 ? Explanation: The %unction DprocessD has A parameters < 4, a pointer to another %unction * and A, integers. Bhen this %unction is in o7ed %rom main, the %ollo+ing substitutions %or %ormal parameters ta7e place, %unc %or p%, A %or al4 and $ %or al*. This %unction returns the result o% the operation per%ormed b! the %unction D%uncD. The %unction %unc has t+o integer parameters. The %ormal parameters are substituted as A %or a and $ %or b. since A is not eSual to $, a))b returns 9. there%ore the %unction returns 9 +hich in turn is returned b! the %unction DprocessD. oid main&( Y static int i)@M i%&<<i(Y main&(M print%&-Ld -,i(M Z Z Answer: 9999 Explanation: The ariable -I- is declared as static, hence memor! %or I +ill be allocated %or onl! once, as it encounters the statement. The %unction main&( +ill be called recursi el! unless I becomes eSual to 9, and since main&( is recursi el! called, so the alue o% static I ie., 9 +ill be printed e er! time the control is returned. :O( oid main&(
Freshersworld.com Resource Center

:$(

A8

First Job. Dream Job. Freshersworld.com Y int 7)ret&si'eo%&%loat((M print%&-Pn here alue is Ld-,++7(M Z int ret&int ret( Y ret +) *.@M return&ret(M Z Answer: 3ere alue is O Explanation: The int ret&int ret(, ie., the %unction name and the argument name can be the same. 6irstl!, the %unction ret&( is called in +hich the si'eo%&%loat( ie., 8 is passed, a%ter the %irst e"pression the alue in ret +ill be $, as ret is integer hence the alue stored in ret +ill ha e implicit t!pe con ersion %rom %loat to int. The ret is returned in main&( it is printed a%ter and preincrement. :#( Y char a/0)-4*A8@P9-M int i)strlen&a(M print%&-here in A LdPn-,++i(M Z Answer: here in A $ Explanation: The char arra! DaD +ill hold the initiali'ed string, +hose length +ill be counted %rom 9 till the null character. 3ence the DID +ill hold the alue eSual to @, a%ter the pre<increment in the print% statement, the $ +ill be printed. ::( Y unsigned gi eit)<4M int gotitM print%&-Lu -,++gi eit(M print%&-Lu Pn-,gotit)<<gi eit(M Z Answer: 9 $@@A@ Explanation: 499( Y int iM
Freshersworld.com Resource Center

oid main&(

oid main&(

oid main&(

A@

First Job. Dream Job. Freshersworld.com char a/0)-P9-M i%&print%&-LsPn-,a(( print%&-O7 here Pn-(M else print%&-6orget itPn-(M Z Answer: O7 here Explanation: Print% +ill return ho+ man! characters does it print. 3ence printing a null character returns 4 +hich ma7es the i% statement true, thus -O7 here- is printed. 494( Y oid 1 M int integer)*M int 1i)GintegerM )iM print%&-Ld-,&int1(1 (M Z Answer: Compiler Trror. Be cannot appl! indirection on t!pe oid1. Explanation: Roid pointer is a generic pointer t!pe. ;o pointer arithmetic can be done on it. Roid pointers are normall! used %or, 4. Passing generic pointers to %unctions and returning such pointers. *. As a intermediate pointer t!pe. A. 5sed +hen the e"act pointer t!pe +ill be 7no+n at a later point o% time. 49*( Y int i)i++,V)V++,7)7++M print%&WLdLdLdX,i,V,7(M Z Answer: 2arbage alues. Explanation: Gn id%ntifi%r is avai$a'$% to &s% in pro=ram cod% from th% point of its d%c$aration. So e"pressions such as i ) i++ are alid statements. The i, V and 7 are automatic ariables and so the! contain some garbage alue. Har'a=% in is =ar'a=% o&t (H)HI). oid main&( oid main&(

Freshersworld.com Resource Center

A$

First Job. Dream Job. Freshersworld.com

49A( Y

oid main&( static int i)i++, V)V++, 7)7++M print%&Wi ) Ld V ) Ld 7 ) LdX, i, V, 7(M Z Answer: i)4V)47)4 Explanation: Since static ariables are initiali'ed to 'ero b! de%ault.

498( Y

oid main&( +hile&4(Y i%&print%&-Ld-,print%&-Ld-((( brea7M else continueM Z Z Answer: 2arbage alues Explanation: The inner print% e"ecutes %irst to print some garbage alue. The print% returns no o% characters printed and this alue also cannot be predicted. Still the outer print% prints something and so returns a non<'ero alue. So it encounters the brea7 statement and comes out o% the +hile statement.

498(

main&( Y unsigned int i)49M +hile&i<<=)9( print%&-Lu -,i(M Z Answer: 49 : # O $ @ 8 A * 4 9 $@@A@ $@@A8_.. Explanation: Since i is an unsigned integer it can ne er become negati e. So the e"pression i<< =)9 +ill al+a!s be true, leading to an in%inite loop.

49@(

Ninclude>conio.h= main&( Y int ",!)*,',aM


Freshersworld.com Resource Center

AO

First Job. Dream Job. Freshersworld.com i%&")!L*( ')*M a)*M print%&-Ld Ld -,',"(M Z Answer: 2arbage< alue 9 Explanation: The alue o% !L* is 9. This alue is assigned to ". The condition reduces to i% &"( or in other +ords i%&9( and so ' goes uninitiali'ed. Thumb Rule: (hec7 all control paths to +rite bug %ree code. 49$( main&( Y int a/490M print%&-Ld-,1a+4<1a+A(M Z Answer: 8 Explanation: 1a and <1a cancels out. The result is as simple as 4 + A ) 8 ? 49O( Nde%ine prod&a,b( a1b main&( Y int ")A,!)8M print%&-Ld-,prod&"+*,!<4((M Z Answer: 49 Explanation: The macro e"pands and e aluates to as, "+*1!<4 )= "+&*1!(<4 )= 49 main&( Y unsigned int i)$@999M +hile&i++?)9(M print%&-Ld-,i(M Z Answer: 4 Explanation: ;ote the semicolon a%ter the +hile statement. Bhen the alue o% i becomes 9 it comes out o% +hile loop. Due to post<increment on i the alue o% i +hile printing is 4.

49#(

Freshersworld.com Resource Center

A#

First Job. Dream Job. Freshersworld.com 49:( main&( Y int i)9M +hile&+&+i<<(?)9( i<)i++M print%&-Ld-,i(M Z Answer: <4 Explanation: *nar( + is th% on$( d&mm( op%rator in @ . So it has no e%%ect on the e"pression and no+ the +hile loop is, +hile&i<<?)9( +hich is %alse and so brea7s out o% +hile loop. The alue Q4 is printed due to the post<decrement operator. 44A( main&( Y %loat %)@,g)49M enumYi)49,V)*9,7)@9ZM print%&-LdPn-,++7(M print%&-L%Pn-,%>>*(M print%&-Ll%Pn-,%Lg(M print%&-Ll%Pn-,%mod&%,g((M Z Answer: Fine no @, Trror, F alue reSuired Fine no $, Cannot appl! le%tshi%t to %loat Fine no O, Cannot appl! mod to %loat Explanation: Tnumeration constants cannot be modi%ied, so !ou cannot appl! ++. .it<+ise operators and L operators cannot be applied on %loat alues. %mod&( is to %ind the modulus alues %or %loats as L operator is %or ints. 449( main&( Y int i)49M oid pascal %&int,int,int(M %&i++,i++,i++(M print%&- Ld-,i(M Z oid pascal %&integer ,i,integer,V,integer ,7( Y +rite&i,V,7(M Z Answer: Compiler error, un7no+n t!pe integer
Freshersworld.com Resource Center

A:

First Job. Dream Job. Freshersworld.com Compiler error, undeclared %unction +rite Explanation: Pascal 7e!+ord doesnKt mean that pascal code can be used. It means that the %unction %ollo+s Pascal argument passing mechanism in calling the %unctions. 444( Y print%&WLd Ld LdX,i, V, 7(M Z oid cdecl %&int i,int V,int 7( Y print%&WLd Ld LdX,i, V, 7(M Z main&( Y int i)49M %&i++,i++,i++(M print%&- LdPn-,i(M i)49M %&i++,i++,i++(M print%&- Ld-,i(M Z Answer: 49 44 4* 4A 4* 44 49 4A Explanation: Pascal argument passing mechanism %orces the arguments to be called %rom le%t to right. cdecl is the normal C argument passing mechanism +here the arguments are passed %rom right to le%t. 44*(. Bhat is the output o% the program gi en belo+ main&( Y signed char i)9M %or&Mi=)9Mi++( M print%&-LdPn-,i(M Z Answer <4*# Explanation ;otice the semicolon at the end o% the %or loop. T3e initial alue o% the i is set to 9. The inner loop e"ecutes to increment the alue %rom 9 to 4*O &the positi e range o% char( and then it rotates to the negati e alue o% <4*#. The condition in the %or
Freshersworld.com Resource Center

oid pascal %&int i,int V,int 7(

89

First Job. Dream Job. Freshersworld.com loop %ails and so comes out o% the %or loop. It prints the current alue o% i that is <4*#. 44A( main&( Y unsigned char i)9M %or&Mi=)9Mi++( M print%&-LdPn-,i(M Z Answer in%inite loop Explanation The di%%erence bet+een the pre ious Suestion and this one is that the char is declared to be unsigned. So the i++ can ne er !ield negati e alue and i=)9 ne er becomes %alse so that it can come out o% the %or loop. 448( main&( Y char i)9M %or&Mi=)9Mi++( M print%&-LdPn-,i(M Z Answer: .eha ior is implementation dependent. Explanation: The detail i% the char is signed/unsigned b! de%ault is implementation dependent. I% the implementation treats the char to be signed b! de%ault the program +ill print Q4*# and terminate. On the other hand i% it considers char to be unsigned b! de%ault, it goes to in%inite loop. Rule: Uou can +rite programs that ha e implementation dependent beha ior. .ut dont +rite programs that depend on such beha ior. 44@( Is the %ollo+ing statement a declaration/de%inition. 6ind +hat does it mean\ int &1"(/490M Answer De%inition. " is a pointer to arra! o%&si'e 49( integers. Appl! cloc7<+ise rule to %ind the meaning o% this de%inition. 44$(. Bhat is the output %or the program gi en belo+
Freshersworld.com Resource Center

84

First Job. Dream Job. Freshersworld.com

t!pede% enum errorT!peY+arning, error, e"ception,ZerrorM main&( Y error g4M g4)4M print%&-Ld-,g4(M Z Answer Compiler error, Cultiple declaration %or error Explanation The name error is used in the t+o meanings. One means that it is a enumerator constant +ith alue 4. The another use is that it is a t!pe name &due to t!pede%( %or enum errorT!pe. 2i en a situation the compiler cannot distinguish the meaning o% error to 7no+ in +hat sense the error is used, error g4M g4)errorM // +hich error it re%ers in each case\ Bhen the compiler can distinguish bet+een usages then it +ill not issue error &in pure technical terms, names can onl! be o erloaded in di%%erent namespaces(. Note, the e"tra comma in the declaration, enum errorT!peY+arning, error, e"ception,Z is not an error. An e"tra comma is alid and is pro ided Vust %or programmerKs con enience. 44O( t!pede% struct errorYint +arning, error, e"ceptionMZerrorM main&( Y error g4M g4.error )4M print%&-Ld-,g4.error(M Z

Answer 4 Explanation The three usages o% name errors can be distinguishable b! the compiler at an! instance, so alid &the! are in di%%erent namespaces(. T!pede% struct errorYint +arning, error, e"ceptionMZerrorM This error can be used onl! b! preceding the error b! struct 7a!+ord as in, struct error someTrrorM t!pede% struct errorYint +arning, error, e"ceptionMZerrorM

Freshersworld.com Resource Center

8*

First Job. Dream Job. Freshersworld.com This can be used onl! a%ter . &dot( or <= &arro+( operator preceded b! the ariable name as in , g4.error )4M print%&-Ld-,g4.error(M t!pede% struct errorYint +arning, error, e"ceptionMZerrorM This can be used to de%ine ariables +ithout using the preceding struct 7e!+ord as in, error g4M Since the compiler can per%ectl! distinguish bet+een these three usages, it is per%ectl! legal and alid. #ote This code is gi en here to Vust e"plain the concept behind. In real programming donKt use such o erloading o% names. It reduces the readabilit! o% the code. Possible doesnKt mean that +e should use it? 44#( Ni%de% something int some)9M Nendi% main&( Y int thing ) 9M print%&-Ld LdPn-, some ,thing(M Z Answer: Compiler error , unde%ined s!mbol some Explanation: This is a er! simple e"ample %or conditional compilation. The name something is not alread! 7no+n to the compiler ma7ing the declaration int some ) 9M e%%ecti el! remo ed %rom the source code. 44:( Ni% something )) 9 int some)9M Nendi% main&( Y int thing ) 9M print%&-Ld LdPn-, some ,thing(M Z Answer
Freshersworld.com Resource Center

8A

First Job. Dream Job. Freshersworld.com 99 Explanation This code is to sho+ that preprocessor e"pressions are not the same as the ordinar! e"pressions. I% a name is not 7no+n the preprocessor treats it to be eSual to 'ero. 4*9(. Bhat is the output %or the %ollo+ing program main&( Y int arr*D/A0/A0M print%&-LdPn-, &&arr*D))1 arr*D(GG&1 arr*D )) arr*D/90(( (M Z Answer 4 Explanation This is due to the close relation bet+een the arra!s and pointers. ; dimensional arra!s are made up o% &;<4( dimensional arra!s. arr*D is made up o% a A single arra!s that contains A integers each . arr*D arr*D/40 arr*D/*0 arr*D/A0

The name arr*D re%ers to the beginning o% all the A arra!s. 1arr*D re%ers to the start o% the %irst 4D arra! &o% A integers( that is the same address as arr*D. So the e"pression &arr*D )) 1arr*D( is true &4(. Similarl!, 1arr*D is nothing but 1&arr*D + 9(, adding a 'ero doesnKt change the alue/meaning. Again arr*D/90 is the another +a! o% telling 1&arr*D + 9(. So the e"pression &1&arr*D + 9( )) arr*D/90( is true &4(. Since both parts o% the e"pression e aluates to true the result is true&4( and the same is printed. 4*4( oid main&( Y i%&`9 )) &unsigned int(<4( print%&WUou can ans+er this i% !ou 7no+ ho+ alues are represented in memor!X(M Z
Freshersworld.com Resource Center

88

First Job. Dream Job. Freshersworld.com Ans+er Uou can ans+er this i% !ou 7no+ ho+ alues are represented in memor! T"planation ` &tilde operator or bit<+ise negation operator( operates on 9 to produce all ones to %ill the space %or an integer. Q4 is represented in unsigned alue as all 4Ks and so both are eSual. 4**( int s+ap&int 1a,int 1b( Y 1a)1a+1bM1b)1a<1bM1a)1a<1bM Z main&( Y int ")49,!)*9M s+ap&G",G!(M print%&-") Ld ! ) LdPn-,",!(M Z Ans+er " ) *9 ! ) 49 T"planation This is one +a! o% s+apping t+o alues. Simple chec7ing +ill help understand this. 4*A( main&( Y char 1p ) Wa!SmXM print%&WLcX,++1&p++((M Z Ans+er, b main&( Y int i)@M print%&-Ld-,++i++(M Z Answer: Compiler error, F alue reSuired in %unction main Explanation: ++i !ields an r alue. 6or post%i" ++ to operate an l alue is reSuired. 4*@( main&( Y char 1p ) Wa!SmXM
Freshersworld.com Resource Center

4*8(

8@

First Job. Dream Job. Freshersworld.com char cM c ) ++1p++M print%&WLcX,c(M Z Answer: b Explanation: There is no di%%erence bet+een the e"pression ++1&p++( and + +1p++. Parenthesis Vust +or7s as a isual clue %or the reader to see +hich e"pression is %irst e aluated. 4*$( int aaa&( Yprint%&W3iX(MZ int bbb&(Yprint%&WhelloX(MZ in! ccc&(Yprint%&Wb!eX(MZ main&( Y int & 1 ptr/A0( &(M ptr/90 ) aaaM ptr/40 ) bbbM ptr/*0 )cccM ptr/*0&(M Z Ans+er, b!e T"planation, int &1 ptr/A0(&( sa!s that ptr is an arra! o% pointers to %unctions that ta7es no arguments and returns the t!pe int. .! the assignment ptr/90 ) aaaM it means that the %irst %unction pointer in the arra! is initiali'ed +ith the address o% the %unction aaa. Similarl!, the other t+o arra! elements also get initiali'ed +ith the addresses o% the %unctions bbb and ccc. Since ptr/*0 contains the address o% the %unction ccc, the call to the %unction ptr/*0&( is same as calling ccc&(. So it results in printing -b!e-. 4*O( main&( Y int i)@M print%&WLdX,i)++i ))$(M Z Answer: 1 Explanation:
Freshersworld.com Resource Center

8$

First Job. Dream Job. Freshersworld.com The e"pression can be treated as i ) &++i))$(, because )) is o% higher precedence than ) operator. In the inner e"pression, ++i is eSual to $ !ielding true&4(. 3ence the result. 4*#( main&( Y char p/ 0)-LdPn-M p/40 ) DcDM print%&p,$@(M Z Answer: A Explanation: Due to the assignment p/40 ) JcK the string becomes, WLcPnX. Since this string becomes the %ormat string %or print% and ASCII alue o% $@ is JAK, the same gets printed. 4*:( oid & 1 abc& int, oid & 1de%( &( ( ( &(M Ans+er,, abc is a ptr to a %unction +hich ta7es * parameters .&a(. an integer ariable.&b(. a ptrto a %untion +hich returns oid. the return t!pe o% the %unction is oid. Explanation: Appl! the cloc7<+ise rule to %ind the result. 4A9( main&( Y +hile &strcmp&WsomeX,XsomeP9X(( print%&WStrings are not eSualPnX(M Z Answer: ;o output Explanation: Tnding the string constant +ith P9 e"plicitl! ma7es no di%%erence. So WsomeX and WsomeP9X are eSui alent. So, strcmp returns 9 &%alse( hence brea7ing out o% the +hile loop. main&( Y char str4/0 ) YJsK,KoK,KmK,KeKZM char str*/0 ) YJsK,KoK,KmK,KeK,KP9KZM +hile &strcmp&str4,str*(( print%&WStrings are not eSualPnX(M Z
Freshersworld.com Resource Center

4A4(

8O

First Job. Dream Job. Freshersworld.com Answer: WStrings are not eSualX WStrings are not eSualX _. Explanation: I% a string constant is initiali'ed e"plicitl! +ith characters, JP9K is not appended automaticall! to the string. Since str4 doesnKt ha e null termination, it treats +hate er the alues that are in the %ollo+ing positions as part o% the string until it randoml! reaches a JP9K. So str4 and str* are not the same, hence the result. 4A*( main&( Y int i ) AM %or &Mi++)9M( print%&WLdX,i(M Z Answer: Compiler Trror, F alue reSuired. Explanation: As +e 7no+ that increment operators return r alues and hence it cannot appear on the le%t hand side o% an assignment operation. 4AA( Y int 1mptr, 1cptrM mptr ) &int1(malloc&si'eo%&int((M print%&WLdX,1mptr(M int 1cptr ) &int1(calloc&si'eo%&int(,4(M print%&WLdX,1cptr(M Z Answer: garbage< alue 9 Explanation: The memor! space allocated b! malloc is uninitiali'ed, +hereas calloc returns the allocated memor! space initiali'ed to 'eros. 4A8( Y static int iM +hile&i>)49( &i=*(\i++,i<<M print%&WLdX, i(M Z Answer:
Freshersworld.com Resource Center

oid main&(

oid main&(

8#

First Job. Dream Job. Freshersworld.com A*O$O Explanation: Since i is static it is initiali'ed to 9. Inside the +hile loop the conditional operator e aluates to %alse, e"ecuting i<<. This continues till the integer alue rotates to positi e alue &A*O$O(. The +hile condition becomes %alse and hence, comes out o% the +hile loop, printing the i alue. 4A@( main&( Y int i)49,V)*9M V ) i, V\&i,V(\i,V,VM print%&-Ld Ld-,i,V(M Z Answer: 49 49 Explanation: The Ternar! operator & \ , ( is eSui alent %or i%<then<else statement. So the Suestion can be +ritten as, i%&i,V( Y i%&i,V( V ) iM else V ) VM Z else V ) VM 4A$( 4. const char 1aM *. char1 const aM A. char const 1aM <Di%%erentiate the abo e declarations. Answer: 4. DconstD applies to char 1 rather than DaD & pointer to a constant char ( 1a)D6D , illegal a)-3i, legal *. DconstD applies to DaD rather than to the alue o% a &constant pointer to char ( 1a)D6D a)-3i, legal , illegal

Freshersworld.com Resource Center

8:

First Job. Dream Job. Freshersworld.com A. Same as 4. 4AO( main&( Y int i)@,V)49M i)iG)VGG49M print%&-Ld Ld-,i,V(M Z Answer: 4 49 Explanation: The e"pression can be +ritten as i)&iG)&VGG49((M The inner e"pression &VGG49( e aluates to 4 because V))49. i is @. i ) @G4 is 4. 3ence the result. 4A#( main&( Y int i)8,V)OM V ) V II i++ GG print%&-UO5 CA;-(M print%&-Ld Ld-, i, V(M Z Answer: 84 Explanation: Jh% 'oo$%an %3pr%ssion n%%ds to '% %va$&at%d on$( ti$$ th% tr&th va$&% of th% %3pr%ssion is not 5no;n. V is not eSual to 'ero itsel% means that the e"pressionKs truth alue is 4. .ecause it is %ollo+ed b! II and tr&% 88 (an(thin=) =C tr&% ;h%r% (an(thin=) ;i$$ not '% %va$&at%d. So the remaining e"pression is not e aluated and so the alue o% i remains the same. Similarl! +hen GG operator is in ol ed in an e"pression, +hen an! o% the operands become %alse, the +hole e"pressionKs truth alue becomes %alse and hence the remaining e"pression +ill not be e aluated. fa$s% 77 (an(thin=) =C fa$s% ;h%r% (an(thin=) ;i$$ not '% %va$&at%d. 4A:( main&( Y register int a)*M print%&-Address o% a ) Ld-,Ga(M print%&-Ralue o% a ) Ld-,a(M Z Answer: Compier Trror, DGD on register ariable
Freshersworld.com Resource Center

@9

First Job. Dream Job. Freshersworld.com Rule to Remember: & (address of ) operator cannot be applied on register variables 489( main&( Y %loat i)4.@M s+itch&i( Y case 4, print%&-4-(M case *, print%&-*-(M de%ault , print%&-9-(M Z Z Answer: Compiler Trror, s+itch e"pression not integral Explanation: K;itch stat%m%nts can '% app$i%d on$( to int%=ra$ t(p%s. 484( main&( Y e"tern iM print%&-LdPn-,i(M Y int i)*9M print%&-LdPn-,i(M Z Z Answer: Fin7er Trror , 5nresol ed e"ternal s!mbol i Explanation: The identi%ier i is a ailable in the inner bloc7 and so using e"tern has no use in resol ing it. 48*( main&( Y int a)*,1%4,1%*M %4)%*)GaM 1%*+)1%*+)a+)*.@M print%&-PnLd Ld Ld-,a,1%4,1%*(M Z Answer: 4$ 4$ 4$ Explanation: %4 and %* both re%er to the same memor! location a. So changes through %4 and %* ultimatel! a%%ects onl! the alue o% a.

Freshersworld.com Resource Center

@4

First Job. Dream Job. Freshersworld.com 48A( main&( Y char 1p)-2OOD-M char a/ 0)-2OOD-M print%&-Pn si'eo%&p( ) Ld, si'eo%&1p( ) Ld, strlen&p( ) Ld-, si'eo%&p(, si'eo%&1p(, strlen&p((M print%&-Pn si'eo%&a( ) Ld, strlen&a( ) Ld-, si'eo%&a(, strlen&a((M Z Answer: si'eo%&p( ) *, si'eo%&1p( ) 4, strlen&p( ) 8 si'eo%&a( ) @, strlen&a( ) 8 Explanation: si'eo%&p( )= si'eo%&char1( )= * si'eo%&1p( )= si'eo%&char( )= 4 Similarl!, si'eo%&a( )= si'e o% the character arra! )= @ ?h%n si9%of op%rator is app$i%d to an arra( it r%t&rns th% si9%of th% arra( and it is not the same as the si'eo% the pointer ariable. 3ere the si'eo%&a( +here a is the character arra! and the si'e o% the arra! is @ because the space necessar! %or the terminating ;5FF character should also be ta7en into account. 488( Nde%ine DIC& arra!, t!pe( si'eo%&arra!(/si'eo%&t!pe( main&( Y int arr/490M print%&WThe dimension o% the arra! is LdX, DIC&arr, int((M Z Answer: 49 Explanation: The si'e o% integer arra! o% 49 elements is 49 1 si'eo%&int(. The macro e"pands to si'eo%&arr(/si'eo%&int( )= 49 1 si'eo%&int( / si'eo%&int( )= 49. int DIC&int arra!/0( Y return si'eo%&arra!(/si'eo%&int (M Z main&( Y int arr/490M print%&WThe dimension o% the arra! is LdX, DIC&arr((M Z Answer: 4
Freshersworld.com Resource Center

48@(

@*

First Job. Dream Job. Freshersworld.com Explanation: Grra(s cannot '% pass%d to f&nctions as ar=&m%nts and on$( th% point%rs can '% pass%d. So the argument is eSui alent to int 1 arra! &this is one o% the er! %e+ places +here /0 and 1 usage are eSui alent(. The return statement becomes, si'eo%&int 1(/ si'eo%&int( that happens to be eSual in this case. 48$( main&( Y static int a/A0/A0)Y4,*,A,8,@,$,O,#,:ZM int i,VM static 1p/0)Ya,a+4,a+*ZM %or&i)9Mi>AMi++( Y %or&V)9MV>AMV++( print%&-LdPtLdPtLdPtLdPn-,1&1&p+i(+V(, 1&1&V+p(+i(,1&1&i+p(+V(,1&1&p+V(+i((M Z Z Answer: 4 * A 8 @ $ O # : 4 8 O * @ # A $ : 4 * A 8 @ $ O # : 4 8 O * @ # A $ :

Explanation: 1&1&p+i(+V( is eSui alent to p/i0/V0. 48O( main&( Y oid s+ap&(M int ")49,!)#M s+ap&G",G!(M print%&-")Ld !)Ld-,",!(M Z oid s+ap&int 1a, int 1b( Y 1a ]) 1b, 1b ]) 1a, 1a ]) 1bM Z Answer: ")49 !)# Explanation:
Freshersworld.com Resource Center

@A

First Job. Dream Job. Freshersworld.com 5sing ] li7e this is a +a! to s+ap t+o ariables +ithout using a temporar! ariable and that too in a single statement. Inside main&(, oid s+ap&(M means that s+ap is a %unction that ma! ta7e an! number o% arguments &not no arguments( and returns nothing. So this doesnKt issue a compiler error b! the call s+ap&G",G!(M that has t+o arguments. This con ention is historicall! due to pre<A;SI st!le &re%erred to as [ernighan and Hitchie st!le( st!le o% %unction declaration. In that st!le, the s+ap %unction +ill be de%ined as %ollo+s, oid s+ap&( int 1a, int 1b Y 1a ]) 1b, 1b ]) 1a, 1a ]) 1bM Z +here the arguments %ollo+ the &(. So naturall! the declaration %or s+ap +ill loo7 li7e, oid s+ap&( +hich means the s+ap can ta7e an! number o% arguments. 48#( main&( Y int i ) *@OM int 1iPtr ) GiM print%&-Ld Ld-, 1&&char1(iPtr(, 1&&char1(iPtr+4( (M Z Answer: 44 Explanation: The integer alue *@O is stored in the memor! as, 99999994 99999994, so the indi idual b!tes are ta7en b! casting it to char 1 and get printed. 48:( main&( Y int i ) *@#M int 1iPtr ) GiM print%&-Ld Ld-, 1&&char1(iPtr(, 1&&char1(iPtr+4( (M Z Answer: *4 Explanation: The integer alue *@O can be represented in binar! as, 99999994 99999994. Hemember that the I;TTF machines are Jsmall<endianK machines. Kma$$,%ndian m%ans that th% $o;%r ord%r '(t%s ar% stor%d in th% hi=h%r m%mor( addr%ss%s and th% hi=h%r ord%r '(t%s ar% stor%d in $o;%r addr%ss%s. The integer alue *@# is stored in memor! as, 99999994 99999949.

Freshersworld.com Resource Center

@8

First Job. Dream Job. Freshersworld.com 4@9( main&( Y int i)A99M char 1ptr ) GiM 1++ptr)*M print%&-Ld-,i(M Z Answer: @@$ Explanation: The integer alue A99 in binar! notation is, 99999994 99494499. It is stored in memor! &small<endian( as, 99494499 99999994. Hesult o% the e"pression 1++ptr ) * ma7es the memor! representation as, 99494499 99999949. So the integer corresponding to it is 99999949 99494499 )= @@$. 4@4( Ninclude >stdio.h= main&( Y char 1 str ) -hello-M char 1 ptr ) strM char least ) 4*OM +hile &1ptr++( least ) &1ptr>least ( \1ptr ,leastM print%&-Ld-,least(M Z Answer: 9 Explanation: A%ter JptrK reaches the end o% the string the alue pointed b! JstrK is JP9K. So the alue o% JstrK is less than that o% JleastK. So the alue o% JleastK %inall! is 9. Declare an arra! o% ; pointers to %unctions returning pointers to %unctions returning pointers to characters\ Answer: &char1&1(& (( &1ptr/;0(& (M main&( Y struct student Y char name/A90M struct date dobM ZstudM struct date
Freshersworld.com Resource Center

4@*(

4@A(

@@

First Job. Dream Job. Freshersworld.com Y int da!,month,!earM ZM scan%&-LsLdLdLd-, stud.rollno, Gstudent.dob.month, Gstudent.dob.!ear(M

Gstudent.dob.da!,

Z Answer: Compiler Trror, 5nde%ined structure date Explanation: Inside the struct de%inition o% JstudentK the member o% t!pe struct date is gi en. The compiler doesnKt ha e the de%inition o% date structure &%or+ard re%erence is not allo+ed in C in this case( so it issues an error. 4@8( main&( Y struct dateM struct student Y char name/A90M struct date dobM ZstudM struct date Y int da!,month,!earM ZM scan%&-LsLdLdLd-, stud.rollno, Gstudent.dob.month, Gstudent.dob.!ear(M

Gstudent.dob.da!,

Z Answer: Compiler Trror, 5nde%ined structure date Explanation: Onl! declaration o% struct date is a ailable inside the structure de%inition o% JstudentK but to ha e a ariable o% t!pe struct date the de%inition o% the structure is reSuired. 4@@( There +ere 49 records stored in Wsome%ile.datX but the %ollo+ing program printed 44 names. Bhat +ent +rong\ oid main&( Y struct student Y char name/A90, rollno/$0M ZstudM 6IFT 1%p ) %open&Wsome%ile.datX,XrX(M +hile&?%eo%&%p((
Freshersworld.com Resource Center

@$

First Job. Dream Job. Freshersworld.com Y %read&Gstud, si'eo%&stud(, 4 , %p(M puts&stud.name(M Z Z Explanation: %read reads 49 records and prints the names success%ull!. It +ill return TO6 onl! +hen %read tries to read another record and %ails reading TO6 &and returning TO6(. So it prints the last record again. A%ter this onl! the condition %eo%&%p( becomes %alse, hence comes out o% the +hile loop. 4@$( Is there an! di%%erence bet+een the t+o declarations, 4. int %oo&int 1arr/0( and *. int %oo&int 1arr/*0( Answer: ;o Explanation: 6unctions can onl! pass pointers and not arra!s. The numbers that are allo+ed inside the /0 is Vust %or more readabilit!. So there is no di%%erence bet+een the t+o declarations. Bhat is the subtle error in the %ollo+ing code segment\ oid %un&int n, int arr/0( Y int 1p)9M int i)9M +hile&i++>n( p ) Garr/i0M 1p ) 9M Z Answer & Explanation: I% the bod! o% the loop ne er e"ecutes p is assigned no address. So p remains ;5FF +here 1p )9 ma! result in problem &ma! rise to runtime error W;5FF pointer assignmentX and terminate the program(. Bhat is +rong +ith the %ollo+ing code\ int 1%oo&( Y int 1s ) malloc&si'eo%&int(499(M assert&s ?) ;5FF(M return sM Z Answer & Explanation:
Freshersworld.com Resource Center

4@O(

4@#(

@O

First Job. Dream Job. Freshersworld.com assert macro should be used %or debugging and %inding out bugs. The chec7 s ?) ;5FF is %or error/e"ception handling and %or that assert shouldnKt be used. A plain i% and the corresponding remed! statement has to be gi en. 4@:( Bhat is the hidden bug +ith the %ollo+ing statement\ assert& al++ ?) 9(M Answer & Explanation: Assert macro is used %or debugging and remo ed in release ersion. In assert, the e"perssion in ol es side<e%%ects. So the beha ior o% the code becomes di%%erent in case o% debug ersion and the release ersion thus leading to a subtle bug. Rule to Remember: LonMt &s% %3pr%ssions that hav% sid%,%ff%cts in ass%rt stat%m%nts. oid main&( Y int 1i ) 9"899M // i points to the address 899 1i ) 9M // set the alue o% memor! location pointed b! iM Z Answer: 5nde%ined beha ior Explanation: The second statement results in unde%ined beha ior because it points to some location +hose alue ma! not be a ailable %or modi%ication. Jhis t(p% of point%r in ;hich th% non,avai$a'i$it( of th% imp$%m%ntation of th% r%f%r%nc%d $ocation is 5no;n as Eincomp$%t% t(p%E. 4$4( Nde%ine assert&cond( i%&?&cond(( P &%print%&stderr, -assertion %ailed, Ls, %ile Ls, line Ld Pn-,Ncond,P EE6IFTEE,EEFI;TEE(, abort&(( oid main&( Y int i ) 49M i%&i))9( assert&i > 499(M else print%&-This statement becomes else %or i% in assert macro-(M Z Ans+er, ;o output Explanation: The else part in +hich the print% is there becomes the else %or i% in the assert macro. 3ence nothing is printed.
Freshersworld.com Resource Center

4$9(

@#

First Job. Dream Job. Freshersworld.com The solution is to use conditional operator instead o% i% statement, Nde%ine assert&cond( &&cond(\&9(, &%print% &stderr, -assertion %ailed, P Ls, %ile Ls, line Ld Pn-,Ncond, EE6IFTEE,EEFI;TEE(, abort&((( ;ote, 3o+e er this problem o% Wmatching +ith nearest elseX cannot be sol ed b! the usual method o% placing the i% statement inside a bloc7 li7e this, Nde%ine assert&cond( Y P i%&?&cond(( P &%print%&stderr, -assertion %ailed, Ls, %ile Ls, line Ld Pn-,Ncond,P EE6IFTEE,EEFI;TEE(, abort&(( P Z 4$*( Is the %ollo+ing code legal\ struct a Y int "M struct a bM Z Ans+er, ;o Explanation: Is it not legal %or a structure to contain a member that is o% the same t!pe as in this case. .ecause this +ill cause the structure declaration to be recursi e +ithout end. Is the %ollo+ing code legal\ struct a Y int "M struct a 1bM Z Answer: Ues. Explanation: 1b is a pointer to t!pe struct a and so is legal. The compiler 7no+s, the si'e o% the pointer to a structure e en be%ore the si'e o% the structure is determined&as !ou 7no+ the pointer to an! t!pe is o% same si'e(. This t!pe o% structures is 7no+n as Jsel%<re%erencingK structure. Is the %ollo+ing code legal\ t!pede% struct a Y int "M aT!pe 1bM
Freshersworld.com Resource Center

4$A(

4$8(

@:

First Job. Dream Job. Freshersworld.com ZaT!pe Answer: ;o Explanation: The t!pename aT!pe is not 7no+n at the point o% declaring the structure &%or+ard re%erences are not made %or t!pede%s(. 4$@( Is the %ollo+ing code legal\ t!pede% struct a aT!peM struct a Y int "M aT!pe 1bM ZM Answer: Ues Explanation: The t!pename aT!pe is 7no+n at the point o% declaring the structure, because it is alread! t!pede%ined. Is the %ollo+ing code legal\ oid main&( Y t!pede% struct a aT!peM aT!pe someRariableM struct a Y int "M aT!pe 1bM ZM Z Answer: ;o Explanation: Bhen the declaration, t!pede% struct a aT!peM is encountered bod! o% struct a is not 7no+n. This is 7no+n as Jincomplete t!pesK. oid main&( Y print%&Wsi'eo% & oid 1( ) Ld PnW, si'eo%& oid 1((M print%&Wsi'eo% &int 1( ) Ld PnX, si'eo%&int 1((M print%&Wsi'eo% &double 1( ) Ld PnX, si'eo%&double 1((M print%&Wsi'eo%&struct un7no+n 1( ) Ld PnX, si'eo%&struct un7no+n 1((M Z
Freshersworld.com Resource Center

4$$(

4$O(

$9

First Job. Dream Job. Freshersworld.com Answer : si'eo% & oid 1( ) * si'eo% &int 1( ) * si'eo% &double 1( ) * si'eo%&struct un7no+n 1( ) * Explanation: The pointer to an! t!pe is o% same si'e. 4$#( char inputString/4990 ) Y9ZM To get string input %rom the 7e!board +hich one o% the %ollo+ing is better\ 4( gets&inputString( *( %gets&inputString, si'eo%&inputString(, %p( Answer & Explanation: The second one is better because gets&inputString( doesnDt 7no+ the si'e o% the string passed and so, i% a er! big input &here, more than 499 chars( the charactes +ill be +ritten past the input string. Bhen %gets is used +ith stdin per%orms the same operation as gets but is sa%e. Bhich ersion do !ou pre%er o% the %ollo+ing t+o, 4( print%&WLsX,str(M // or the more curt one *( print%&str(M Answer & Explanation: Pre%er the %irst one. I% the str contains an! %ormat characters li7e Ld then it +ill result in a subtle bug. oid main&( Y int i)49, V)*M int 1ip) Gi, 1Vp ) GVM int 7 ) 1ip/1VpM print%&WLdX,7(M Z Answer: Compiler Trror, W5ne"pected end o% %ile in comment started in line @X. Explanation: The programmer intended to di ide t+o integers, but b! the Wma"imum munchX rule, the compiler treats the operator seSuence / and 1 as /1 +hich happens to be the starting o% comment. To %orce +hat is intended b! the programmer, int 7 ) 1ip/ 1VpM // gi e space e"plicit! separating / and 1 //or int 7 ) 1ip/&1Vp(M // put braces to %orce the intention +ill sol e the problem.

4$:(

4O9(

Freshersworld.com Resource Center

$4

First Job. Dream Job. Freshersworld.com 4O4( oid main&( Y char chM %or&ch)9Mch>)4*OMch++( print%&WLc Ld PnW, ch, ch(M Z Answer: Implementaion dependent Explanation: The char t!pe ma! be signed or unsigned b! de%ault. I% it is signed then ch++ is e"ecuted a%ter ch reaches 4*O and rotates bac7 to <4*#. Thus ch is al+a!s smaller than 4*O. 4O*( Is this code legal\ int 1ptrM ptr ) &int 1( 9"899M Answer: Ues Explanation: The pointer ptr +ill point at the integer in the memor! location 9"899. main&( Y char a/80)-3TFFO-M print%&-Ls-,a(M Z Answer: Compiler error, Too man! initiali'ers Explanation: The arra! a is o% si'e 8 but the string constant reSuires $ b!tes to get stored. 4O8( main&( Y char a/80)-3TFF-M print%&-Ls-,a(M Z Answer: 3TFFLa?`a?a\\\a``? Explanation: The character arra! has the memor! Vust enough to hold the string W3TFFX and doesnt ha e enough space to store the terminating null character. So it prints the 3TFF correctl! and continues to print garbage alues till it accidentall! comes across a ;5FF character. 4O@( main&(
Freshersworld.com Resource Center

4OA(

$*

First Job. Dream Job. Freshersworld.com Y int a)49,1VM oid 17M V)7)GaM V++M 7++M print%&-Pn Lu Lu -,V,7(M Z Answer: Compiler error, Cannot increment a oid pointer Explanation: Roid pointers are generic pointers and the! can be used onl! +hen the t!pe is not 7no+n and as an intermediate address storage t!pe. ;o pointer arithmetic can be done on it and !ou cannot appl! indirection operator &1( on oid pointers. 4O$( main&( Y Y Y Z print%&-Ld-,i(M Z print%&-Ld-,i(M Z int iM 4OO( Print% can be implemented b! using EEEEEEEEEE list. Answer: Rariable length argument lists 4O#( char 1some6un&( Y char 1temp ) Wstring constant-M return tempM Z int main&( Y puts&some6un&((M Z Ans+er, string constant Explanation, e"tern int iM int i)*9M const olatile unsigned i)A9M print%&-Ld-,i(M

Freshersworld.com Resource Center

$A

First Job. Dream Job. Freshersworld.com The program su%%ers no problem and gi es the output correctl! because the character constants are stored in code/data area and not allocated in stac7, so this doesnKt lead to dangling pointers. 4O:( char 1some6un4&( Y char temp/ 0 ) Wstring-M return tempM Z char 1some6un*&( Y char temp/ 0 ) YJsK, JtK,KrK,KiK,KnK,KgKZM return tempM Z int main&( Y puts&some6un4&((M puts&some6un*&((M Z Ans+er, 2arbage alues. Explanation, .oth the %unctions su%%er %rom the problem o% dangling pointers. In some6un4&( temp is a character arra! and so the space %or it is allocated in heap and is initiali'ed +ith character string WstringX. This is created d!namicall! as the %unction is called, so is also deleted d!namicall! on e"iting the %unction so the string data is not a ailable in the calling %unction main&( leading to print some garbage alues. The %unction some6un*&( also su%%ers %rom the same problem but the problem can be easil! identi%ied in this case.

Freshersworld.com Resource Center

$8

Potrebbero piacerti anche