[go: up one dir, main page]

0% found this document useful (0 votes)
184 views65 pages

175 C Ques

The document provides examples of C code snippets and questions about their output. It contains 22 examples of short code snippets with explanations of the predicted output or errors. The examples cover topics like pointers, arrays, structures, macros, operators and their precedence.

Uploaded by

akhshayam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views65 pages

175 C Ques

The document provides examples of C code snippets and questions about their output. It contains 22 examples of short code snippets with explanations of the predicted output or errors. The examples cover topics like pointers, arrays, structures, macros, operators and their precedence.

Uploaded by

akhshayam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

First Job. Dream Job. Freshersworld.

com

Continue from %# ???????? C Questions


Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that, rograms run under !"# en$ironment, The underl%ing machine is an &'( s%stem, rogram is compiled using Turbo C/C++ compiler. The program output ma% depend on the information based on this assumptions )for e&ample si*eof)int+ ,, - ma% be assumed+. redict the output or error)s+ for the follo.ing/ 1. void main() { int const * p=5; printf("%d",++(*p)); } Answer: Compiler error/ Cannot modif% a constant $alue. Explanation/ p is a pointer to a 0constant integer0. 1ut .e tried to change the $alue of the 0constant integer0. 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/ s2i3, 4)i+s+, 4)s+i+, i2s3 are all different .a%s of e&pressing the same idea. 5enerall% arra% name is the base address for that arra%. 6ere s is the base address. i is the inde& number/displacement from the base address. #o, indirecting it .ith 4 is same as s2i3. i2s3 ma% be surprising. 1ut in the case of C it is same as s2i3. #. main()
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com { f$oat m% = 1.1; do&'$% (o& = 1.1; if(m%==(o&) printf(") $ov% *"); %$s% printf(") hat% *"); } Answer: I hate 8 Explanation/ 9or floating point numbers (float, double, long double) the $alues cannot be predicted e&actl%. !epending on the number of b%tes, the precession .ith of the $alue represented $aries. 9loat ta:es ; b%tes and long double ta:es 7< b%tes. #o float stores <.= .ith less precision than long double. Rule of Thumb: >e$er compare or at?least be cautious .hen using floating point numbers .ith relational operators (,, , @, A, A,, @,,B, ) . +. main() { static int var = 5; printf("%d ",var,,); if(var) main(); } Answer: C;D-7 Explanation: Ehen static storage class is gi$en, it is initiali*ed once. The change in the $alue of a static $ariable is retained e$en bet.een the function calls. Fain is also treated li:e an% other ordinar% function, .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; }
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com } Answer: ------D;(C Explanation: Initiall% pointer c is assigned to both p and q. In the first loop, since onl% q is incremented and not c , the $alue - .ill be printed C times. In second loop p itself is incremented. #o the $alues - D ; ( C .ill be printed. .. main() { %3t%rn int i; i=2!; printf("%d",i); } Answer: 4in5%r 6rror / 8ndefined s%mbol GHiG Explanation: e&tern storage class in the follo.ing declaration, extern int i; specifies to the compiler that the memor% for i is allocated in some other program and that address .ill be gi$en to the current program at the time of lin:ing. 1ut lin:er finds that no other $ariable of name i is a$ailable in an% other program .ith memor% space allocated for it. 6ence a lin:er error has occured . /. 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: <<7D7 Explanation : Iogical operations al.a%s gi$e a result of 1 or 0 . And also the logical A>! )JJ+ operator has higher priorit% o$er the logical "K )LL+ operator. #o the e&pression Mi++ && j++ && k++ is e&ecuted first. The result of this e&pression is < )?7 JJ ?7 JJ < , <+. >o. the e&pression is < LL - .hich e$aluates to 7 )because "K operator al.a%s gi$es 7 e&cept for M< LL <N combination? for .hich it gi$es <+. #o the $alue of m is 7. The $alues of other $ariables are also incremented b% 7.

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

-. main() { char *p; printf("%d %d ",si9%of(*p),si9%of(p)); }

Answer: 79or int ??? -,9or 9loat ???;,Explanation: The si*eof)+ operator gi$es the number of b%tes ta:en b% its operand. is a character pointer, .hich needs one b%te for storing its $alue )a character+. 6ence si*eof)4p+ gi$es a $alue of 7. #ince it needs t.o b%tes to store the address of the character pointer si*eof)p+ gi$es -. :. 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 default case can be placed an%.here inside the loop. It is e&ecuted onl% .hen all other cases doesnGt match.

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

44444444444444
1!. main() { printf("%3",,122+); } Answer: fff< Explanation : ?7 is internall% represented as all 7Gs. Ehen left shifted four times the least significant ; bits are filled .ith <Gs.The %& format specifier specifies 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 of function displa% Explanation : In third line, .hen the function displ ! is encountered, the compiler doesnGt :no. an%thing about the function displa%. It assumes the arguments and return t%pes to be integers, ).hich is the default t%pe+. Ehen it sees the actual function displ !" the arguments and t%pe contradicts .ith .hat it has assumed pre$iousl%. 6ence a compile time error occurs.

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

#olution/ $oid main)+ O char string23,06ello Eorld0P displa%)Jstring+P printf)0%s0,string+P Q displa%)string+ O char #tr727-3P strcp%)#tr7,string+P getch)+P Q

44444444
12. main() { int c=, ,2; printf("c=%d",c); } Answer: c,-P Explanation: 6ere unar% minus )or negation+ operator is used t.ice. #ame maths rules applies, ie. minus 4 minus, plus. #ote: 6o.e$er %ou cannot gi$e li:e ??-. 1ecause ?? operator can onl% be applied to $ariables as a de$rement operator )eg., i??+. - is a constant and not a $ariable.

444444444
1#. Ad%fin% int char main() { int i=.5; printf("si9%of(i)=%d",si9%of(i)); } Answer: si*eof)i+,7
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com Explanation: #ince the #define replaces the string int b% the macro $h r 1+. main() { int i=1!; i=BiC1+; Drintf ("i=%d",i); } Answer: i,< Explanation: In the e&pression %i&1' , >"T )B+ operator has more precedence than M @N s%mbol. % is a unar% logical operator. Bi )B7<+ is < )not of true is false+. <@7; is false )*ero+. HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 15. Ainc$&d%2stdio.hC 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: RR Explanation: p is pointing to character GSnG. str7 is pointing to character GaG ++4p. 0p is pointing to GSnG and that is incremented b% one.0 the A#CII $alue of GSnG is 7<, .hich is then incremented to 77. The $alue of ++4p is 77. ++4str7, str7 is pointing to GaG that is incremented b% 7 and it becomes GbG. A#CII $alue of GbG is ='. >o. performing )77 + =' T D-+, .e get RR)0F0+P #o .e get the output RR // 0F0 )Ascii is RR+.

1.. Ainc$&d%2stdio.hC
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com 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: #ome5arbageUalue???7< Explanation: p,Ja2-32-32-3 %ou declare onl% t.o -! arra%s, but %ou are tr%ing to access the third -!).hich %ou are not declared+ it .ill print garbage $alues. 4V,444a starting address of a is assigned integer pointer. >o. V is pointing to starting address of a. If %ou print 4V, it .ill print first element of D! arra%.

