Anda di halaman 1dari 45

1. What will be output when you will execute following c code? #include<stdio.h> void main(){ printf("%d\t",sizeof(6.

5)); printf("%d\t",sizeof(90000)); printf("%d",sizeof('A')); } Choose all that apply: (A) 4 2 1 (B) 8 2 1 (C) 4 4 1 (D) 8 4 1 (E) 8 4 2 Answer:E 2. Array is also called......? A)Multistoragge variable B)Continous storage c)Heap Variable D)Random acsess stucture Answer:D 3.

Which of the following is not modifier of data type in c? (A) (B) (C) (D) (E) extern interrupt huge register All of these are modifiers of data type

Answer:E 4. What will be output when you will execute following c code? #include<stdio.h> void main(){ const int *p; int a=10; p=&a; printf("%d",*p); } Choose all that apply: (A) 0 (B) 10 (C) Garbage value (D) Any memory address (E) Error: Cannot modify const object Answer:B 5. Which of the following is integral data type? (A) (B) (C) (D) void char float double

(E)

None of these

Answer:E 6. What will be output if you will execute following c code? #include<stdio.h> void main(){ long double a; signed char b; int arr[sizeof(!a+b)]; printf("%d",sizeof(arr)) } (A) (B) (C) (D) (E) 8 9 1 4 Compilation error

Answer:D 7. What will be output when you will execute following c code? #include<stdio.h> void main(){ char a=250; int expr; expr= a+ !a + ~a + ++a; printf("%d",expr); } Choose all that apply: (A) 249 (B) 250 (C) 0 (D) -6

(E) Compilation error Answer:D 8. What will be output when you will execute following c code? #include<stdio.h> void main(){ int a= sizeof(signed) +sizeof(unsigned); int b=sizeof(const)+sizeof(volatile); printf("%d",a+++b); } Choose all that apply: (A) 10 (B) 9 (C) 8 (D) Error: Cannot find size of modifiers (E) Error: Undefined operator +++ Answer:C 9. What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=-5; unsigned int b=-5u; if(a==b) printf("Avatar"); else printf("Alien"); } Choose all that apply: (A) Avatar (B) Alien (C) Run time error

(D) Error: Illegal assignment (E) Answer:A 10. Consider on following declaration in c: (i)short const register i=10; (ii)static volatile const int i=10; (iii)unsigned auto long register i=10; (iv)signed extern float i=10.0; Choose correct one: (A) Only (iv)is correct (B) Only (ii) and (iv) is correct (C) Only (i) and (ii) is correct (D) Only (iii) correct (E) All are correct declaration Answer:C 11. What will be output when you will execute following c code? #include<stdio.h> void main(){ if(!printf("Mukesh Ambani")) if(printf(" Lakashmi Mittal")); } Choose all that apply: (A) (B) (C) (D) (E) Answer:A 12. Mukesh Ambani Lakashmi Mittal It will print nothing Mukesh Ambani Lakashmi Mittal Compilation error: if statement without body

What will be output when you will execute following c code? #include<stdio.h> void main(){ int a=5,b=10; if(++a||++b) printf("%d %d",a,b); else printf("John Terry"); } Choose all that apply: (A) (B) (C) (D) (E) Answer:C 5 10 6 11 6 10 5 11 John Terry

13. What will be output when you will execute following c code? #include<stdio.h> union group{ char xarr[2][2]; char yarr[4]; }; void main(){ union group x={'A','B','C','D'}; printf("%c",x.xarr[x.yarr[2]-67][x.yarr[3]-67]); } Choose all that apply: (A) A (B) B (C) C (D) D (E) Compilation error

Answer:B 14. What will be output if you will execute following c code? #include<stdio.h> void main(){ char arr[20]="MysticRiver"; printf("%d",sizeof(arr)); } (A) 20 (B) 11 (C) 12 (D) 22 (E) 24 Answer:A 15. What will be output when you will execute following c code? #include<stdio.h> #define WWW -1 enum {cat,rat}; void main(){ int Dhoni[]={2,'b',0x3,01001,'\x1d','\111',rat,WWW}; int i; for(i=0;i<8;i++) printf(" %d",Dhoni[i]); } Choose all that apply: (A) 2 98 4 513 28 73 1 -1 (B) 2 98 4 507 29 73 2 -1 (C) 2 99 3 513 29 73 2 -1 (D) 2 98 3 513 29 73 1 -1 (E) Compilation error Answer:D 16 . What is pointer?

