C Questions and Answers
Short Description
Download C Questions and Answers...
Description
Mid-Semester Examination 1. main() { int i =1; switch(i) { i++; case 1 : printf (""); break; case 2 : printf(""); break; default: printf(""); break; } } Which case will be executed here? a. Syntax error b. Case 1 c. Case 2 d. default case Any statement which is not part of any case is called unreachable statement and it will never gets executed. Such statements will be ignored by the compiler and no error or warning gets generated. ANS: b 2. main( ) { unsigned int i=3; while( i >=0) printf( "%d", i--); } how many times will the printf statement be executed? a.0 b.3 c.4 d. infinite As 'i' is declared as unsigned integer it never gets decremented below '0' and the loop never terminates. ANS: d, infinite. 3. main( ) { int x,y, z; x=2; y=5; z= x+++y; printf("%d %d %d", x, y z); } Output of the program will be : a) 3 5 7 b) 2 6 8 c) Compiler Error d) Runtime Error
z = x+++y; gets expanded as z = x++ + y; now z will be 7 and then x gets incremented to 3. ANS: a 4. Which of the following can be used to print % to the screen? a) printf("\%"); b)printf("\\%"); c)printf("%%"); d) Both a) and c) %% can be used specifically. But \% works in most cases with gcc (not always). ANS: c 5. In C, x-=y+1 how will u represent it.. a) x=x-y-1 b) x = -x-y-1 c) x=-x+y+1
d) x = x-y+1
This short hand operator ( -= ) will be expanded as follows x = x - (y+1) which is equivalent to x = x-y-1 ANS: a 6. printf("%u",-1) What is the value printed here? a) -1 b) 1 c) (MAX of unsigned int) - 1
d) MAX of unsigned int
-1 is internally represented in binary format as All '1' (2's complement notation). When all '1' is converted into unsigned integer will give us max value of unsigned integer. ANS: d 7. main() { printf("he llo"); } What is the expected outcome of this program? a) he b) hello c) llo d) compilation error llo Syntax error. We cannot break a string across sentences. If you want to break it then do it this way, printf("he \ llo"); ANS: d 8. main() { int i=-1, j=-1, k=0, l=2, m; m = i++ && j++ && k++ || l++; printf("%d %d %d %d %d %d\n", i, j, k, l, m, ~(~0i; printf("%d",x); } Answer:
2 Explanation: above all statements form a double circular linked list; abc.next->next->prev->next->i this one points to "ghi" node the value of at particular node is 2. 96. struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); } Answer: origin is(0,0) origin is(0,0) Explanation: pp is a pointer to structure. We can access the elements of the structure either with arrow mark or with indirection operator. Note: Since structure point is globally declared x & y are initialized as zeroes 97. main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); } Answer: 9 Explanation: return(i++) it will first return i and then increments. i.e. 10 will be returned. 98. main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++;
printf("%p...%p...%p",p,q,r); } Answer: 0001...0002...0004 Explanation: ++ operator when applied to pointers increments address according to their corresponding data-types. 99. main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c
View more...
Comments