You are on page 1of 38

C - AVERAGE QUESTIONS

1. #include<stdio.h> int main() { extern int a; printf("%d\n", a); return 0; } int a=20; A. extern int a is declaration, int a = 20 is the definition B. int a = 20 is declaration, extern int a is the definition C. int a = 20 is definition, a is not defined D. a is declared, a is not defined Ans. A EXP. - During declaration we tell the datatype of the Variable. - During definition the value is initialized. 2. which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ? A. rem = 3.14 % 2.1; B. rem = modf(3.14, 2.1); C. rem = fmod(3.14, 2.1); D. Remainder cannot be obtain in floating point division. Ans. C EXP. fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator. But fmod() performs floating point divisions. 3. Which of the following statement obtains the remainder on dividing 5.5 by 1.3 ? A. rem = (5.5 % 1.3) B. rem = modf(5.5, 1.3) C. rem = fmod(5.5, 1.3) D. Error: we can't divide Ans. C EXP. fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator. But fmod() performs floating point divisions. 4. The binary equivalent of 5.375 is A. 101.101110111

B. 101.011 C. 101011 D. None of above Ans. B EXP. --5. What is (void*)0? A. Representation of NULL pointer B. Representation of void pointer C. Error D. None of above Ans. A EXP. Representation of NULL pointer 6. How many bytes are occupied by near, far and huge pointers (DOS)? A. near=2 far=4 huge=4 B. near=4 far=8 huge=8 C. near=2 far=4 huge=8 D. near=4 far=8 huge=8 Ans. A EXP. near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long. 7. What is the purpose of fflush() function. A. flushes all streams and specified streams. B. flushes only specified stream. C. flushes input/output buffer. D. flushes file buffer. Ans. A EXP. "fflush()" flush any buffered output associated with filename, which is either a file opened for writing or a shell command for redirecting output to a pipe or coprocess. Example: fflush(FilePointer); fflush(NULL); flushes all streams. 8. What will the function randomize() do? A. returns a random number. B. returns a random number generator in the specified range. C. returns a random number generator with a random value based on time. D. return a random number with a given seed value. Ans. C EXP. he randomize() function initializes the random number generator with a random value based on time. You can try the sample program given below in Turbo-C, it may not work as expected in other compilers.

9. Which standard library function will you use to find the last occurance of a character in a string in C? A. strnchar() B. strchar() C. strrchar() D. strrchr() Ans. D EXP. --10. What is the notation for following functions? 1. int f(int a, float b) { /* Some code */ } 2. int f(a, b) int a; float b; { /* Some code */ } A. 1. KR Notation 2. ANSI Notation B. 1.Pre ANSI C Notation 2. KR Notation C. 1. ANSI Notation 2. KR Notation D. 1. ANSI Notation 2. Pre ANSI Notation Ans. C EXP. 1. ANSI Notation 2. KR Notation 11. How many times the program will print "IndiaBIX" ? #include<stdio.h> int main() { printf("IndiaBIX"); main(); return 0; } A. Infinite times B. 32767 times

C. 65535 times D. Till stack doesn't overflow Ans. D EXP. A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing. A stack overflow occurs when too much memory is used on the call stack. Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error. 12. In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). what will str contain? A. "I am a boy\r\n\0" B. "I am a boy\r\0" C. "I am a boy\n\0" D. "I am a boy" Ans. C EXP. Declaration: char *fgets(char *s, int n, FILE *stream); fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first. Therefore, the string str contain "I am a boy\n\0" 13. On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"? #include<stdio.h> int main() { int i, fss; char ch, source[20] = "source.txt", target[20]="target.txt", t; FILE *fs, *ft; fs = fopen(source, "r"); ft = fopen(target, "w"); while(1) { ch=getc(fs); if(ch==EOF) break; else { fseek(fs, 4L, SEEK_CUR);

fputc(ch, ft); } } return 0; } A. r n B. Trh C. err D. None of above Ans. B EXP. he file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human". Inside the while loop, ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF. if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops. If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt. fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt. The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file. 14. Which files will get closed through the fclose() in the following program? #include<stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("A.C", "r"); fs = fopen("B.C", "r"); ft = fopen("C.C", "r"); fclose(fp, fs, ft); return 0; } A. "A.C" "B.C" "C.C" B. "B.C" "C.C"