A)TYPE OF ARRAY B)A variable to store data C)A variable to storeC address of a variable D)Not applicable in C language Answer: 17. Is this valid? int a,b; 2=a; A) Valid,data is stored B) Valid,no error C) Invalid,illegal assignment D) Invalid,b is not used Answer:C 18. What will be output if you will compile and execute the following c code? struct marks{ int p:3; int c:3; int m:2; }; void main(){ struct marks s={2,-6,5}; printf("%d %d %d",s.p,s.c,s.m); } (a) 2 -6 5 (b) 2 -6 1 (c) 2 2 1 (d) Compiler error (e) None of these Answer: (c)

19.What will be output if you will compile and execute the following c code? void main(){

int huge*p=(int huge*)0XC0563331; int huge*q=(int huge*)0xC2551341; *p=200; printf("%d",*q); } (a)0 (b)Garbage value (c)null (d) 200 (e)Compiler error Answer: (d) 20. What will be output if you will compile and execute the following c code? void main(){ int i; double a=5.2; char *ptr; ptr=(char *)&a; for(i=0;i<=7;i++) printf("%d ",*ptr++); } (a) -51 -52 -52 -52 -52 -52 20 64 (b) 51 52 52 52 52 52 20 64 (c) Eight garbage values. (d) Compiler error (e) None of these Answer: (a) 21. What will be output if you will compile and execute the following c code? void main(){ printf("%s","c" "question" "bank"); } (a) c question bank (b) c (c) bank

(d) cquestionbank (e) Compiler error Answer: (d) 22. What will be output if you will compile and execute the following c code? void main(){ char *str="c-pointer"; printf("%*.*s",10,7,str); } (a) c-pointer (b) c-pointer (c) c-point (d) cpointer null null (e) c-point Answer: (e) 23. What will be output if you will compile and execute the following c code? void main(){ int a=-12; a=a>>3; printf("%d",a); } (a) -4 (b) -3 (c) -2 (d) -96 (e) Compiler error Answer :( c)

24. What will be output if you will compile and execute the following c code?

#include "string.h" void main(){ clrscr(); printf("%d %d",sizeof("string"),strlen("string")); getch(); } (a) 6 6 (b) 7 7 (c) 6 7 (d) 7 6 (e) None of these Answer: (d) 25. What will be output if you will execute following c code? #include<stdio.h> void main(){ char arr[11]="The African Queen"; printf("%s",arr); } (A) (B) (C) (D)# (E) The African Queen The Queen null Compilation error

26. What will be output if you will compile and execute the following c code? void main(){ static main; int x; x=call(main); clrscr(); printf("%d ",x); getch(); } int call(int address){ address++;

return address; } (a) 0 (b) 1 (c) Garbage value (d) Compiler error (e) None of these Answer: (b) 27. What will be output if you will compile and execute the following c code? int extern x; void main() printf("%d",x); x=2; getch(); } int x=23; (a) 0 (b) 2 (c) 23 (d) Compiler error (e) None of these Answer: (c) 28. What will be output if you will compile and execute the following c code? void main(){ int i=0; if(i==0){ i=((5,(i=3)),i=1); printf("%d",i); } else printf("equal"); } (a) 5

(b) 3 (c) 1 29. What will be output if you will compile and execute the following c code? void main(){ int a=25; clrscr(); printf("%o %x",a,a); getch(); } (a) 25 25 (b) 025 0x25 (c) 12 42 (d) 31 19 (e) None of these Answer: (d) 30. What will be output if you will compile and execute the following c code? #define message "union is\ power of c" void main(){ clrscr(); printf("%s",message); getch(); } (a) union is power of c (b) union ispower of c (c) union is Power of c (d) Compiler error (e) None of these Answer: (b) 31. What will be output if you will execute following c code?

#include<stdio.h> void main(){ char arr[7]="Network"; printf("%s",arr); } (A) (B) (C) (D)# (E) Network N network Garbage value Compilation error

32. What will be output if you will compile and execute the following c code? void main(){ if(printf("cquestionbank")) printf("I know c"); else printf("I know c++"); } (a) I know c (b) I know c++ (c) cquestionbankI know c (d) cquestionbankI know c++ (e) Compiler error Answer: (c) (d) equal (e) None of above Answer: (c) 33. What will be output if you will execute following c code? #include<stdio.h> void main(){ int xxx[10]={5}; printf("%d %d",xxx[1],xxx[9]); } (A) 0 5

(B) (C) (D)# (E)

5 5 5 0 0 0 Compilation error

Predict the output or error(s) for the following(34 TO 40)


34. struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; 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. 35. 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 36. 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.

37. 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. 38. main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; } Answer: Compiler error Explanation: declaration of convert and format of getc() are wrong.