44444444444
1/. Ainc$&d%2stdio.hC main() { str&ct 33 { int 3=#; char nam%[ ="h%$$o"; }; str&ct 33 *s; printf("%d",s,C3); printf("%s",s,Cnam%); } Answer: Compiler Wrror Explanation: Xou should not initiali*e $ariables in declaration

*********************
1-. Ainc$&d%2stdio.hC main() { str&ct 33
Freshersworld.com Resource Center

'

First Job. Dream Job. Freshersworld.com { int 3; str&ct (( { char s; str&ct 33 *p; }; str&ct (( *1; }; } Answer: Compiler Wrror Explanation: The structure %% is nested .ithin structure &&. 6ence, the elements are of %% are to be accessed through the instance of structure &&, .hich needs an instance of %% to be :no.n. If the instance is created after defining the structure the compiler .ill not :no. about the instance relati$e to &&. 6ence for nested structure %% %ou ha$e to declare member.

44444444444
1:. main() { printf(""na'"); printf(""'si"); printf(""rha"); } Answer: hai Explanation: Sn ? ne.line Sb ? bac:space Sr ? linefeed 2!. main() { int i=5; printf("%d%d%d%d%d%d",i++,i,,,++i,,,i,i); } Answer: ;CC;C Explanation: The arguments in a function call are pushed into the stac: from left to right. The e$aluation is b% popping out from the stac:. and the e$aluation is from right to left, hence the result.
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

44444444444444
21. Ad%fin% s1&ar%(3) 3*3 main() { int i; i = .+Fs1&ar%(+); printf("%d",i); } Answer: (; Explanation: the macro call sVuare);+ .ill substituted b% ;4; so the e&pression becomes i , (;/;4; . #ince / and 4 has eVual priorit% the e&pression .ill be e$aluated as )(;/;+4; i.e. 7(4; , (; BBBBBBBBBB4444444444444 22. main() { char *p="hai fri%nds",*p1; p1=p; ;hi$%(*pB=E"!E) ++*p++; printf("%s %s",p,p1); } Answer: ibYBgsYfoet Explanation: ++4p++ .ill be parse in the gi$en order 4p that is $alue at the location currentl% pointed b% p .ill be ta:en ++4p the retrie$ed $alue .ill be incremented .hen P is encountered the location .ill be incremented that is p++ .ill be e&ecuted 6ence, in the .hile loop initial $alue pointed b% p is MhN, .hich is changed to MiN b% e&ecuting ++4p and pointer mo$es to point, MaN .hich is similarl% changed to MbN and so on. #imilarl% blan: space is con$erted to MBN. Thus, .e obtain $alue in p becomes ZibYBgsYfoet[ and since p reaches MS<N and p7 points to p thus p7doesnot print an%thing.

44444444444
2#. Ainc$&d% 2stdio.hC Ad%fin% a 1!
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com main() { Ad%fin% a 5! printf("%d",a); } Answer: C< Explanation: The preprocessor directi$es can be redefined an%.here in the program. #o the most recentl% assigned $alue .ill be ta:en. 2+. Ad%fin% c$rscr() 1!! main() { c$rscr(); printf("%d"n",c$rscr()); } Answer: 7<< Explanation: reprocessor e&ecutes as a seperate pass before the e&ecution of the compiler. #o te&tual replacement of clrscr)+ to 7<< occurs.The input program to compiler loo:s li:e this / main)+ O 7<<P printf)0%dSn0,7<<+P Q #ote: 7<<P is an e&ecutable statement but .ith no action. #o it doesnGt gi$e an% problem 25. main() { printf("%p",main); % } Answer: #ome address .ill be printed. Explanation: 9unction names are Yust addresses )Yust li:e arra% names are addresses+. main)+ is also a function. #o the address of function main .ill be printed. %p in printf specifies that the argument is an address. The% are printed as he&adecimal numbers. -R+ main)+ O
Freshersworld.com Resource Center

p >%3ad%cima$ format

First Job. Dream Job. Freshersworld.com clrscr)+P Q clrscr)+P Answer: >o output/error Explanation: The first clrscr)+ occurs inside a function. #o it becomes a function call. In the second clrscr)+P is a function declaration )because it is not inside an% function+. -'+ enum colors O1IAC\,1I8W,5KWW>Q main)+ // !onNt !eclare as $oid main)+ it fires error O printf)0%d..%d..%d0,1IAC\,1I8W,5KWW>+P return)7+P Q Answer: <..7..Explanation: enum assigns numbers starting from <, if not e&plicitl% defined.

44444444
-=+ $oid main)+ O char far 4farther,4farthestP //far t%pe modifier, similar to that near, printf)0%d..%d0,si*eof)farther+,si*eof)farthest++P // huge r t%pe modifiers Q Answer: ;..Explanation: the second pointer is of char t%pe and not a far pointer main)+ O int i,;<<,Y,D<<P printf)0%d..%d0+P Q Answer: ;<<..D<< Explanation:
Freshersworld.com Resource Center

D<+

First Job. Dream Job. Freshersworld.com printf ta:es the $alues of the first t.o assignments of the program. An% number of printfGs ma% be gi$en. All of them ta:e onl% the first t.o $alues. If more number of assignments gi$en in the program,then printf .ill ta:e garbage $alues. 44444444 D7+ main)+ O char 4pP p,06ello0P printf)0%cSn0,4J4p+P Q Answer: 6 Explanation: 4 is a dereference operator J is a reference operator. The% can be applied an% number of times pro$ided it is meaningful. 6ere p points to the first character in the string 06ello0. 4p dereferences it and so its $alue is 6. Again J references it to an address and 4 dereferences it to the $alue 6. D-+ main)+ O int i,7P .hile )iA,C+ O printf)0%d0,i+P if )i@-+ goto hereP i++P Q Q fun)+ O here/ printf)0 0+P Q Answer: Compiler error/ 8ndefined label GhereG in function main Explanation: Iabels ha$e functions scope, in other .ords The scope of the labels is limited to functions . The label GhereG is a$ailable in function fun)+ 6ence it is not $isible in function main. main)+ O static char names2C32-<3,O0pascal0,0ada0,0cobol0,0fortran0,0perl0QP
Freshersworld.com Resource Center

DD+

First Job. Dream Job. Freshersworld.com int iP char 4tP t,names2D3P names2D3,names2;3P names2;3,tP for )i,<PiA,;Pi+++ printf)0%s0,names2i3+P Q Answer: Compiler error/ I$alue reVuired in function main Explanation: Arra% names are pointer constants. #o it cannot be modified. D;+ $oid main)+ O int i,CP printf)0%d0,i++ + ++i+P Q Answer: "utput Cannot be predicted e&actl%. Explanation: #ide effects are in$ol$ed in the e$aluation of i $oid main)+ O int i,CP printf)0%d0,i++ ++ + i+P Q Answer: Compiler Wrror Explanation: The e&pression i+++++i is parsed as i ++ ++ + i .hich is an illegal combination of operators. #includeAstdio.h@ main)+ O int i,7,Y,-P s.itch)i+ O case 7/ printf)05""!0+P brea:P case Y/ printf)01A!0+P brea:P Q Q
Freshersworld.com Resource Center

DC+

D(+

First Job. Dream Job. Freshersworld.com Answer: Compiler Wrror/ Constant e&pression reVuired in function main. Explanation: The case statement can ha$e onl% constant e&pressions )this implies that .e cannot use $ariable names directl% so an error+.

#ote: Wnumerated t%pes can be used in case statements. 444444444444444444444444444


DR+ main)+ O int iP printf)0%d0,scanf)0%d0,Ji++P // $alue 7< is gi$en as input here Q Answer: 7 Explanation: #canf returns number of items successfull% read and not 7/<. 6ere 7< is gi$en as input .hich should ha$e been scanned successfull%. #o number of items read is 7.

D'+

#define f)g,g-+ g##gmain)+ O int $ar7-,7<<P printf)0%d0,f)$ar,7-++P Q Answer: 7<< main)+ O int i,<P for)Pi++Pprintf)0%d0,i++ P printf)0%d0,i+P Q Answer: 7 Explanation:
Freshersworld.com Resource Center

D=+

First Job. Dream Job. Freshersworld.com before entering into the for loop the chec:ing condition is 0e$aluated0. 6ere it e$aluates to < )false+ and comes out of the loop, and i is incremented )note the semicolon after the for loop+.

;;+

main)+ O printf)0%d0, out+P Q int out,7<<P Answer: Compiler error/ undefined s%mbol out in function main. Explanation: The rule is that a $ariable is a$ailable for use from the point of declaration. W$en though a is a global $ariable, it is not a$ailable for main. 6ence an error. main)+ O e&tern outP printf)0%d0, out+P Q int out,7<<P Answer: 7<< Explanation: This is the correct .a% of .riting the pre$ious program. main)+ O sho.)+P Q $oid sho.)+ O printf)0IGm the greatest0+P Q Answer: Compier error/ T%pe mismatch in redeclaration of sho.. Explanation: Ehen the compiler sees the function sho. it doesnGt :no. an%thing about it. #o the default return t%pe )ie, int+ is assumed. 1ut .hen compiler sees the actual definition of sho. mismatch occurs since it is declared as $oid. 6ence the error.

44444444444444444
;C+

;(+

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

The solutions are as follo.s/ 7. declare $oid sho.)+ in main)+ . -. define sho.)+ before main)+. D. declare e&tern $oid sho.)+ before the use of sho.)+.

BBBBBBBBBBBBBBBBBBB
;R+ main) + O int a2-32D32-3 , OOO-,;Q,OR,'Q,OD,;QQ,OO-,-Q,O-,DQ,OD,;QQQP printf)Z%u %u %u %d Sn[,a,4a,44a,444a+P printf)Z%u %u %u %d Sn[,a+7,4a+7,44a+7,444a+7+P Q Answer: 7<<, 7<<, 7<<, 77;, 7<;, 7<-, D Explanation: The gi$en arra% is a D?! one. It can also be $ie.ed as a 7?! arra%. - ' D ; - D D ; R 7<< 7<- 7<; 7<( 7<' 77< 77- 77; 77( 77' 7-< 7-thus, for the first printf statement a, 4a, 44a gi$e address of first element . since the indirection 444a gi$es the $alue. 6ence, the first line of the output. for the second printf a+7 increases in the third dimension thus points to $alue at 77;, 4a+7 increments in second dimension thus points to 7<;, 44a +7 increments the first dimension thus points to 7<- and 444a+7 first gets the $alue at first location and then increments it b% 7. 6ence, the output. ;'+ main) + O int a2 3 , O7<,-<,D<,;<,C<Q,Y,4pP for)Y,<P YACP Y+++ O printf)Z%d[ ,4a+P a++P Q p , aP for)Y,<P YACP Y+++ O printf)Z%d [ ,4p+P p++P
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com Q Q Answer: Compiler error/ l$alue reVuired. Explanation: Wrror is in line .ith statement a++. The operand must be an l$alue and ma% be of an% of scalar t%pe for the an% operator, arra% name onl% .hen subscripted is an l$alue. #impl% arra% name is a non?modifiable l$alue. ;=+ main) + O static int a2 3 , O<,7,-,D,;QP int 4p2 3 , Oa,a+7,a+-,a+D,a+;QP int 44ptr , pP ptr++P printf)ZSn %d %d %d[, ptr?p, 4ptr?a, 44ptr+P 4ptr++P printf)ZSn %d %d %d[, ptr?p, 4ptr?a, 44ptr+P 4++ptrP printf)ZSn %d %d %d[, ptr?p, 4ptr?a, 44ptr+P ++4ptrP printf)ZSn %d %d %d[, ptr?p, 4ptr?a, 44ptr+P Q Answer: 111 222 ### #++ Explanation: Iet us consider the arra% and the t.o pointers .ith some address <77<< 7<7<; 7<( 7<' p 7<<7 <-7< ; 7<<< 7<<- 7<<; 7<<( 7<<' ptr 7<<<-<<< After e&ecution of the instruction ptr++ $alue in ptr becomes 7<<-, if scaling factor for integer is - b%tes. >o. ptr T p is $alue in ptr T starting location of arra% p, )7<<- T 7<<<+ / )scaling factor+ , 7, 4ptr T a , $alue at address pointed b% ptr T starting $alue of arra% a, 7<<- has a $alue 7<- so the $alue is )7<- T 7<<+/)scaling factor+ , 7, 44ptr is
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com the $alue stored in the location pointed b% the pointer of ptr , $alue pointed b% $alue pointed b% 7<<- , $alue pointed b% 7<- , 7. 6ence the output of the firs printf is 7, 7, 7. After e&ecution of 4ptr++ increments $alue of the $alue in ptr b% scaling factor, so it becomes7<<;. 6ence, the outputs for the second printf are ptr T p , -, 4ptr T a , -, 44ptr , -. After e&ecution of 4++ptr increments $alue of the $alue in ptr b% scaling factor, so it becomes7<<;. 6ence, the outputs for the third printf are ptr T p , D, 4ptr T a , D, 44ptr , D. After e&ecution of ++4ptr $alue in ptr remains the same, the $alue pointed b% the $alue is incremented b% the scaling factor. #o the $alue in arra% p at location 7<<( changes from 7<( 7< 7<',. 6ence, the outputs for the fourth printf are ptr T p , 7<<( T 7<<< , D, 4ptr T a , 7<' T 7<< , ;, 44ptr , ;.

4444444444444
C<+ main) + O char 4VP int YP for )Y,<P YADP Y+++ scanf)Z%s[ ,)V+Y++P for )Y,<P YADP Y+++ printf)Z%c[ ,4)V+Y++P for )Y,<P YADP Y+++ printf)Z%s[ ,)V+Y++P Q Explanation: 6ere .e ha$e onl% one pointer to t%pe char and since .e ta:e input in the same pointer thus .e :eep .riting o$er in the same location, each time shifting the pointer $alue b% 7. #uppose the inputs are F"8#W, TKAC\ and UIKT8AI. Then for the first input suppose the pointer starts at location 7<< then the input one is stored as F"8 # W Ehen the second input is gi$en the pointer is incremented as Y $alue becomes 7, so the input is filled in memor% starting from 7<7. FTK The third input starts filling from the location 7<FTU This is the final $alue stored . The first printf prints the $alues at the position V, V+7 and V+- , F T U The second printf prints three strings starting from locations V, V+7, V+i.e FTUIKT8AI, TUIKT8AI and UIKT8AI. C7+ main) + O
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com $oid 4$pP char ch , MgN, 4cp , Zgoof%[P int Y , -<P $p , JchP printf)Z%c[, 4)char 4+$p+P $p , JYP printf)Z%d[,4)int 4+$p+P $p , cpP printf)Z%s[,)char 4+$p + D+P Q Answer: g-<f% Explanation: #ince a $oid pointer is used it can be t%pe casted to an% other t%pe pointer. $p , Jch stores address of char ch and the ne&t statement prints the $alue stored in $p after t%pe casting it to the proper data t%pe pointer. the output is MgN. #imilarl% the output from second printf is M-<N. The third printf statement t%pe casts it to print the string from the ;th $alue hence the output is Mf%N.