C. "A.C" D. Error in fclose() Ans. C EXP. Here the file A.C alone closed. 15. Consider the following program and what will be content of t? #include<stdio.h> int main() { FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0; } A. size of "DUMMY.C" file B. The handle associated with "DUMMY.C" file C. Garbage value D. Error in fileno() Ans. B EXP. fileno is a macro that returns the file handle for the stream. fp = fopen("DUMMY.C", "w"); A file DUMMY.C is opened in write mode and returns the file pointer to fp t = fileno(fp); returns the handle for the fp stream and it stored in the variable t printf("%d\n", t); It prints the handle number. 16. What will be the output of the program? #include<stdio.h> #include<stdarg.h> void fun1(int num, ....); void fun2(int num, ....); int main() { fun1(1, "Apple", "Boys", "Cats", "Dogs"); fun2(2, 12, 13, 14); return 0;

} void fun1(int num, ...) { char *str; va_list ptr; va_start(ptr, num); str = va_arg(ptr, char *); printf("%s", str); } void fun2(int num, ...) { va_list ptr; va_start(ptr, num); num = va_arg(ptr, int); printf("%d", num); } A. Dogs 12 B. Cats 14 C. Boys 13 D. Apple 12 Ans. D EXP. First Apple gets printed followed by 12 17. What will be the output of the program? #include<stdio.h> #include<stdarg.h> void dumplist(int, ...); int main() { dumplist(2, 4, 8); dumplist(3, 6, 9, 7); return 0; } void dumplist(int n, ...) { va_list p; int i; va_start(p, n); while(n -> 0) { i = va_arg(p, int); printf("%d", i); }

va_end(p); } A. 2 4 36 B. 2 4 8 3, 6, 9, 7 C. 4 8 697 D. 1 1 1 1111 Ans. C ExP. 4 8 697 18. Which of the following is the correct usage of conditional operators used in C? A. a>b ? c=30 : c=40; B. a>b ? c=30; C. max = a>b ? a>c?a:c:b>c?b:c D. return (a>b)?(a:b) Ans. C EXP. Option A: assignment statements are always return in paranthesis in the case of conditional operator. It should be a>b? (c=30):(c=40); Option B: it is syntatically wrong. Option D: syntatically wrong, it should be return(a>b ? a:b); Option C: it uses nested conditional operator, this is logic for finding greatest number out of three numbers. 19. What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile? #include<stdio.h> #define SWAP(a, b, c)(c t; t=a, a=b, b=t) int main() { int x=10, y=20; SWAP(x, y, int); printf("%d %d\n", x, y); return 0; } A. It compiles

B. Compiles with an warning C. Not compile D. Compiles and print nothing Ans. C EXP. C t cannot be treated as int t 20. In which stage the following code #include<stdio.h> gets replaced by the contents of the file stdio.h A. During editing B. During linking C. During execution D. During preprocessing Ans. D EXP. The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More precisely, the entire text of the file 'stdio.h' replaces the #include directive. 21. What will be the output of the program? #include<stdio.h> int fun(int **ptr); int main() { int i=10, j=20; const int *ptr = &i; printf("i = %5X", ptr); printf("ptr = %d", *ptr); ptr = &j; printf("j = %5X", ptr); printf("ptr = %d", *ptr); return 0; } A. i= FFE2 ptr=12 j=FFE4 ptr=24 B. i= FFE4 ptr=10 j=FFE2 ptr=20 C. i= FFE0 ptr=20 j=FFE1 ptr=30 D. Garbage value Ans. B EXP. ---22. what do the following declaration signify? int *f();

A. f is a pointer variable of function type. B. f is a function returning pointer to an int. C. f is a function pointer. D. f is a simple declaration of pointer variable. Ans. B EXP. f is a function returning pointer to an int. 23. What do the following declaration signify? char **argv; A. argv is a pointer to pointer. B. argv is a pointer to a char pointer. C. argv is a function pointer. D. argv is a member of function pointer. Ans. B EXP. argv is a pointer to a char pointer. 24. What do the following declaration signify? int (*ptr)[30]; A. ptr is a pointer to an array of 30 integer pointers. B. ptr is a array of 30 integer function pointer. C. ptr is a array of 30 integer pointers. D. ptr is a array 30 pointers. Ans. A EXP. ptr is a pointer to an array of 30 integer pointers. 25. what do the following declaration signify? void (*cmp)(); A. cmp is a pointer to an void function type. B. cmp is a void type pointer function. C. cmp is a function that return a void pointer. D. cmp is a pointer to a function which returns void . Ans. D ANS. cmp is a pointer to a function which returns void . 26. Declare the following statement? "A pointer to a function which receives nothing and returns nothing". A. void *(ptr)*int; B. void *(*ptr)() C. void *(*ptr)(*) D. void (*ptr)()

