You are on page 1of 19

C Interview Questions with Answers

Mohammad Haris B
haris313@live.com

2011

1. What will be the output of following program? #include <stdio.h> const int SZ=30; typedef struct { char s[SZ]; } st; int main() { st obj; strcpy(obj.s, "hello world"); printf("%s", obj.s); return 0; } Ans: Compiler error. (SZ cannot be used for array size) 2. What will be the output of following program, if the inputs are given as 12,13? int main() { int a=0,b=0; scanf("%d/%d",&a,&b); printf("%d,%d",a,b); } Ans: 12,0 3. What will be the output of following program? int mian() { int i = 0; for (i++;i++;i++) { Printf(%d ,i); if (i == 6) break; } Ans : 246

Page | 2

4. What will be the output of following program? #include<stdio.h> union u1; typedef struct { union u1 *pI; union u1 *pD; } st1; typedef union u1 { st1 var_st1; int i; }var_u1; int main() { var_u1 *var1, *var2; var1= malloc(sizeof(var_u1)); var2= malloc(sizeof(var_u1)); var2->i=1234; var1->var_st1.pI = var2; printf("%d", var1->var_st1.pD- >i); /* Error here (Pd is null )*/ } Ans: Runtime error 5. What will be the output of following program? int main() { char *p; p = Hello; printf(%s,*&*&p); } Ans: Hello 6. A pointer p contains address of a pointer to a pointer to an integer pointer. To retrieve the integer value, we should use __________. a)****p b)****&p c)*****p d)***p Ans: d ( ****p).
Page | 3

7. What is the output of the following program? void fun(int **p); int main() { int a[3][4] = {1,2,3,4,4,3,2,5,3,2,4,6}; int *ptr; ptr=&a[2][1]; fun(&ptr); return 0; } void fun(int **p) { printf("%d\n",**p); } Ans : 2 8. What will be the output of the following code? void func(int *x, int *y) { int temp; x=(int**)malloc(sizeof(int)); y=(int**)malloc(sizeof(int)); temp = *x; *x=*y; *y = temp; } int main() { int i = 1; int j = 2; printf("%d %d\n",i,j); func(&i, &j); printf("%d %d\n",i,j); return 0; } Ans: 1 2 12

Page | 4

9. Which of the following statements is TRUE about the following program? #include<stdio.h> int main() { char a[25], b[25]; puts("Name your favorite song title: "); gets_s(a, 25); puts("\nName another song title:"); scanf_s("%s", &b, 25); printf("\na = %s, b = %s\n", a, b); return 0; } a).The scanf_s() method is able to read only the first word b).The gets_s() method is able to read the entire string or only the first word c).The gets_s() method stops reading at the first space d).None of the above Ans: a. 10. What will be the output of following program ? #include<stdio.h> int main() { char *s = "ABCDEF"; char a[6]= {'A','B','C','D','E','F'}; printf("%d", strcmp(s,a)); return 0; } Ans: -1 11. What will be the output of following program? int main() { char *ch = "World"; printf("%[-10.10]s",ch); return 0; } Ans: [-10.10]s
Page | 5

12. What is the output of the following program? #include <stdio.h> int main() { void fun(int, int []); int arr[] = {1,2,3,4}; int i; fun(4,arr); for(i = 0; i < 4; i++) printf("%d", arr[i]); printf("\n"); return 0; } void fun(int n, int arr[]) { int *p = 0; int i = 0; while(i++ < n) p = &arr[i]; *p = 0; } Ans: Runtime error (May print 1234 if memory leak does not happens) 13. What will be the output of following program? #include<stdio.h> char *fun(char *m) { return (m++); } int main() { char *m, *res; m = "World of C"; m++; res = fun(m); puts(res); return 0; } Ans: orld of C (Because of post increment in the function return)
Page | 6

14. The programmer wanted to find out the size of the abc.txt file in number of bytes, but the following code always prints the result as 0 bytes, Identify the problem. int main () { FILE * pFile; long size; pFile = fopen ("abc.txt","rb"); if (pFile==NULL) perror ("Error opening file"); else { fseek (pFile, 0, SEK_SET); size=ftell (pFile); fclose (pFile); printf ("Size of file: %ld bytes.\n",Size); } return 0; } fseek (pFile, 0, SEEK_CUR); must be used to set the file cursor to the current location in the file fseek (pFile, 0, SEEK_SET); must be removed as it is setting the cursor to the beginning of file The file is a text file and cannot be opened in binary mode fseek (pFile, 0, SEEK_END); must be used to set the file cursor to the end of file Ans: fseek (pFile, 0, SEEK_END); must be used to set the file cursor to the end of file 15. What will be the output of following program? #include<stdio.h> int a[2][3][4][2]; int main() { int c; a[1][2][2][1] = 90; c = *(*(*(*(a + 1) + 2) + 2) + 1); printf(%d",c); return 0; } Ans: 90