4444444444
C-+ main ) + O static char 4s2 3 , OZblac:[, Z.hite[, Z%ello.[, Z$iolet[QP char 44ptr2 3 , Os+D, s+-, s+7, sQ, 444pP p , ptrP 44++pP printf)Z%s[,4??4++p + D+P Q Answer: c: Explanation: In this problem .e ha$e an arra% of char pointers pointing to start of ; strings. Then .e ha$e ptr .hich is a pointer to a pointer of t%pe char and a $ariable p .hich is a pointer to a pointer to a pointer of t%pe char. p hold the initial $alue of ptr, i.e. p , s+D. The ne&t statement increment $alue in p b% 7 , thus no. $alue of p , s+-. In the printf statement the e&pression is e$aluated 4++p causes gets $alue s+7 then

the indirection operator no. gets the $alue from the arra% of s and adds D to the starting address. The string is
the pre decrement is e&ecuted and .e get s+7 T 7 , s .
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

printed starting from this position. Thus, the output is Mc:N.


CD+ main)+ O int i, nP char 4& , Zgirl[P n , strlen)&+P 4& , &2n3P for)i,<P iAnP ++i+ O printf)Z%sSn[,&+P &++P Q Q Answer: )blan: space+ irl rl l Explanation: 6ere a string )a pointer to char+ is initiali*ed .ith a $alue Zgirl[. The strlen function returns the length of the string, thus n has a $alue ;. The ne&t statement assigns $alue at the nth location )MS<N+ to the first location. >o. the string becomes ZS<irl[ . >o. the printf statement prints the string after each iteration it increments it starting position. Ioop starts from < to ;. The first time &2<3 , MS<N hence it prints nothing and pointer $alue is incremented. The second time it prints from &273 i.e Zirl[ and the third time it prints Zrl[ and the last time it prints Zl[ and the loop terminates. int i,YP for)i,<PiA,7<Pi+++ O Y+,CP assert)iAC+P Q Answer: Kuntime error/ Abnormal program termination. assert failed )iAC+, Afile name@,Aline number@ Explanation: asserts are used during debugging to ma:e sure that certain conditions are satisfied. If assertion fails, the program .ill terminate reporting the same. After debugging use, #undef >!W185
Freshersworld.com Resource Center

C;+

First Job. Dream Job. Freshersworld.com and this .ill disable all the assertions from the source code. Assertion is a good debugging tool to ma:e use of. CC+ main)+ O int i,?7P +iP printf)0i , %d, +i , %d Sn0,i,+i+P Q Answer: i , ?7, +i , ?7 Explanation: 8nar% + is the onl% dumm% operator in C. Ehere?e$er it comes %ou can Yust ignore it Yust because it has no effect in the e&pressions )hence the name dumm% operator+. Ehat are the files .hich are automaticall% opened .hen a C file is e&ecuted? Answer: stdin, stdout, stderr )standard input,standard output,standard error+.

C(+

CR+ .hat .ill be the position of the file mar:er? a/ fsee:)ptr,<,#WW\H#WT+P b/ fsee:)ptr,<,#WW\HC8K+P Answer : a/ The #WW\H#WT sets the file position mar:er to the starting of the file. b/ The #WW\HC8K sets the file position mar:er to the current position of the file. C'+ main)+ O char name27<3,s27-3P scanf)0 S0%2]S03S00,s+P Q 6o. scanf .ill e&ecute? Answer: 9irst it chec:s for the leading .hite space and discards it.Then it matches .ith a Vuotation mar: and then it reads all character upto another Vuotation mar:. Ehat is the problem .ith the follo.ing code segment? .hile ))fgets)recei$ing arra%,C<,fileHptr++ B, W"9+ P Answer & Explanation:

C=+

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com fgets returns a pointer. #o the correct end of file chec: is chec:ing for B , >8II. (<+ main)+ O main)+P Q Answer: Kuntime error / #tac: o$erflo.. Explanation: main function calls itself again and again. Wach time the function is called its return address is stored in the call stac:. #ince there is no condition to terminate the function call, the call stac: o$erflo.s at runtime. #o it terminates the program and results in an error. main)+ O char 4cptr,cP $oid 4$ptr,$P c,7<P $,<P cptr,JcP $ptr,J$P printf)0%c%$0,c,$+P Q Answer: Compiler error )at line number ;+/ si*e of $ is 8n:no.n. Explanation: Xou can create a $ariable of t%pe $oid 4 but not of t%pe $oid, since $oid is an empt% t%pe. In the second line %ou are creating $ariable $ptr of t%pe $oid 4 and $ of t%pe $oid hence an error. main)+ O char 4str7,0abcd0P char str-23,0abcd0P printf)0%d %d %d0,si*eof)str7+,si*eof)str-+,si*eof)0abcd0++P Q Answer: -CC Explanation: In first si*eof, str7 is a character pointer so it gi$es %ou the si*e of the pointer $ariable. In second si*eof the name str- indicates the name of the arra% .hose si*e is C )including the GS<G termination character+. The third si*eof is similar to the second one. main)+ O
Freshersworld.com Resource Center

(7+

(-+

(D+

First Job. Dream Job. Freshersworld.com char notP not,B-P printf)0%d0,not+P Q Answer: < Explanation: B is a logical operator. In C the $alue < is considered to be the boolean $alue 9AI#W, and an% non?*ero $alue is considered to be the boolean $alue TK8W. 6ere - is a non?*ero $alue so TK8W. BTK8W is 9AI#W )<+ so it prints <. (;+ #define 9AI#W ?7 #define TK8W 7 #define >8II < main)+ O if)>8II+ puts)0>8II0+P else if)9AI#W+ puts)0TK8W0+P else puts)09AI#W0+P Q Answer: TK8W Explanation/ The input program to the compiler after processing b% the preprocessor is, main)+O if)<+ puts)0>8II0+P else if)?7+ puts)0TK8W0+P else puts)09AI#W0+P Q reprocessor doesnGt replace the $alues gi$en inside the double Vuotes. The chec: b% if condition is boolean $alue false so it goes to else. In second if ?7 is boolean $alue true hence 0TK8W0 is printed. main)+ O int :,7P printf)0%d,,7 is 00%s0,:,:,,7?0TK8W0/09AI#W0+P Q Answer:
Freshersworld.com Resource Center

(C+

First Job. Dream Job. Freshersworld.com 7,,7 is TK8W Explanation: Ehen t.o strings are placed together )or separated b% .hite?space+ the% are concatenated )this is called as 0stringi*ation0 operation+. #o the string is as if it is gi$en as 0%d,,7 is %s0. The conditional operator) ?/ + e$aluates to 0TK8W0. ((+ main)+ O int %P scanf)0%d0,J%+P // input gi$en is -<<< if) )%%;,,< JJ %%7<< B, <+ LL %%7<< ,, < + printf)0%d is a leap %ear0+P else printf)0%d is not a leap %ear0+P Q Answer: -<<< is a leap %ear Explanation: An ordinar% program to chec: if leap %ear or not. #define ma& C #define int arr72ma&3 main)+ O t%pedef char arr-2ma&3P arr7 list,O<,7,-,D,;QP arr- name,0name0P printf)0%d %s0,list2<3,name+P Q Answer: Compiler error )in the line arr7 list , O<,7,-,D,;Q+ Explanation: arr- is declared of t%pe arra% of si*e C of characters. #o it can be used to declare the $ariable name of the t%pe arr-. 1ut it is not the case of arr7. 6ence an error. Rule of Thumb: #defines are used for te&tual replacement .hereas t%pedefs are used for declaring ne. t%pes. int i,7<P main)+ O e&tern int iP O int i,-<P
Freshersworld.com Resource Center

(R+

('+

First Job. Dream Job. Freshersworld.com O const $olatile unsigned i,D<P printf)0%d0,i+P Q printf)0%d0,i+P Q printf)0%d0,i+P Q Answer: D<,-<,7< Explanation: GOG introduces ne. bloc: and thus ne. scope. In the innermost bloc: i is declared as, const $olatile unsigned .hich is a $alid declaration. i is assumed of t%pe int. #o printf prints D<. In the ne&t bloc:, i has $alue -< and so printf prints -<. In the outermost bloc:, i is declared as e&tern, so no storage space is allocated for it. After compilation is o$er the lin:er resol$es it to global $ariable i )since it is the onl% $ariable $isible there+. #o it prints iGs $alue as 7<. (=+ main)+ O int 4YP O int i,7<P Y,JiP Q printf)0%d0,4Y+P Q Answer: 7< Explanation/ The $ariable i is a bloc: le$el $ariable and the $isibilit% is inside that bloc: onl%. 1ut the lifetime of i is lifetime of the function so it li$es upto the e&it of main function. #ince the i is still allocated space, 4Y prints the $alue stored in i since Y points i. main)+ O int i,?7P ?iP printf)0i , %d, ?i , %d Sn0,i,?i+P Q Answer: i , ?7, ?i , 7
Freshersworld.com Resource Center