Ans. D EXP. void (*ptr)() 27. main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } } A. 2 2 2 2 2 2 3 4 6 5 B. 2 2 2 2 2 2 3 3 6 6 C. 2 a a 3 2 2 4 4 6 5 D. Cannot convert char* to int Ans. A EXP. Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed. 28. main() { extern int i; i=20; printf("%d",i); } A. 20 B. 23654 C. Linker Error : Undefined symbol '_i' D. Compiler error : should be declare outside main() Ans. C EXP. extern storage class in the following declaration, extern int i; specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred . 29. main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

A. 2 2 B. 1 2 C. 2 1 D. 2 2 Ans. B EXP. The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2. 30. main() { printf("%x",-1<<4); } A. ffe0 B. fff0 C. 8 D. 4 Ans. B EXP. -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value. 31. main() { int c=- -2; printf("c=%d",c); } A. c=0 B. c=-2 C. c=2 D. c=-4 Ans. C EXP. Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable. 32. #define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); } A. sizeof(i)=2

B. sizeof(i)=1 C. sizeof(i)=65 D. sizeof(i)=4 Ans. B EXP. Since the #define replaces the string int by the macro char 33. main() { int i=10; i=!i>14; Printf ("i=%d",i); } A. i=0 B. i=1 C. i=10 D. i=14 Ans. A EXP. In the expression !i>14 , NOT (!) operator has more precedence than > symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero). 34. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } A. 63 B. 93 C. 77 D. 104 Ans. C EXP. p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 32), we get 77("M"); So we get the output 77 :: "M" (Ascii is 77). 35. #include<stdio.h>

main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); } A. 10----10 B. 10 2----5 6 C. 10 2 3 4----5 6 7 8 D. Garbage Value Ans. C EXP. p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array. 36. #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } A. Pre-processor directive cannot overwrite system function B. linker error C. 100 D. GARBAGE VALUE Ans. C EXP. Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this : main() { 100; printf("%d\n",100); } 37. void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); } A. 4..2 B. 8..4

C. 4..8 D. 2..4 Ans. A EXP. the second pointer is of char type and not a far pointer 38. main() { int i=400,j=300; printf("%d..%d"); } A. garbage value B. Compiler error C. 400..300 D. chsvcv@#$..chsvcv@#$ Ans. C EXP. printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage values. 39. main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } A. Compiler error: Undefined symbol label 'here'; B. 1 2 pp pp pp C. 1 2 PP PP PP D. 1 2 3 4 5 Ans. A Exp. Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main.

40. main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } A. pascal ada fortan cobal perl B. pascal ada cobal fortan perl C. Compiler error D. PascalPascalPascalPascalPascal Ans. C Exp. Array names are pointer constants. So it cannot be modified. 41. void main() { int i=5; printf("%d",i+++++i); } A. 11 B. 10 C. 5 D. compiler error Ans. D Exp. Side effects are involved in the evaluation of i 42. main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } A. 10 B. 1 C. 101 D. 110 Ans. B Exp. The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators. 43. #include<stdio.h> main() {

char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } A. M B. D C. A D. B Ans. A Exp. p is pointing to character '\n'.str1 is pointing to character 'a' ++*p meAnswer:"p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is incremented to 11. the value of ++*p is 11. ++*str1 meAnswer:"str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is added and result is subtracted from 32. i.e. (11+98-32)=77("M"); 44. main( ) { void *vp; char ch = g, *cp = goofy; int j = 20; vp = &ch; printf(%c, *(char *)vp); vp = &j; printf(%d,*(int *)vp); vp = cp; printf(%s,(char *)vp + 3); } A. goofy B. g C. gg00fy D. g20fy Ans. D Exp. Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is g. Similarly the output from second printf is 20. The third printf statement type casts it to print the string from the 4th value hence the output is fy. 45. main ( ) { static char *s[ ] = {black, white, yellow, violet}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;

p = ptr; **++p; printf(%s,*--*++p + 3); } A. ck B. c--y3 C. *--*ite D. compiler error Ans. A EXP. In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 1 = s . the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is ck. 46. main() { int i, n; char *x = girl; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(%s\n,x); x++; } } A. (blank space) irl rl l B. girl irl rl l C. girl D. irl rl l ANS. A Exp. Here a string (a pointer to char) is initialized with a value girl. The strlen function returns the length of the string, thus n has a value 4. The next statement assigns