Page | 7

16. What will be the output of following program assuming test.txt contains "hello world"? int main(void) { FILE *f = NULL; long offset = 0; f = fopen("test.txt", "rb"); if(f==NULL) { fprintf(stderr,"fopen failed"); exit(-1); } while(fseek(f, 1, SEEK) != EOF) { offset = ftell(f); printf("%d", offset); } return 0; } Ans: Compiler error (SEEK undifined) 17. What will be the output of following program? #include<stdio.h> int main() { int x = 5, y = 3,z; z = x+= y+= 10; printf("%d %d %d",x ,y, z); return 0; } Ans: 18 13 18 (Right to left) 18. What will be the output of following program? #include<stdio.h> int main() { int x = 1, y = 3, z = 5,res; res = x<<y<<z; printf(%d",res); return 0; } Ans: 256 (Left to right)
Page | 8

19. What will be the output of following program? #include<stdio.h> typedef struct str_x { struct str_x *x_ptr; int var; }st_x; int main() { st_x *x_ptr1, *x_ptr2, *x_ptr3; int i = 100; x_ptr1 = NULL; x_ptr3 =(st_x *) &x_ptr1; x_ptr2 = malloc (sizeof (st_x)); x_ptr2->x_ptr = NULL; x_ptr2->var = i; x_ptr3->x_ptr = x_ptr2; printf("%d", (x_ptr1->var)); return 0; } Ans: 100 20. What will be the output of following program? #include<stdio.h> int main() { int denom = 6; int numer = 5; if (denom != 0) printf("\nAnswer = %d",( numer/denom)); else #error "Division by zero" return 0; } Ans: Compiler Error

Page | 9

21. What will be the output of following program? #include <stdio.h> #define SINT short typedef int SINT; void typedef_ashDef() { SINT i; printf("\n Size of i = %d \n", sizeof(i)); return; } Ans: Size of I = 2 (i.e: Size of short) 22. What will be the output of following program? int main() { enum { a, b, c =50, d= b+1, e} st1,st2; st1 = c; st2 = d; printf("%d %d",st1, st2); return 0; } Ans: 50, 2 23. Write a code to remove white space in an entered string without using any extra string. #include<stdio.h> int main() { char string[50]; int i = 0, j= 0; gets(string); while (string[i] != '\0') { if(string[i] != ' ' && string[i] != '\n') { string[j] = string[i]; j++; } i++; } string[j] = '\0'; puts(string); return 0; }
Page | 10

24. What will be the output of following program, assume address of arr is 98760 ? #include<stdio.h> int main() { int arr[] = { 1 ,2, 3, 4 , 7 , 9}; printf("%u %u", arr, &arr); return 0; } Ans: 98760 98760 25. What is the output of the following program? #include<stdio.h> #define SQR(a) (a*a) int main() { return(printf("%d",SQR(2+3))); } Ans: 11 26. What is the output of the following program? #include<stdio.h> int main() { int num = 10; goto LABEL; { static int num = 99; LABEL: printf("%d",num); } return 0; } Ans: 99; 27. Write a program to print Hello world without using a single semicolon. Ans: #include<stdio.h> void main() { if(printf("Hello world\n")) { } }
Page | 11

28. What is the output of the following program? #include<stdio.h> #include<stdlib.h> int main() { float num = 109.68; char string[10]; gcvt( num, 4, string ); puts( string ); return 0; } Ans: 109.6 29. What is the output of the following program? #include<stdio.h> #define MY_MACRO(T,m) (unsigned int)&(((T*)0)->m) typedef struct { char a; int i:9; char b; }OBJECT; int main() { printf("%u",MY_MACRO(OBJECT,b)); return 0; } Ans: 8 30. What is the output of the following program? #include<stdio.h> #include<string.h> int main() { char *str1 = "hello"; char *str2 = "world"; char *str3 = strcat(str1,str2); puts(str3); return 0; } Ans: Runtime error.
Page | 12