39. main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } Answer: Compiler error. Explanation: argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values. 40. # include int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); } Answer: garbage value Explanation: ptr pointer is pointing to out of the array range of one_d. Predict the output or error(s) for the following: 41. main()

{ int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } Answer: 41 Explanation: The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expressions truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same. Similarly when && operator is involved in an expression, when any of the operands become false, the whole expressions truth value becomes false and hence the remaining expression will not be evaluated. false && (anything) => false where (anything) will not be evaluated. 42. main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); } Answer: Compier Error: '&' on register variable Rule to Remember: & (address of ) operator cannot be applied on register variables. 43. { main() float i=1.5;

switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Answer: Compiler Error: switch expression not integral Explanation: Switch statements can be applied only to integral types. 44. main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } } Answer: Linker Error : Unresolved external symbol i Explanation: The identifier i is available in the inner block and so using extern has no use in resolving it. 45. main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

Answer: 16 16 16 Explanation: f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately affects only the value of a. 46. { main()

char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); } Answer: sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4 sizeof(a) = 5, strlen(a) = 4 Explanation: sizeof(p) => sizeof(char*) => 2 sizeof(*p) => sizeof(char) => 1 Similarly, sizeof(a) => size of the character array => 5 When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account. 47. #define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(The dimension of the array is %d, DIM(arr, int)); } Answer:

10 Explanation: The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to sizeof(arr)/sizeof(int) => 10 * sizeof(int) / sizeof(int) => 10. 48. int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(The dimension of the array is %d, DIM(arr)); } Answer: 1 Explanation: Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the argument is equivalent to int * array (this is one of the very few places where [] and * usage are equivalent). The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be equal in this case. 49. { main() static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

Answer: 1 2 3 4 5 6 7 8 9 Explanation: *(*(p+i)+j) is equivalent to p[i][j]. Predict the output or error(s) for the following: 50. main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; } Answer: x=10 y=8 Explanation: Using ^ like this is a way to swap two variables without using a temporary variable and that too in a single statement. Inside main(), void swap(); means that swap is a function that may take any number of arguments (not no arguments) and returns nothing. So this doesnt issue a compiler error by the call swap(&x,&y); that has two arguments. This convention is historically due to pre-ANSI style (referred to as Kernighan and Ritchie style) style of function declaration. In that style, the swap function will be defined as follows, 1 4 7 2 5 8 3 6 9 1 2 3 4 5 6 7 8 9 1 4 7 2 5 8 3 6 9

void swap() int *a, int *b { *a ^= *b, *b ^= *a, *a ^= *b; } where the arguments follow the (). So naturally the declaration for swap will look like, void swap() which means the swap can take any number of arguments. 51. { main() int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 11 Explanation: The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed. 52. { main() int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 21 Explanation: The integer value 257 can be represented in binary as, 00000001 00000001. Remember that the INTEL machines are small-endian machines. Small-endian means that the lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses. The integer value 258 is stored in memory as: 00000001 00000010.

53. {

main() int i=300; char *ptr = &i; *++ptr=2; printf("%d",i);

} Answer: 556 Explanation: The integer value 300 in binary notation is: 00000001 00101100. It is stored in memory (small-endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010 00101100 => 556. 54. #include main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptrprintf("%d",least); } Answer: 0 Explanation: After ptr reaches the end of the string the value pointed by str is \0. So the value of str is less than that of least. So the value of least finally is 0. 55. Declare an array of N pointers to functions returning pointers to functions returning pointers to characters? Answer:

(char*(*)( )) (*ptr[N])( ); 56. main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } Answer: Compiler Error: Undefined structure date Explanation: Inside the struct definition of student the member of type struct date is given. The compiler doesnt have the definition of date structure (forward reference is not allowed in C in this case) so it issues an error. 57. main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

Answer: Compiler Error: Undefined structure date Explanation: Only declaration of struct date is available inside the structure definition of student but to have a variable of type struct date the definition of the structure is required. 58. There were 10 records stored in somefile.dat but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(somefile.dat,r); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } } Explanation: fread reads 10 records and prints the names successfully. It will return EOF only when fread tries to read another record and fails reading EOF (and returning EOF). So it prints the last record again. After this only the condition feof(fp) becomes false, hence comes out of the while loop.

Preprocessor questions

59. #define max value 10 void main() { int a=60; if(a/max value==6) printf("equal"); else printf("not equal"); } 60. What will be output if you compile and execute the above code? (a)equal (b)not equal (c)Run time error (d)Compiler error (2) 61. #define num int long void main() {

num a=0;