value at the nth location (\0) to the first location. Now the string becomes \0irl . Now the printf statement prints the string after each iteration it increments it starting position. Loop starts from 0 to 4. The first time x[0] = \0 hence it prints nothing and pointer value is incremented. The second time it prints from x[1] i.e irl and the third time it prints rl and the last time it prints l and the loop terminates. 47. main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); } A. i=-1, +i=-1 B. undefined symbol +i c. declartion error D. -1-1 Ans. A EXP. Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator). 48. main() { main(); } A. infinite call of main B. L value required C. Run time error:stack overflow D. one time call of main Ans. C EXP. main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error. 49. main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); } A. 2 5 5 B. 1 1 1 C. 2 2 2 D. 4 4 4 Ans. A

EXP. In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one. 50. main() { char not; not=!2; printf("%d",not); } A. 1 B. 0 C. Compiler error(at line 4):cannot initilize D. 2 Ans. B EXP. ! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0. 51. int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } A. 30,20,10 B. 10,20,30 C. 30,30,30 D. Compiler error Ans. A EXP. '{' introduces new block and thus new scope. In the innermost block i is declared as, const volatile unsigned which is a valid declaration. i is assumed of type int. So printf prints 30. In the next block, i has value 20 and so printf prints 20. In the outermost block, i is declared as extern, so no storage space is allocated for it. After compilation is over the linker resolves it to global variable i (since it is the only variable visible there). So it prints i's value as 10.

52. main() { int *j; { int i=10; j=&i; } printf("%d",*j); } A. some address B. 10 C. Cannot convert int to int* D. 1 Ans. B EXP. The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives upto the exit of main function. Since the i is still allocated space, *j prints the value stored in i since j points i. 53. #include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); } A. Compiler error Explanation: i is a constant. you cannot change the value of constant B. 4 6.0 C. 4 5.0 D. 4 4.0 Ans. A EXP. i is a constant. you cannot change the value of constant 54. 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); } A. 2 B. 3 C. 4 D. 5 Ans. A EXP. 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. 55. 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); } A. origin is(0,0) origin is(0,0) B. origin is(1,0) origin is(0,1) C. origin is(0,1) origin is(0,1) D. origin is(1,1) origin is(0,1) Ans. A EXP. 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 56. main() { char *p;

int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } A. 0001...0002...0004 B. 0000...0002...0004 C. 0001...0009...0004 D. 0001...0002...0010 Ans. A EXP. ++ operator when applied to pointers increments address according to their corresponding data-types. 57. main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; } A. Compiler error B. Program Executed but no output C. program executed but with output D. None of the above Ans. A EXP. argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values. 58. int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p A. 4--0 3--1 2--2

B. 3--4 3--1 2--0 C. 1--2 2--3 3--4 D. 4--1 3--2 2--3 Ans. A EXP. Let us assume some x= scanf("%d",&i)-t the values during execution will be, t i x 4 0 -4 3 1 -2 2 2 0 59. main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } A. hello B. No output C. compiler design D. Linker error Ans. A EXP. The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed. 60. func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2;

{ return((*pf) (val1,val2)); } A. the value of process is 0! B. the value of process is 1! C. the value of process is 10! D. the value of process is 2! Ans. A EXP. The function 'process' has 3 parameters - 1, a pointer to another function 2 and 3, integers. When this function is invoked from main, the following substitutions for formal parameters take place: func for pf, 3 for val1 and 6 for val2. This function returns the result of the operation performed by the function 'func'. The function func has two integer parameters. The formal parameters are substituted as 3 for a and 6 for b. since 3 is not equal to 6, a==b returns 0. therefore the function returns 0 which in turn is returned by the function 'process'. 61. main(){ char a[100]; a[0]='a';a[1]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); } A. bd B. bc C. ab D. ad Ans. B EXP. The base address is modified only in function and as a result a points to 'b' then after incrementing to 'c' so bc will be printed. 62. void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); } A. 0 0 B. 0 65535 C. 0 garbage value