31. What is the output of the following program? #include<stdio.h> #include<string.h> void myalloc(char **str, int n) { *str = (char *)malloc(n*sizeof(char)); memset(str, '\0',n); } int main() { char * str = "Hello"; myalloc(&str, 25); strcpy(str,"Hello world"); puts(str); return 0; } Ans: Runtime error (at strcpy) 32. In the following code, at which line compilation error will occur? #include<stdio.h> int main() { int a = 0, b = 0; int *ptr1 = NULL, *ptr2 = NULL; ptr1 = &a; ptr2 = 10; a *=3; ptr2 -= 6; ptr1= ptr1 - ptr2; ptr1 = ptr1*3; return 0; } Ans: At ptr1 = ptr1*3 (This is an illegal operation) 33. What is the output of the following program? #include<stdio.h> int main() { printf("%p",&main); return 0; } Ans: Address of main() will be printed.
Page | 13

34. What is the output of the following program? #include<stdio.h> struct st { int (*x)[10]; int (*g)(); }object; int main() { printf("%d",sizeof(object)); return 0; } Ans: 8 35. What is the output of the following program? #include<stdio.h> int main() { char numbers[5][6] = {"Zero","One","Two","Three","Four"}; printf("%s is %c",&numbers[4][0],numbers[0][0]); return 0; } Ans: Four is Z 36. What is the output of the following program? #include <stdio.h> void func() { void func1() { printf("%s\n", "func1"); } printf("%s\n", "func"); } int main() { func(); return 0; } Ans: Compiler Error

Page | 14

37. What is the output of the following program? #include<stdio.h> int main() { float x = 7, y; y = ++x/--x || ++x; printf("%f", y); return 0; } Ans: 1.0000 38. What is the output of the following program? #include <stdio.h> main() { int num=25; while() { printf("%d", num++); if(num >20)break; } Return 0; } Ans: Compiler error. 39. What is the output of the following program? #include<stdio.h> void func(int *fp) { fp=malloc(2); *fp=10; } void main(void) { int *ptrpop=NULL; func(ptrpop); printf("%d",*ptrpop); } Ans: Runtime error

Page | 15

40. What is the output of the following program? #include<stdio.h> void f(int x, int *y) { x = *(y)+= 2; } int main() { int a[5] = {2,4,6,8,10}; int i,b=5; for( i = 0; i<5; i++) { f(a[i],&b); printf("%d %d\n",a[i],b); } return 0; } Ans: 2 7 4 9 6 11 8 13 10 15 41. What is the output of the following program? #include<stdio.h> void main() { char omni[2][30]={"Hello, world of C","Hello, world of c++"}; printf("%c%c",*(omni[0]+9),*(*(omni+0)+5)); getch(); } Ans: r, 42. What is the output of the following program? #include<stdio.h> int main() { static int var = 5; printf("%d ",var--); if(var) main(); return 0; } Ans: 5 4 3 2 1
Page | 16

43. What is the output of the following program? void main() { char *p; short *q; long *r; p=q=r=0; p++; q++; r++; printf("%p %p %p", p, q, r); } Ans: 00000001 00000002 00000004 44. What is the output of the following program? #include<stdio.h> int main() { printf("%c", "abcdefgh"[6]); return 0; } Ans: g 45. What is the output of the following program? #include <stdio.h> char *str="char *str=%f;main(){printf(str,37);}"; void main() { printf(str,37,str,37); } Ans: char *str=0.000000;main(){printf(str,37);} 46. What is the output of the following program? #include<stdio.h> enum myEnum { AB, BC }; int main() { printf("%d", &((enum myEnum*)0)->BC); return 0; } Ans: Compiler error
Page | 17

47. What is the output of the following program? #include<stdio.h> int main( ) { float i = 10.1; if(++i < 11.1) printf("Awesome"); else printf("Aweful"); return 0; } Ans: Aweful 48. What is the output of the following program? #include<stdio.h> #define FUNC func int func(int x, int y) { return (x+y); } int main() { int z = func(7, 8); printf("%d\n",z); return 0; } Ans: 15 49. What is the output of the following program? #include<stdio.h> int main( ) { int k = 35, z ; k = func1 ( k = func1 ( k = func1 ( k ) ) ) ; printf ( "k = %d", k ) ; return 0; } func1 ( int k ) { k++ ; return ( k ) ; } Ans: k = 38;
Page | 18

50. What is the output of the following program? #include<stdio.h> #include<conio.h> #define A 10 #define B 11 #define C (B/A+A) typedef enum { x1 = (A>B)?A:B, x2 = (A>1)>(B>2)?A:B, x3 = C, }X; int main() { X* xptr = calloc(1, sizeof(unsigned int)); *xptr = x2; printf("%d", *xptr); *xptr = x3; printf("%d", *xptr); getch(); return 0; } Ans: 1111 *************************** All the best *****************************

Page | 19

You might also like