printf("%d,%d,%d",a++,sizeof a++,sizeof(a++));

} 62. What will be output if you compile and execute the above code? (a)3 2 4 (b)0 0 4 (c)0 4 4 (d)Compiler error

63. #define short int long void main() {

printf("%d",sizeof(short));

} 64. What will be output if you compile and execute the above code?

(a)2 (b)4 (c)8 (d)Compiler error (4) 65. #define float char void main() { float f=255;

printf("%d",sizeof(f++));

} 66. What will be output if you compile and execute the above code? (a)1 (b)2 (c)8 (d)Compiler error

67. void main() { char f=255.0;

printf("%d",sizeof(f++));

} What will be output if you compile and execute the above code? (a)1 (b)8 (c)10 (d)Compiler error

68. void main() { char f=255; f++;

printf("%d",sizeof(f));

} 69. What will be output if you compile and execute the above code? (a)1 (b)2 (c)4 (d)Compiler error 70. #define value 10\2 void main() {

printf("%d",value);

} 71. What will be output if you compile and execute the above code? (a)5 (b)20 (c)102

(d)Compiler error 72. #define xxx 11\3 void main() {

printf("%ld",xxx);

} 73. What will be output if you compile and execute the above code? (a)11 (b)3 (c)113 (d)Compiler error

74. #define option1 a++; printf("%d",a); #define option2 print("%d",a); void main() {

int a=10;

if(a++) option1 else option2;

} 75. What will be output if you compile and execute the above code? (a)10 (b)11 (c)12 (d)Compiler error 76. //test.c void main() {

printf("%s",__FILE__);

77. What will be output if you compile and execute the above code? (a)null (b)test.c (c)url of current working directory (d)Compiler error 78. #include"stdio.h" void main() {

#ifdef __stdio_h prinrf("defined"); #else printf("not defined"); #endif

} 79. What will be output if you compile and execute the above code? (a)defined (b)not defined (c)Run time error (d)Compiler error 80. #include"stdio.h" void main() { const int a=1;

#if 5+~5+1 printf("%d",a+1); #elif 7 printf("%d",a+2); #else printf("%d",a+3); #endif

81. What will be output if you compile and execute the above code? (a)2 (b)3 (c)4 (d)Compiler error 82. #define conio.h #include "stdio.h" #define max 0\5 void main() {

#ifndef __conio_h #define conio.h printf("%s",__TIME__); #else #undef __conio_h

printf("%s",__DATE__); #endif }

83. What will be output if you compile and execute the above code? (a)Output will be date of compilation (b)Output will be time of compilation (c)Output will be both time of compilation and date of compilation (d)Compiler error 84. #define max 5 #define a max*max #define value a\a void main() { const int aa=5;

printf("%d",value+aa);

85. What will be output if you compile and execute the above code? (a)5 (b)6 (c)10 (d)Compiler error (15) #define function(a,b) a##b void main() {

printf("%d",function(5,2));

87. What will be output if you compile and execute the above code? (a)10

(b)2 (c)52 (d)Compiler error 88. #define find(a,b,c) #a#b#c void main() { int a=10; int b=20; int c=30;

printf("%s",find(a,b,c)+1);

} 89. What will be output if you compile and execute the above code? (a)102030 (b)02030 (c)bc

(d)Compiler error 90. #define swap(x,y) x^=y^=x^=y void main() { int a=10,b=20; swap(a,b);

printf("%d %d" ,a,b);

91. What will be output if you compile and execute the above code? (a)10 20 (b)20 10 (c)0 10 (d)Compiler error 92. #define int long #define cal(x,y,z) x*y*z

void main() { int a=2; int b=cal(a++,a++,a++);

printf("%d,%d" ,a,b);

93. What will be output if you compile and execute the above code? (a)5,0 (b)5,125 (c)5,60 (d)Compiler error 94. #define final(a,b,c) ++a+ ++b+ ++c void main() {

printf("%d" ,final(1,2,3));

95. What will be output if you compile and execute the above code? (a)6 (b)9 (c)10 (d)Compiler error 96. void one(); void two(); #pragma startup one 2 #pragma startup two 1 void main() { printf("main ");

} void one()

{ printf("one "); } void two() { printf("two "); } What will be output if you compile and execute the above code? (a)main one two (b)one main two (c)main (d)two main one

Answer of preprocessor questions: 1. 2. 3. 4. 5. (d) (b) (b) (a) (a)

6. 7. 8. 9.

(a) (c) (c) (d)

10. (b) 11. (b) 12. (b) 13. (b) 14. (c) 15. (c) 16. (c) 17. (b) 18. (a) 19. (d) 20. (d)

Anda mungkin juga menyukai