D. -4521 -4875 Ans. B EXP. the value of give it is 0 and --give it leads to -65535 and assigned to gotit. %u prints only signed numbers so ans is 0 65535 63. oid main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } A. Ok here B. forget it C. a ok here D. a forget it Ans. A EXP. Printf will return how many characters does it print. Hence printing a null character returns 1 which makes the if statement true, thus "Ok here" is printed. 64. void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); } A. linker error: cannot convert int* to void B. 2 C. Compiler Error. We cannot apply indirection on type void*. D. 4 Ans. C EXP. Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for, 1. Passing generic pointers to functions and returning such pointers. 2. As a intermediate pointer type. 3. Used when the exact pointer type will be known at a later point of time. 65. void main() { int i=i++,j=j++,k=k++; printf(%d%d%d,i,j,k); }

A. garbage value B. 012 C. 123 D. Runtime Error:i,j,k not assigned Ans. A EXP. An identifier is available to use in program code from the point of its declaration. So expressions such as i = i++ are valid statements. The i, j and k are automatic variables and so they contain some garbage value. Garbage in is garbage out (GIGO). 66. #include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); } A. 2 0 B. 2 2 C. Garbage-value 0 D. Garbage-value garbage-value Ans. C EXP. The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other words if(0) and so z goes uninitialized. Thumb Rule: Check all control paths to write bug free code. 67. main() { int a[10]; printf("%d",*a+1-*a+3); } A. Garbage value B. 1 C. 3 D. 4 Ans. D EXP. *a and -*a cancels out. The result is as simple as 1 + 3 = 4 ! 68. #define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); } A. 15 B. 10

C. 12 D. prod should have a prototype Ans. B EXP. The macro expands and evaluates to as: x+2*y-1 => x+(2*y)-1 => 10 69. main() { unsigned int i=65000; while(i++!=0); printf("%d",i); } A. 1 B. 65000,65001,.... C. 65000,65001,....65535 D. 65000,65001,....-65535 Ans. A EXP. Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1. 70. main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); } A. Line no 4: Error: Lvalue required Line no 6: Cannot apply leftshift to float Line no 8: Cannot apply mod to float B. Line no 5: Error: Lvalue required Line no 4: Cannot apply leftshift to float Line no 7: Cannot apply mod to float C. Line no 5: Error: Lvalue required Line no 6: Cannot apply leftshift to float Line no 7: Cannot apply mod to float D. Compiler error Ans. C EXP. Enumeration constants cannot be modified, so you cannot apply ++. Bit-wise operators and % operators cannot be applied on float values. fmod() is to find the modulus values for floats as % operator is for ints. 71. main() {

int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); } A. Compiler error: unknown type integer Compiler error: undeclared function write B. Compiler error: function Compiler error: undeclared function write C. Compiler error: unknown type integer Compiler error: undeclared i write D. Compiler error: unknown type float Compiler error: undeclared function write Ans. A EXP. Pascal keyword doesnt mean that pascal code can be used. It means that the function follows Pascal argument passing mechanism in calling the functions. 72. void pascal f(int i,int j,int k) { printf(%d %d %d,i, j, k); } void cdecl f(int i,int j,int k) { printf(%d %d %d,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); } A. 10 11 12 13 12 11 10 13 B. 10 11 20 13 12 11 10 13 C. Compiler error D. linker error Ans. A

EXP. Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left. 73. main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); } A. infinite loop B. 0,1,2....65535 C. 1,2,3....-65535 D. -128 Ans. A EXP. The difference between the previous question and this one is that the char is declared to be unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can come out of the for loop. 74. What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); } A. Compiler error: Multiple declaration for error B. Compiler error: enum cannot be typedef C. Compiler error: 1 cannot be assigned to g1 D. Compiler error: L value required Ans. A EXP. The name error is used in the two meanings. One means that it is a enumerator constant with value 1. The another use is that it is a type name (due to typedef) for enum errorType. Given a situation the compiler cannot distinguish the meaning of error to know in what sense the error is used: error g1; 75. #if something == 0 int some=0; #endif main() { int thing = 0;

printf("%d %d\n", some ,thing); } A. Compiler error : undefined symbol some B. 0 0 C. garbage value 0 D. L value required Ans. B EXP. This code is to show that preprocessor expressions are not the same as the ordinary expressions. If a name is not known the preprocessor treats it to be equal to zero. 76. #include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); } A. 1 B. 0 C. 3 D. error Ans. B EXP. 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. 77. What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++<n) p = &arr[i]; *p = 0; } A. Runtime Error B. Compiler Error C. Linking Error D. Syntax Error Ans. A EXP. If the body of the loop never executes p is assigned no address. So p remains NULL where *p =0 may result in problem (may rise to runtime error NULL pointer assignment and terminate the program).