R<+

First Job. Dream Job. Freshersworld.com Explanation: ?i is e&ecuted and this e&ecution doesnGt affect the $alue of i. In printf first %ou Yust print the $alue of i. After that the $alue of the e&pression ?i , ?)?7+ is printed. R7+ #includeAstdio.h@ main)+ O const int i,;P float YP Y , ++iP printf)0%d %f0, i,++Y+P Q Answer/ Compiler error Explanation/ i is a constant. %ou cannot change the $alue of constant #includeAstdio.h@ main)+ O int a2-32-32-3 , O O7<,-,D,;Q, OC,(,R,'Q QP int 4p,4VP p,Ja2-32-32-3P 4V,444aP printf)0%d..%d0,4p,4V+P Q Answer: garbage$alue..7 Explanation: p,Ja2-32-32-3 %ou declare onl% t.o -! arra%s. but %ou are tr%ing to access the third -!).hich %ou are not declared+ it .ill print garbage $alues. 4V,444a starting address of a is assigned integer pointer. no. V is pointing to starting address of a.if %ou print 4V meAns.er/it .ill print first element of D! arra%. #includeAstdio.h@ main)+ O register i,CP char Y23, 0hello0P printf)0%s %d0,Y,i+P Q Answer: hello C Explanation/
Freshersworld.com Resource Center

R-+

RD+

First Job. Dream Job. Freshersworld.com if %ou declare i as register compiler .ill treat it as ordinar% integer and it .ill ta:e integer $alue. i $alue ma% be stored either in register or in memor%. R;+ main)+ O int i,C,Y,(,*P printf)0%d0,i+++Y+P Q Answer: 77 Explanation: the e&pression i+++Y is treated as )i++ + Y+ struct aaaO struct aaa 4pre$P int iP struct aaa 4ne&tP QP main)+ O struct aaa abc,def,ghi,Y:lP int &,7<<P abc.i,<Pabc.pre$,JY:lP abc.ne&t,JdefP def.i,7Pdef.pre$,JabcPdef.ne&t,JghiP ghi.i,-Pghi.pre$,JdefP ghi.ne&t,JY:lP Y:l.i,DPY:l.pre$,JghiPY:l.ne&t,JabcP &,abc.ne&t?@ne&t?@pre$?@ne&t?@iP printf)0%d0,&+P Q Answer: Explanation: abo$e all statements form a double circular lin:ed listP abc.ne&t?@ne&t?@pre$?@ne&t?@i this one points to 0ghi0 node the $alue of at particular node is -. struct point O int &P int %P QP struct point origin,4ppP main)+
Freshersworld.com Resource Center

R(+

RR+

First Job. Dream Job. Freshersworld.com O pp,JoriginP printf)0origin is)%d%d+Sn0,)4pp+.&,)4pp+.%+P printf)0origin is )%d%d+Sn0,pp?@&,pp?@%+P Q Answer: origin is)<,<+ origin is)<,<+ Explanation/ pp is a pointer to structure. .e can access the elements of the structure either .ith arro. mar: or .ith indirection operator. #ote: #ince structure point is globall% declared & J % are initiali*ed as *eroes R'+ main)+ O int i,HlHabc)7<+P printf)0%dSn0,??i+P Q int HlHabc)int i+ O return)i+++P Q Answer: = Explanation: return)i+++ it .ill first return i and then increments. i.e. 7< .ill be returned. main)+ O char 4pP int 4VP long 4rP p,V,r,<P p++P V++P r++P printf)0%p...%p...%p0,p,V,r+P Q Answer: <<<7...<<<-...<<<; Explanation:

R=+

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com ++ operator .hen applied to pointers increments address according to their corresponding data?t%pes. '<+ main)+ O char c,G G,&,con$ert)*+P getc)c+P if))c@,GaG+ JJ )cA,G*G++ &,con$ert)c+P printf)0%c0,&+P Q con$ert)*+ O return *?D-P Q Answer: Compiler error Explanation: declaration of con$ert and format of getc)+ are .rong. main)int argc, char 44arg$+ O printf)0enter the character0+P getchar)+P sum)arg$273,arg$2-3+P Q sum)num7,num-+ int num7,num-P O return num7+num-P Q Answer: Compiler error. Explanation: arg$273 J arg$2-3 are strings. The% are passed to the function sum .ithout con$erting it to integer $alues. # include Astdio.h@ int oneHd23,O7,-,DQP main)+ O int 4ptrP ptr,oneHdP ptr+,DP printf)0%d0,4ptr+P Q
Freshersworld.com Resource Center

'7+

'-+

First Job. Dream Job. Freshersworld.com Answer/ garbage $alue Explanation: ptr pointer is pointing to out of the arra% range of oneHd. 'D+ # includeAstdio.h@ aaa)+ O printf)0hi0+P Q bbb)+O printf)0hello0+P Q ccc)+O printf)0b%e0+P Q main)+ O int )4ptr2D3+)+P ptr2<3,aaaP ptr273,bbbP ptr2-3,cccP ptr2-3)+P Q Answer/ b%e Explanation: ptr is arra% of pointers to functions of return t%pe int.ptr2<3 is assigned to address of the function aaa. #imilarl% ptr273 and ptr2-3 for bbb and ccc respecti$el%. ptr2-3)+ is in effect of .riting ccc)+, since ptr2-3 points to ccc. #includeAstdio.h@ main)+ O 9IIW 4ptrP char iP ptr,fopen)0***.c0,0r0+P .hile))i,fgetch)ptr++B,W"9+ printf)0%c0,i+P Q Answer: contents of ***.c follo.ed b% an infinite loop Explanation: The condition is chec:ed against W"9, it should be chec:ed against >8II.

'C+

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com '(+ main)+ O int i ,<PY,<P if)i JJ Y+++ printf)0%d..%d0,i++,Y+P printf)0%d..%d,i,Y+P Q Answer: <..< Explanation: The $alue of i is <. #ince this information is enough to determine the truth $alue of the boolean e&pression. #o the statement follo.ing the if statement is not e&ecuted. The $alues of i and Y remain unchanged and get printed. main)+ O int iP i , abc)+P printf)0%d0,i+P Q abc)+ O HA^ , 7<<<P Q Answer: 7<<< Explanation: >ormall% the return $alue from the function is through the information from the accumulator. 6ere HA6 is the pseudo global $ariable denoting the accumulator. 6ence, the $alue of the accumulator is set 7<<< so the function returns $alue 7<<<. int iP main)+O int tP for ) t,;Pscanf)0%d0,Ji+?tPprintf)0%dSn0,i++ printf)0%d??0,t??+P Q // If the inputs are <,7,-,D find the o/p Answer: ;??< D??7 -??Explanation: Iet us assume some &, scanf)0%d0,Ji+?t the $alues during e&ecution
Freshersworld.com Resource Center

'R+

''+

First Job. Dream Job. Freshersworld.com .ill be, t i ; < D 7 '=+

& ?; ?<

main)+O int a, <Pint b , -<Pchar & ,7Pchar % ,7<P if)a,b,&,%+ printf)0hello0+P Q Answer: hello Explanation: The comma operator has associati$it% from left to right. "nl% the rightmost $alue is returned and the other $alues are e$aluated and ignored. Thus the $alue of last $ariable % is returned to chec: in if. #ince it is a non *ero $alue if becomes true so, 0hello0 .ill be printed. main)+O unsigned int iP for)i,7Pi@?-Pi??+ printf)0c aptitude0+P Q Explanation: i is an unsigned integer. It is compared .ith a signed $alue. #ince the both t%pes doesnGt match, signed is promoted to unsigned $alue. The unsigned eVui$alent of ?- is a huge $alue so condition becomes false and control comes out of the loop. In the follo.ing pgm add a stmt in the function fun such that the address of GaG gets stored in GYG. main)+O int 4 YP $oid fun)int 44+P fun)JY+P Q $oid fun)int 44:+ O int a ,<P /4 add a stmt here4/ Q Answer: 4: , Ja Explanation: The argument of the function is a pointer to a pointer.

=<+

=7+

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com =-+ Ehat are the follo.ing notations of defining functions :no.n as? i. int abc)int a,float b+ O /4 some code 4/ Q ii. int abc)a,b+ int aP float bP O /4 some code4/ Q Answer: i. A>#I C notation ii. \ernighan J Kitche notation main)+ O char 4pP p,0%dSn0P p++P p++P printf)p?-,D<<+P Q Answer: D<< Explanation: The pointer points to % since it is incremented t.ice and again decremented b% -, it points to G%dSnG and D<< is printed. main)+O char a27<<3P a2<3,GaGPa2733,GbGPa2-3,GcGPa2;3,GdGP abc)a+P Q abc)char a23+O a++P printf)0%c0,4a+P a++P printf)0%c0,4a+P Q Explanation: The base address is modified onl% in function and as a result a points to GbG then after incrementing to GcG so bc .ill be printed. func)a,b+ int a,bP O
Freshersworld.com Resource Center