78. #define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); } A. Runtime Error B. This statement becomes else for if in assert macro C. 100 D. No Output Ans. D EXP. The else part in which the printf is there becomes the else for if in the assert macro. Hence nothing is printed. The solution is to use conditional operator instead of if statement, #define assert(cond) ((cond)?(0): (fprintf (stderr, "assertion failed: \ %s, file %s, line %d \n",#cond, __FILE__,__LINE__), abort())) However this problem of matching with nearest else cannot be solved by the usual method of placing the if statement inside a block like this, #define assert(cond) { \ if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) \ } 79. Is the following code legal? typedef struct a { int x; aType *b; }aType A. Yes B. Syntax Error C. No D. Runtime Error Ans. C EXP. The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs). 80. Is the following code legal? typedef struct a aType;

struct a { int x; aType *b; }; A. Yes B. Syntax Error C. No D. Runtime Error Ans. A EXP. The typename aType is known at the point of declaring the structure, because it is already typedefined. 81. void main() { char ch; for(ch=0;ch<=127;ch++) printf(%c %d \n, ch, ch); } A. Compiler Error B. Type mismatch C. Implementation dependent D. Runtime Error Ans. C EXP. The char type may be signed or unsigned by default. If it is signed then ch++ is executed after ch reaches 127 and rotates back to -128. Thus ch is always smaller than 127. 82. main() { char a[4]="HELL"; printf("%s",a); } A. HELL B. HELL%@!~@!@???@~~! C. Compiler error D. Runtime error Ans. B EXP. The character array has the memory just enough to hold the string HELL and doesnt have enough space to store the terminating null character. So it prints the HELL correctly and continues to print garbage values till it accidentally comes across a NULL character. 83. Printf can be implemented by using __________ list. A. Hash list B. Dictionaries

C. Variable length argument lists D. enum Ans. C 84. char *someFun() { char *temp = string constant"; return temp; } int main() { puts(someFun()); } A. Compiler Eror B. String constant C. string constant D. Runtime Error Ans. C EXP. The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesnt lead to dangling pointers. 85. char *someFun1() { char temp[ ] = string"; return temp; } char *someFun2() { char temp[ ] = {s, t,r,i,n,g}; return temp; } int main() { puts(someFun1()); puts(someFun2()); } A. string B. s t r i n g C. Compiler Error D. Garbage values. Ans. D EXP. Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string string. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the

calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case. 86. int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); } A. x = 20 y=20 B. x = 10 y = 10 C. x = 10 y = 20 D. x = 20 y = 10 Ans. D EXP. This is one way of swapping two values. Simple checking will help understand this. 87. main() { while (strcmp(some,some\0)) printf(Strings are not equal\n); } A. No output B. strings are not equal\n C. syntax error D. runtime error Ans. A 88. main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } A. 5 10 B. 5 1 C. 10 1 D. 1 10 Ans. D EXP. The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result.

EXP. Ending the string constant with \0 explicitly makes no difference. So some and some\0 are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop. 89.main() { char str1[] = {s,o,m,e}; char str2[] = {s,o,m,e,\0}; while (strcmp(str1,str2)) printf(Strings are not equal\n); } A. Strings are not equalStrings are not equal B. Strings are not equal C. Strings are not equal Strings are not equal D. syntax error Ans. C EXP. If a string constant is initialized explicitly with characters, \0 is not appended automatically to the string. Since str1 doesnt have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a \0. So str1 and str2 are not the same, hence the result. 90. main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } A. 1 4 B. 4 1 C. 4 7 D. 1 7 Ans. B EXP. 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. 91. main()

{ float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } A. compiler error B. runtime error C. syntax error D. linker error Ans. A EXP. switch expression not integral. Switch statements can be applied only to integral types. 92. 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; } A. x=10 y=8 B. x=8 y=10 C. x=10 y=10 D. x=8 y=8 Ans. A EXP. 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, 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. 93. main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); } A. 300 B. 557 C. compiler error D. 556 Ans. D EXP. 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. 94. void main() { char *s="\12345s\n"; printf("%d",sizeof(s)); } A. 6 B. 5 C. 7 D. Compiler error:cannot convert \1 as a char pointer at line 3 ANS. A EXP. \n should not be counted in using size of() ;

You might also like