=D+

=;+

=C+

First Job. Dream Job. Freshersworld.com return) a, )a,,b+ +P Q main)+ O int process)+,func)+P printf)0The $alue of process is %d BSn 0,process)func,D,(++P Q process)pf,$al7,$al-+ int )4pf+ )+P int $al7,$al-P O return))4pf+ )$al7,$al-++P Q Answer: The $alue if process is < B Explanation: The function GprocessG has D parameters ? 7, a pointer to another function - and D, integers. Ehen this function is in$o:ed from main, the follo.ing substitutions for formal parameters ta:e place/ func for pf, D for $al7 and ( for $al-. This function returns the result of the operation performed b% the function GfuncG. The function func has t.o integer parameters. The formal parameters are substituted as D for a and ( for b. since D is not eVual to (, a,,b returns <. therefore the function returns < .hich in turn is returned b% the function GprocessG. =(+ $oid main)+ O static int i,CP if)??i+O main)+P printf)0%d 0,i+P Q Q Answer: <<<< Explanation: The $ariable 0I0 is declared as static, hence memor% for I .ill be allocated for onl% once, as it encounters the statement. The function main)+ .ill be called recursi$el% unless I becomes eVual to <, and since main)+ is recursi$el% called, so the $alue of static I ie., < .ill be printed e$er% time the control is returned. $oid main)+ O int :,ret)si*eof)float++P printf)0Sn here $alue is %d0,++:+P
Freshersworld.com Resource Center

=R+

First Job. Dream Job. Freshersworld.com Q int ret)int ret+ O ret +, -.CP return)ret+P Q Answer: 6ere $alue is R Explanation: The int ret)int ret+, ie., the function name and the argument name can be the same. 9irstl%, the function ret)+ is called in .hich the si*eof)float+ ie., ; is passed, after the first 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 from float to int. The ret is returned in main)+ it is printed after and preincrement. ='+ $oid main)+ O char a23,07-D;CS<0P int i,strlen)a+P printf)0here in D %dSn0,++i+P Q Answer: here in D ( Explanation: The char arra% GaG .ill hold the initiali*ed string, .hose length .ill be counted from < till the null character. 6ence the GIG .ill hold the $alue eVual to C, after the pre?increment in the printf statement, the ( .ill be printed. $oid main)+ O unsigned gi$eit,?7P int gotitP printf)0%u 0,++gi$eit+P printf)0%u Sn0,gotit,??gi$eit+P Q Answer: < (CCDC Explanation: $oid main)+ O int iP char a23,0S<0P if)printf)0%sSn0,a++ printf)0": here Sn0+P
Freshersworld.com Resource Center

==+

7<<+

First Job. Dream Job. Freshersworld.com else printf)09orget itSn0+P Q Answer: ": here Explanation: rintf .ill return ho. man% characters does it print. 6ence printing a null character returns 7 .hich ma:es the if statement true, thus 0": here0 is printed. 7<7+ $oid main)+ O $oid 4$P int integer,-P int 4i,JintegerP $,iP printf)0%d0,)int4+4$+P Q Answer: Compiler Wrror. Ee cannot appl% indirection on t%pe $oid4. Explanation: Uoid pointer is a generic pointer t%pe. >o pointer arithmetic can be done on it. Uoid pointers are normall% used for, 7. assing generic pointers to functions and returning such pointers. -. As a intermediate pointer t%pe. D. 8sed .hen the e&act pointer t%pe .ill be :no.n at a later point of time. $oid main)+ O int i,i++,Y,Y++,:,:++P printf)Z%d%d%d[,i,Y,:+P Q Answer: 5arbage $alues. Explanation: Gn id%ntifi%r is avai$a'$% to &s% in pro=ram cod% from th% point of its d%c$aration. #o e&pressions such as i , i++ are $alid statements. The i, Y and : are automatic $ariables and so the% contain some garbage $alue. Har'a=% in is =ar'a=% o&t (H)HI). $oid main)+ O
Freshersworld.com Resource Center

7<-+

7<D+

First Job. Dream Job. Freshersworld.com static int i,i++, Y,Y++, :,:++P printf)Zi , %d Y , %d : , %d[, i, Y, :+P Q Answer: i,7Y,7:,7 Explanation: #ince static $ariables are initiali*ed to *ero b% default. 7<;+ $oid main)+ O .hile)7+O if)printf)0%d0,printf)0%d0+++ brea:P else continueP Q Q Answer: 5arbage $alues Explanation: The inner printf e&ecutes first to print some garbage $alue. The printf returns no of characters printed and this $alue also cannot be predicted. #till the outer printf prints something and so returns a non?*ero $alue. #o it encounters the brea: statement and comes out of the .hile statement. main)+ O unsigned int i,7<P .hile)i??@,<+ printf)0%u 0,i+P Q Answer: 7< = ' R ( C ; D - 7 < (CCDC (CCD;_.. Explanation: #ince i is an unsigned integer it can ne$er become negati$e. #o the e&pression i?? @,< .ill al.a%s be true, leading to an infinite loop. 7<C+ #includeAconio.h@ main)+ O int &,%,-,*,aP if)&,%%-+ *,-P a,-P printf)0%d %d 0,*,&+P
Freshersworld.com Resource Center

7<;+

First Job. Dream Job. Freshersworld.com Q Answer: 5arbage?$alue < Explanation: The $alue of %%- is <. This $alue is assigned to &. The condition reduces to if )&+ or in other .ords if)<+ and so * goes uninitiali*ed. Thumb Rule: (hec: all control paths to .rite bug free code. 7<(+ main)+ O int a27<3P printf)0%d0,4a+7?4a+D+P Q Answer: ; Explanation: 4a and ?4a cancels out. The result is as simple as 7 + D , ; B 7<R+ #define prod)a,b+ a4b main)+ O int &,D,%,;P printf)0%d0,prod)&+-,%?7++P Q Answer: 7< Explanation: The macro e&pands and e$aluates to as/ &+-4%?7 ,@ &+)-4%+?7 ,@ 7< main)+ O unsigned int i,(C<<<P .hile)i++B,<+P printf)0%d0,i+P Q Answer: 7 Explanation: >ote the semicolon after the .hile statement. Ehen the $alue of i becomes < it comes out of .hile loop. !ue to post?increment on i the $alue of i .hile printing is 7. 7<=+ main)+ O int i,<P
Freshersworld.com Resource Center

7<'+

First Job. Dream Job. Freshersworld.com .hile)+)+i??+B,<+ i?,i++P printf)0%d0,i+P Q Answer: ?7 Explanation: *nar( + is th% on$( d&mm( op%rator in @. #o it has no effect on the e&pression and no. the .hile loop is, .hile)i??B,<+ .hich is false and so brea:s out of .hile loop. The $alue T7 is printed due to the post?decrement operator. 77D+ main)+ O float f,C,g,7<P enumOi,7<,Y,-<,:,C<QP printf)0%dSn0,++:+P printf)0%fSn0,fAA-+P printf)0%lfSn0,f%g+P printf)0%lfSn0,fmod)f,g++P Q Answer: Iine no C/ Wrror/ I$alue reVuired Iine no (/ Cannot appl% leftshift to float Iine no R/ Cannot appl% mod to float Explanation: Wnumeration constants cannot be modified, so %ou cannot appl% ++. 1it?.ise operators and % operators cannot be applied on float $alues. fmod)+ is to find the modulus $alues for floats as % operator is for ints. 77<+ main)+ O int i,7<P $oid pascal f)int,int,int+P f)i++,i++,i+++P printf)0 %d0,i+P Q $oid pascal f)integer /i,integer/Y,integer /:+ O .rite)i,Y,:+P Q Answer: Compiler error/ un:no.n t%pe integer Compiler error/ undeclared function .rite Explanation:

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com ascal :e%.ord doesnNt mean that pascal code can be used. It means that the function follo.s ascal argument passing mechanism in calling the functions. 777+ $oid pascal f)int i,int Y,int :+ O printf)Z%d %d %d[,i, Y, :+P Q $oid cdecl f)int i,int Y,int :+ O printf)Z%d %d %d[,i, Y, :+P Q main)+ O int i,7<P f)i++,i++,i+++P printf)0 %dSn0,i+P i,7<P f)i++,i++,i+++P printf)0 %d0,i+P Q Answer: 7< 77 7- 7D 7- 77 7< 7D Explanation: ascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism .here the arguments are passed from right to left.

77-+. Ehat is the output of the program gi$en belo. main)+ O signed char i,<P for)Pi@,<Pi+++ P printf)0%dSn0,i+P Q Answer ?7-' Explanation >otice the semicolon at the end of the for loop. T6e initial $alue of the i is set to <. The inner loop e&ecutes to increment the $alue from < to 7-R )the positi$e range of char+ and then it rotates to the negati$e $alue of ?7-'. The condition in the for loop fails and so comes out of the for loop. It prints the current $alue of i that is ?7-'.
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com

77D+ main)+ O unsigned char i,<P for)Pi@,<Pi+++ P printf)0%dSn0,i+P Q Answer infinite loop Explanation The difference bet.een the pre$ious Vuestion and this one is that the char is declared to be unsigned. #o the i++ can ne$er %ield negati$e $alue and i@,< ne$er becomes false so that it can come out of the for loop. 77;+ main)+ O char i,<P for)Pi@,<Pi+++ P printf)0%dSn0,i+P Q Answer: 1eha$ior is implementation dependent. Explanation: The detail if the char is signed/unsigned b% default is implementation dependent. If the implementation treats the char to be signed b% default the program .ill print T7-' and terminate. "n the other hand if it considers char to be unsigned b% default, it goes to infinite loop. Rule: Xou can .rite programs that ha$e implementation dependent beha$ior. 1ut dont .rite programs that depend on such beha$ior. 77C+ Is the follo.ing statement a declaration/definition. 9ind .hat does it mean? int )4&+27<3P Answer !efinition. & is a pointer to arra% of)si*e 7<+ integers. Appl% cloc:?.ise rule to find the meaning of this definition. 77(+. Ehat is the output for the program gi$en belo. t%pedef enum errorT%peO.arning, error, e&ception,QerrorP
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com main)+ O error g7P g7,7P printf)0%d0,g7+P Q Answer Compiler error/ Fultiple declaration for error Explanation The name error is used in the t.o meanings. "ne means that it is a enumerator constant .ith $alue 7. The another use is that it is a t%pe name )due to t%pedef+ for enum errorT%pe. 5i$en a situation the compiler cannot distinguish the meaning of error to :no. in .hat sense the error is used/ error g7P g7,errorP // .hich error it refers in each case? Ehen the compiler can distinguish bet.een usages then it .ill not issue error )in pure technical terms, names can onl% be o$erloaded in different namespaces+. Note/ the e&tra comma in the declaration, enum errorT%peO.arning, error, e&ception,Q is not an error. An e&tra comma is $alid and is pro$ided Yust for programmerNs con$enience. 77R+ t%pedef struct errorOint .arning, error, e&ceptionPQerrorP main)+ O error g7P g7.error ,7P printf)0%d0,g7.error+P Q 7 Explanation The three usages of name errors can be distinguishable b% the compiler at an% instance, so $alid )the% are in different namespaces+. T%pedef struct errorOint .arning, error, e&ceptionPQerrorP This error can be used onl% b% preceding the error b% struct :a%.ord as in/ struct error someWrrorP t%pedef struct errorOint .arning, error, e&ceptionPQerrorP This can be used onl% after . )dot+ or ?@ )arro.+ operator preceded b% the $ariable name as in / g7.error ,7P
Freshersworld.com Resource Center

Answer

First Job. Dream Job. Freshersworld.com printf)0%d0,g7.error+P t%pedef struct errorOint .arning, error, e&ceptionPQerrorP This can be used to define $ariables .ithout using the preceding struct :e%.ord as in/ error g7P #ince the compiler can perfectl% distinguish bet.een these three usages, it is perfectl% legal and $alid. #ote This code is gi$en here to Yust e&plain the concept behind. In real programming donNt use such o$erloading of names. It reduces the readabilit% of the code. ossible doesnNt mean that .e should use itB 77'+ #ifdef something int some,<P #endif main)+ O int thing , <P printf)0%d %dSn0, some ,thing+P Q Answer: Compiler error / undefined s%mbol some Explanation: This is a $er% simple e&ample for conditional compilation. The name something is not alread% :no.n to the compiler ma:ing the declaration int some , <P effecti$el% remo$ed from the source code. 77=+ #if something ,, < int some,<P #endif main)+ O int thing , <P printf)0%d %dSn0, some ,thing+P Q Answer << Explanation

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com This code is to sho. that preprocessor e&pressions are not the same as the ordinar% e&pressions. If a name is not :no.n the preprocessor treats it to be eVual to *ero. 7-<+. Ehat is the output for the follo.ing program main)+ O int arr-!2D32D3P printf)0%dSn0, ))arr-!,,4 arr-!+JJ)4 arr-! ,, arr-!2<3++ +P Q Answer 7 Explanation This is due to the close relation bet.een the arra%s and pointers. > dimensional arra%s are made up of )>?7+ dimensional arra%s. arr-! is made up of a D single arra%s that contains D integers each . arr-! arr-!273 arr-!2-3 arr-!2D3

The name arr-! refers to the beginning of all the D arra%s. 4arr-! refers to the start of the first 7! arra% )of D integers+ that is the same address as arr-!. #o the e&pression )arr-! ,, 4arr-!+ is true )7+. #imilarl%, 4arr-! is nothing but 4)arr-! + <+, adding a *ero doesnNt change the $alue/meaning. Again arr-!2<3 is the another .a% of telling 4)arr-! + <+. #o the e&pression )4)arr-! + <+ ,, arr-!2<3+ is true )7+. #ince both parts of the e&pression e$aluates to true the result is true)7+ and the same is printed. 7-7+ $oid main)+ O if)`< ,, )unsigned int+?7+ printf)ZXou can ans.er this if %ou :no. ho. $alues are represented in memor%[+P Q Ans.er

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com Xou can ans.er this if %ou :no. ho. $alues are represented in memor% W&planation ` )tilde operator or bit?.ise negation operator+ operates on < to produce all ones to fill the space for an integer. T7 is represented in unsigned $alue as all 7Ns and so both are eVual. 7--+ int s.ap)int 4a,int 4b+ O 4a,4a+4bP4b,4a?4bP4a,4a?4bP Q main)+ O int &,7<,%,-<P s.ap)J&,J%+P printf)0&, %d % , %dSn0,&,%+P Q Ans.er & , -< % , 7< W&planation This is one .a% of s.apping t.o $alues. #imple chec:ing .ill help understand this. 7-D+ main)+ O char 4p , Za%Vm[P printf)Z%c[,++4)p++++P Q Ans.er/ b main)+ O int i,CP printf)0%d0,++i+++P Q Answer: Compiler error/ I$alue reVuired in function main Explanation: ++i %ields an r$alue. 9or postfi& ++ to operate an l$alue is reVuired. 7-C+ main)+ O char 4p , Za%Vm[P char cP
Freshersworld.com Resource Center

7-;+

First Job. Dream Job. Freshersworld.com c , ++4p++P printf)Z%c[,c+P Q Answer: b Explanation: There is no difference bet.een the e&pression ++4)p+++ and + +4p++. arenthesis Yust .or:s as a $isual clue for the reader to see .hich e&pression is first e$aluated. 7-(+ int aaa)+ Oprintf)Z6i[+PQ int bbb)+Oprintf)Zhello[+PQ in% ccc)+Oprintf)Zb%e[+PQ main)+ O int ) 4 ptr2D3+ )+P ptr2<3 , aaaP ptr273 , bbbP ptr2-3 ,cccP ptr2-3)+P Q Ans.er/ b!e W&planation/ int )4 ptr2D3+)+ sa%s that ptr is an arra% of pointers to functions that ta:es no arguments and returns the t%pe int. 1% the assignment ptr2<3 , aaaP it means that the first function pointer in the arra% is initiali*ed .ith the address of the function aaa. #imilarl%, the other t.o arra% elements also get initiali*ed .ith the addresses of the functions bbb and ccc. #ince ptr2-3 contains the address of the function ccc, the call to the function ptr2-3)+ is same as calling ccc)+. #o it results in printing 0b%e0. 7-R+ main)+ O int i,CP printf)Z%d[,i,++i ,,(+P Q Answer: 1 Explanation:

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com The e&pression can be treated as i , )++i,,(+, because ,, is of higher precedence than , operator. In the inner e&pression, ++i is eVual to ( %ielding true)7+. 6ence the result. 7-'+ main)+ O char p2 3,0%dSn0P p273 , GcGP printf)p,(C+P Q Answer: A Explanation: !ue to the assignment p273 , McN the string becomes, Z%cSn[. #ince this string becomes the format string for printf and A#CII $alue of (C is MAN, the same gets printed. 7-=+ $oid ) 4 abc) int, $oid ) 4def+ )+ + + )+P Ans.er// abc is a ptr to a function .hich ta:es - parameters .)a+. an integer $ariable.)b+. a ptrto a funtion .hich returns $oid. the return t%pe of the function is $oid. Explanation: Appl% the cloc:?.ise rule to find the result. 7D<+ main)+ O .hile )strcmp)Zsome[,[someS<[++ printf)Z#trings are not eVualSn[+P Q Answer: >o output Explanation: Wnding the string constant .ith S< e&plicitl% ma:es no difference. #o Zsome[ and ZsomeS<[ are eVui$alent. #o, strcmp returns < )false+ hence brea:ing out of the .hile loop. main)+ O char str723 , OMsN,NoN,NmN,NeNQP char str-23 , OMsN,NoN,NmN,NeN,NS<NQP .hile )strcmp)str7,str-++ printf)Z#trings are not eVualSn[+P Q
Freshersworld.com Resource Center

7D7+

First Job. Dream Job. Freshersworld.com Answer: Z#trings are not eVual[ Z#trings are not eVual[ _. Explanation: If a string constant is initiali*ed e&plicitl% .ith characters, MS<N is not appended automaticall% to the string. #ince str7 doesnNt ha$e null termination, it treats .hate$er the $alues that are in the follo.ing positions as part of the string until it randoml% reaches a MS<N. #o str7 and str- are not the same, hence the result. 7D-+ main)+ O int i , DP for )Pi++,<P+ printf)Z%d[,i+P Q Answer: Compiler Wrror/ I$alue reVuired. Explanation: As .e :no. that increment operators return r$alues and hence it cannot appear on the left hand side of an assignment operation. 7DD+ $oid main)+ O int 4mptr, 4cptrP mptr , )int4+malloc)si*eof)int++P printf)Z%d[,4mptr+P int 4cptr , )int4+calloc)si*eof)int+,7+P printf)Z%d[,4cptr+P Q Answer: garbage?$alue < Explanation: The memor% space allocated b% malloc is uninitiali*ed, .hereas calloc returns the allocated memor% space initiali*ed to *eros. $oid main)+ O static int iP .hile)iA,7<+ )i@-+?i++/i??P printf)Z%d[, i+P Q Answer:
Freshersworld.com Resource Center

7D;+

First Job. Dream Job. Freshersworld.com D-R(R Explanation: #ince i is static it is initiali*ed to <. Inside the .hile loop the conditional operator e$aluates to false, e&ecuting i??. This continues till the integer $alue rotates to positi$e $alue )D-R(R+. The .hile condition becomes false and hence, comes out of the .hile loop, printing the i $alue. 7DC+ main)+ O int i,7<,Y,-<P Y , i, Y?)i,Y+?i/Y/YP printf)0%d %d0,i,Y+P Q Answer: 7< 7< Explanation: The Ternar% operator ) ? / + is eVui$alent for if?then?else statement. #o the Vuestion can be .ritten as/ if)i,Y+ O if)i,Y+ Y , iP else Y , YP Q else Y , YP 7D(+ 7. const char 4aP -. char4 const aP D. char const 4aP ?!ifferentiate the abo$e declarations. Answer: 7. GconstG applies to char 4 rather than GaG ) pointer to a constant char + 4a,G9G / illegal a,06i0 / legal -. GconstG applies to GaG rather than to the $alue of a )constant pointer to char + 4a,G9G a,06i0 / legal / illegal

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com D. #ame as 7. 7DR+ main)+ O int i,C,Y,7<P i,iJ,YJJ7<P printf)0%d %d0,i,Y+P Q Answer: 7 7< Explanation: The e&pression can be .ritten as i,)iJ,)YJJ7<++P The inner e&pression )YJJ7<+ e$aluates to 7 because Y,,7<. i is C. i , CJ7 is 7. 6ence the result. 7D'+ main)+ O int i,;,Y,RP Y , Y LL i++ JJ printf)0X"8 CA>0+P printf)0%d %d0, i, Y+P Q Answer: ;7 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. Y is not eVual to *ero itself means that the e&pressionNs truth $alue is 7. 1ecause it is follo.ed b% LL and tr&% 88 (an(thin=) =C tr&% ;h%r% (an(thin=) ;i$$ not '% %va$&at%d. #o the remaining e&pression is not e$aluated and so the $alue of i remains the same. #imilarl% .hen JJ operator is in$ol$ed in an e&pression, .hen an% of the operands become false, the .hole e&pressionNs truth $alue becomes false 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. 7D=+ main)+ O register int a,-P printf)0Address of a , %d0,Ja+P printf)0Ualue of a , %d0,a+P Q Answer: Compier Wrror/ GJG on register $ariable
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com Rule to Remember: & (address of ) operator cannot be applied on register variables 7;<+ main)+ O float i,7.CP s.itch)i+ O case 7/ printf)070+P case -/ printf)0-0+P default / printf)0<0+P Q Q Answer: Compiler Wrror/ s.itch e&pression not integral Explanation: K;itch stat%m%nts can '% app$i%d on$( to int%=ra$ t(p%s. 7;7+ main)+ O e&tern iP printf)0%dSn0,i+P O int i,-<P printf)0%dSn0,i+P Q Q Answer: Iin:er Wrror / 8nresol$ed e&ternal s%mbol i Explanation: The identifier i is a$ailable in the inner bloc: and so using e&tern has no use in resol$ing it. 7;-+ main)+ O int a,-,4f7,4f-P f7,f-,JaP 4f-+,4f-+,a+,-.CP printf)0Sn%d %d %d0,a,4f7,4f-+P Q Answer: 7( 7( 7( Explanation: f7 and f- both refer to the same memor% location a. #o changes through f7 and f- ultimatel% affects onl% the $alue of a.

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com 7;D+ main)+ O char 4p,05""!0P char a2 3,05""!0P printf)0Sn si*eof)p+ , %d, si*eof)4p+ , %d, strlen)p+ , %d0, si*eof)p+, si*eof)4p+, strlen)p++P printf)0Sn si*eof)a+ , %d, strlen)a+ , %d0, si*eof)a+, strlen)a++P Q Answer: si*eof)p+ , -, si*eof)4p+ , 7, strlen)p+ , ; si*eof)a+ , C, strlen)a+ , ; Explanation: si*eof)p+ ,@ si*eof)char4+ ,@ si*eof)4p+ ,@ si*eof)char+ ,@ 7 #imilarl%, si*eof)a+ ,@ si*e of the character arra% ,@ C ?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*eof the pointer $ariable. 6ere the si*eof)a+ .here a is the character arra% and the si*e of the arra% is C because the space necessar% for the terminating >8II character should also be ta:en into account. 7;;+ #define !IF) arra%, t%pe+ si*eof)arra%+/si*eof)t%pe+ main)+ O int arr27<3P printf)ZThe dimension of the arra% is %d[, !IF)arr, int++P Q Answer: 7< Explanation: The si*e of integer arra% of 7< elements is 7< 4 si*eof)int+. The macro e&pands to si*eof)arr+/si*eof)int+ ,@ 7< 4 si*eof)int+ / si*eof)int+ ,@ 7<. int !IF)int arra%23+ O return si*eof)arra%+/si*eof)int +P Q main)+ O int arr27<3P printf)ZThe dimension of the arra% is %d[, !IF)arr++P Q Answer: 7
Freshersworld.com Resource Center

7;C+

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. #o the argument is eVui$alent to int 4 arra% )this is one of the $er% fe. places .here 23 and 4 usage are eVui$alent+. The return statement becomes, si*eof)int 4+/ si*eof)int+ that happens to be eVual in this case. 7;(+ main)+ O static int a2D32D3,O7,-,D,;,C,(,R,',=QP int i,YP static 4p23,Oa,a+7,a+-QP for)i,<PiADPi+++ O for)Y,<PYADPY+++ printf)0%dSt%dSt%dSt%dSn0,4)4)p+i++Y+, 4)4)Y+p++i+,4)4)i+p++Y+,4)4)p+Y++i++P Q Q Answer: 7 D ; C ( R ' = 7 ; R C ' D ( = 7 D ; C ( R ' = 7 ; R C ' D ( =

Explanation: 4)4)p+i++Y+ is eVui$alent to p2i32Y3. 7;R+ main)+ O $oid s.ap)+P int &,7<,%,'P s.ap)J&,J%+P printf)0&,%d %,%d0,&,%+P Q $oid s.ap)int 4a, int 4b+ O 4a ], 4b, 4b ], 4a, 4a ], 4bP Q Answer: &,7< %,' Explanation:
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com 8sing ] li:e 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)+P means that s.ap is a function that ma% ta:e an% number of arguments )not no arguments+ and returns nothing. #o this doesnNt issue a compiler error b% the call s.ap)J&,J%+P that has t.o arguments. This con$ention is historicall% due to pre?A>#I st%le )referred to as \ernighan and Kitchie st%le+ st%le of function declaration. In that st%le, the s.ap function .ill be defined as follo.s, $oid s.ap)+ int 4a, int 4b O 4a ], 4b, 4b ], 4a, 4a ], 4bP Q .here the arguments follo. the )+. #o naturall% the declaration for s.ap .ill loo: li:e, $oid s.ap)+ .hich means the s.ap can ta:e an% number of arguments. 7;'+ main)+ O int i , -CRP int 4i tr , JiP printf)0%d %d0, 4))char4+i tr+, 4))char4+i tr+7+ +P Q Answer: 77 Explanation: The integer $alue -CR is stored in the memor% as, <<<<<<<7 <<<<<<<7, so the indi$idual b%tes are ta:en b% casting it to char 4 and get printed. 7;=+ main)+ O int i , -C'P int 4i tr , JiP printf)0%d %d0, 4))char4+i tr+, 4))char4+i tr+7+ +P Q Answer: -7 Explanation: The integer $alue -CR can be represented in binar% as, <<<<<<<7 <<<<<<<7. Kemember that the I>TWI machines are Msmall?endianN 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 -C' is stored in memor% as/ <<<<<<<7 <<<<<<7<.

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com 7C<+ main)+ O int i,D<<P char 4ptr , JiP 4++ptr,-P printf)0%d0,i+P Q Answer: CC( Explanation: The integer $alue D<< in binar% notation is/ <<<<<<<7 <<7<77<<. It is stored in memor% )small?endian+ as/ <<7<77<< <<<<<<<7. Kesult of the e&pression 4++ptr , - ma:es the memor% representation as/ <<7<77<< <<<<<<7<. #o the integer corresponding to it is <<<<<<7< <<7<77<< ,@ CC(. 7C7+ #include Astdio.h@ main)+ O char 4 str , 0hello0P char 4 ptr , strP char least , 7-RP .hile )4ptr+++ least , )4ptrAleast + ?4ptr /leastP printf)0%d0,least+P Q Answer: < Explanation: After MptrN reaches the end of the string the $alue pointed b% MstrN is MS<N. #o the $alue of MstrN is less than that of MleastN. #o the $alue of MleastN finall% is <. !eclare an arra% of > pointers to functions returning pointers to functions returning pointers to characters? Answer: )char4)4+) ++ )4ptr2>3+) +P main)+ O struct student O char name2D<3P struct date dobP QstudP struct date
Freshersworld.com Resource Center

7C-+

7CD+

First Job. Dream Job. Freshersworld.com O int da%,month,%earP QP scanf)0%s%d%d%d0, stud.rollno, Jstudent.dob.month, Jstudent.dob.%ear+P

Jstudent.dob.da%,

Q Answer: Compiler Wrror/ 8ndefined structure date Explanation: Inside the struct definition of MstudentN the member of t%pe struct date is gi$en. The compiler doesnNt ha$e the definition of date structure )for.ard reference is not allo.ed in C in this case+ so it issues an error. 7C;+ main)+ O struct dateP struct student O char name2D<3P struct date dobP QstudP struct date O int da%,month,%earP QP scanf)0%s%d%d%d0, stud.rollno, Jstudent.dob.month, Jstudent.dob.%ear+P

Jstudent.dob.da%,

Q Answer: Compiler Wrror/ 8ndefined structure date Explanation: "nl% declaration of struct date is a$ailable inside the structure definition of MstudentN but to ha$e a $ariable of t%pe struct date the definition of the structure is reVuired. 7CC+ There .ere 7< records stored in Zsomefile.dat[ but the follo.ing program printed 77 names. Ehat .ent .rong? $oid main)+ O struct student O char name2D<3, rollno2(3P QstudP 9IIW 4fp , fopen)Zsomefile.dat[,[r[+P .hile)Bfeof)fp++
Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com O fread)Jstud, si*eof)stud+, 7 , fp+P puts)stud.name+P Q Q Explanation: fread reads 7< records and prints the names successfull%. It .ill return W"9 onl% .hen fread tries to read another record and fails reading W"9 )and returning W"9+. #o it prints the last record again. After this onl% the condition feof)fp+ becomes false, hence comes out of the .hile loop. 7C(+ Is there an% difference bet.een the t.o declarations, 7. int foo)int 4arr23+ and -. int foo)int 4arr2-3+ Answer: >o Explanation: 9unctions can onl% pass pointers and not arra%s. The numbers that are allo.ed inside the 23 is Yust for more readabilit%. #o there is no difference bet.een the t.o declarations. Ehat is the subtle error in the follo.ing code segment? $oid fun)int n, int arr23+ O int 4p,<P int i,<P .hile)i++An+ p , Jarr2i3P 4p , <P Q Answer & Explanation: If the bod% of the loop ne$er e&ecutes p is assigned no address. #o p remains >8II .here 4p ,< ma% result in problem )ma% rise to runtime error Z>8II pointer assignment[ and terminate the program+. Ehat is .rong .ith the follo.ing code? int 4foo)+ O int 4s , malloc)si*eof)int+7<<+P assert)s B, >8II+P return sP Q Answer & Explanation:
Freshersworld.com Resource Center

7CR+

7C'+

First Job. Dream Job. Freshersworld.com assert macro should be used for debugging and finding out bugs. The chec: s B, >8II is for error/e&ception handling and for that assert shouldnNt be used. A plain if and the corresponding remed% statement has to be gi$en. 7C=+ Ehat is the hidden bug .ith the follo.ing statement? assert)$al++ B, <+P Answer & Explanation: Assert macro is used for debugging and remo$ed in release $ersion. In assert, the e&perssion in$ol$es side?effects. #o the beha$ior of the code becomes different in case of 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)+ O int 4i , <&;<<P // i points to the address ;<< 4i , <P // set the $alue of memor% location pointed b% iP Q Answer: 8ndefined beha$ior Explanation: The second statement results in undefined beha$ior because it points to some location .hose $alue ma% not be a$ailable for modification. 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. #define assert)cond+ if)B)cond++ S )fprintf)stderr, 0assertion failed/ %s, file %s, line %d Sn0,#cond,S HH9IIWHH,HHII>WHH+, abort)++ $oid main)+ O int i , 7<P if)i,,<+ assert)i A 7<<+P else printf)0This statement becomes else for if in assert macro0+P Q Ans.er/ >o output Explanation: The else part in .hich the printf is there becomes the else for if in the assert macro. 6ence nothing is printed.
Freshersworld.com Resource Center

7(<+

7(7+

First Job. Dream Job. Freshersworld.com The solution is to use conditional operator instead of if statement, #define assert)cond+ ))cond+?)<+/ )fprintf )stderr, 0assertion failed/ S %s, file %s, line %d Sn0,#cond, HH9IIWHH,HHII>WHH+, abort)+++ >ote/ 6o.e$er this problem of Zmatching .ith nearest else[ cannot be sol$ed b% the usual method of placing the if statement inside a bloc: li:e this, #define assert)cond+ O S if)B)cond++ S )fprintf)stderr, 0assertion failed/ %s, file %s, line %d Sn0,#cond,S HH9IIWHH,HHII>WHH+, abort)++ S Q 7(-+ Is the follo.ing code legal? struct a O int &P struct a bP Q Ans.er/ >o Explanation: Is it not legal for a structure to contain a member that is of the same t%pe as in this case. 1ecause this .ill cause the structure declaration to be recursi$e .ithout end. Is the follo.ing code legal? struct a O int &P struct a 4bP Q Answer: Xes. Explanation: 4b is a pointer to t%pe struct a and so is legal. The compiler :no.s, the si*e of the pointer to a structure e$en before the si*e of the structure is determined)as %ou :no. the pointer to an% t%pe is of same si*e+. This t%pe of structures is :no.n as Mself?referencingN structure. Is the follo.ing code legal? t%pedef struct a O int &P aT%pe 4bP
Freshersworld.com Resource Center

7(D+

7(;+

First Job. Dream Job. Freshersworld.com QaT%pe Answer: >o Explanation: The t%pename aT%pe is not :no.n at the point of declaring the structure )for.ard references are not made for t%pedefs+. 7(C+ Is the follo.ing code legal? t%pedef struct a aT%peP struct a O int &P aT%pe 4bP QP Answer: Xes Explanation: The t%pename aT%pe is :no.n at the point of declaring the structure, because it is alread% t%pedefined. Is the follo.ing code legal? $oid main)+ O t%pedef struct a aT%peP aT%pe someUariableP struct a O int &P aT%pe 4bP QP Q Answer: >o Explanation: Ehen the declaration, t%pedef struct a aT%peP is encountered bod% of struct a is not :no.n. This is :no.n as Mincomplete t%pesN. $oid main)+ O printf)Zsi*eof )$oid 4+ , %d SnZ, si*eof) $oid 4++P printf)Zsi*eof )int 4+ , %d Sn[, si*eof)int 4++P printf)Zsi*eof )double 4+ , %d Sn[, si*eof)double 4++P printf)Zsi*eof)struct un:no.n 4+ , %d Sn[, si*eof)struct un:no.n 4++P Q
Freshersworld.com Resource Center

7((+

7(R+

First Job. Dream Job. Freshersworld.com Answer : si*eof )$oid 4+ , si*eof )int 4+ , si*eof )double 4+ , si*eof)struct un:no.n 4+ , Explanation: The pointer to an% t%pe is of same si*e. 7('+ char input#tring27<<3 , O<QP To get string input from the :e%board .hich one of the follo.ing is better? 7+ gets)input#tring+ -+ fgets)input#tring, si*eof)input#tring+, fp+ Answer & Explanation: The second one is better because gets)input#tring+ doesnGt :no. the si*e of the string passed and so, if a $er% big input )here, more than 7<< chars+ the charactes .ill be .ritten past the input string. Ehen fgets is used .ith stdin performs the same operation as gets but is safe. Ehich $ersion do %ou prefer of the follo.ing t.o, 7+ printf)Z%s[,str+P // or the more curt one -+ printf)str+P Answer & Explanation: refer the first one. If the str contains an% format characters li:e %d then it .ill result in a subtle bug. $oid main)+ O int i,7<, Y,-P int 4ip, Ji, 4Yp , JYP int : , 4ip/4YpP printf)Z%d[,:+P Q Answer: Compiler Wrror/ Z8ne&pected end of file in comment started in line C[. Explanation: The programmer intended to di$ide t.o integers, but b% the Zma&imum munch[ rule, the compiler treats the operator seVuence / and 4 as /4 .hich happens to be the starting of comment. To force .hat is intended b% the programmer, int : , 4ip/ 4YpP // gi$e space e&plicit% separating / and 4 //or int : , 4ip/)4Yp+P // put braces to force the intention .ill sol$e the problem.

7(=+

7R<+

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com 7R7+ $oid main)+ O char chP for)ch,<PchA,7-RPch+++ printf)Z%c %d SnZ, ch, ch+P Q Answer: Implementaion dependent Explanation: The char t%pe ma% be signed or unsigned b% default. If it is signed then ch++ is e&ecuted after ch reaches 7-R and rotates bac: to ?7-'. Thus ch is al.a%s smaller than 7-R. Is this code legal? int 4ptrP ptr , )int 4+ <&;<<P Answer: Xes Explanation: The pointer ptr .ill point at the integer in the memor% location <&;<<. main)+ O char a2;3,06WII"0P printf)0%s0,a+P Q Answer: Compiler error/ Too man% initiali*ers Explanation: The arra% a is of si*e ; but the string constant reVuires ( b%tes to get stored. 7R;+ main)+ O char a2;3,06WII0P printf)0%s0,a+P Q Answer: 6WII%aB`aBa???a``B Explanation: The character arra% has the memor% Yust enough to hold the string Z6WII[ and doesnt ha$e enough space to store the terminating null character. #o it prints the 6WII correctl% and continues to print garbage $alues till it accidentall% comes across a >8II character. 7RC+ main)+
Freshersworld.com Resource Center

7R-+

7RD+

First Job. Dream Job. Freshersworld.com O int a,7<,4YP $oid 4:P Y,:,JaP Y++P :++P printf)0Sn %u %u 0,Y,:+P Q Answer: Compiler error/ Cannot increment a $oid pointer Explanation: Uoid pointers are generic pointers and the% can be used onl% .hen the t%pe is not :no.n and as an intermediate address storage t%pe. >o pointer arithmetic can be done on it and %ou cannot appl% indirection operator )4+ on $oid pointers. 7R(+ main)+ O O O Q printf)0%d0,i+P Q printf)0%d0,i+P Q int iP 7RR+ rintf can be implemented b% using HHHHHHHHHH list. Answer: Uariable length argument lists 7R'+ char 4some9un)+ O char 4temp , Zstring constant0P return tempP Q int main)+ O puts)some9un)++P Q Ans.er/ string constant Explanation/ e&tern int iP int i,-<P const $olatile unsigned i,D<P printf)0%d0,i+P

Freshersworld.com Resource Center

First Job. Dream Job. Freshersworld.com The program suffers no problem and gi$es the output correctl% because the character constants are stored in code/data area and not allocated in stac:, so this doesnNt lead to dangling pointers. 7R=+ char 4some9un7)+ O char temp2 3 , Zstring0P return tempP Q char 4some9un-)+ O char temp2 3 , OMsN, MtN,NrN,NiN,NnN,NgNQP return tempP Q int main)+ O puts)some9un7)++P puts)some9un-)++P Q Ans.er/ 5arbage $alues. Explanation/ 1oth the functions suffer from the problem of dangling pointers. In some9un7)+ temp is a character arra% and so the space for it is allocated in heap and is initiali*ed .ith character string Zstring[. This is created d%namicall% as the function is called, so is also deleted d%namicall% on e&iting the function so the string data is not a$ailable in the calling function main)+ leading to print some garbage $alues. The function some9un-)+ also suffers from the same problem but the problem can be easil% identified in this case.

Freshersworld.com Resource Center

You might also like