Sie sind auf Seite 1von 17

C interview questions 1. A frequent reader of this site sent this in. No answers, but a nice set of questions.

Consider getting Kernighan and Ritchie title if you find many things puzzling here. 1. What does static variable mean? 2. What is a pointer? 3. What is a structure? 4. What are the differences between structures and arrays? 5. In header files whether functions are declared or defined? 6. What are the differences between malloc() and calloc()? 7. What are macros? What are the advantages and disadvantages? 8. Difference between pass by reference and pass by value? 9. What is static identifier? 10. Where are the auto variables stored? 11. Where does global, static, local, register variables, free memory and C Program instructions get stored? 12. Difference between arrays and linked list? 13. What are enumerations? 14. Describe about storage allocation and scope of global, extern, static, local and register variables? 15. What are register variables? What are the advantage of using register variables? 16. What is the use of typedef? 17. Can we specify variable field width in a scanf() format string? If possible how? 18. Out of fgets() and gets() which function is safe to use and why? 19. Difference between strdup and strcpy? 20. What is recursion? 21. Differentiate between a for loop and a while loop? What are it uses? 22. What are the different storage classes in C? 23. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? 24. What is difference between Structure and Unions? 25. What the advantages of using Unions? 26. What are the advantages of using pointers in a program? 27. What is the difference between Strings and Arrays? 28. In a header file whether functions are declared or defined? 29. What is a far pointer? where we use it? 30. How will you declare an array of three function pointers where each function receives two ints and returns a float? 31. What is a NULL Pointer? Whether it is same as an uninitialized pointer? 32. What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

33. What does the error Null Pointer Assignment mean and what causes this error? 34. What is near, far and huge pointers? How many bytes are occupied by them? 35. How would you obtain segment and offset addresses from a far address of a memory location? 36. Are the expressions arr and *arr same for an array of integers? 37. Does mentioning the array name gives the base address in all the contexts? 38. Explain one method to process an entire string as one unit? 39. What is the similarity between a Structure, Union and enumeration? 40. Can a Structure contain a Pointer to itself? 41. How can we check whether the contents of two structure variables are same or not? 42. How are Structure passing and returning implemented by the complier? 43. How can we read/write Structures from/to data files? 44. What is the difference between an enumeration and a set of pre-processor # defines? 45. What do the c and v in argc and argv stand for? 46. Are the variables argc and argv are local to main? 47. What is the maximum combined length of command line arguments including the space between adjacent arguments? 48. If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which? 49. Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function? 50. What are bit fields? What is the use of bit fields in a Structure declaration? 51. To which numbering system can the binary number 1101100100111100 be easily converted to? 52. Which bit wise operator is suitable for checking whether a particular bit is on or off? 53. Which bit wise operator is suitable for turning off a particular bit in a number? 54. Which bit wise operator is suitable for putting on a particular bit in a number? 55. Which bit wise operator is suitable for checking whether a particular bit is on or off? 56. Which one is equivalent to multiplying by 2? Left shifting a number by 1 Left shifting an unsigned int or char by 1? 57. Write a program to compare two strings without using the strcmp() function. 58. Write a program to concatenate two strings. 59. Write a program to interchange 2 variables without using the third one. 60. Write programs for String Reversal. The same for Palindrome check. 61. Write a program to find the Factorial of a number.

62. Write a program to generate the Fibonacci Series? 63. Write a program which employs Recursion? 64. Write a program which uses command line arguments. 65. Write a program which uses functions like strcmp(), strcpy(), etc. 66. What are the advantages of using typedef in a program? 67. How would you dynamically allocate a one-dimensional and twodimensional array of integers? 68. How can you increase the size of a dynamically allocated array? 69. How can you increase the size of a statically allocated array? 70. When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically? 71. Which function should be used to free the memory allocated by calloc()? 72. How much maximum can you allocate in a single call to malloc()? 73. Can you dynamically allocate arrays in expanded memory? 74. What is object file? How can you access object file? 75. Which header file should you include if you are to develop a function which can accept variable number of arguments? 76. Can you write a function similar to printf()? 77. How can a called function determine the number of arguments that have been passed to it? 78. Can there be at least some solution to determine the number of arguments passed to a variable argument list function? 79. How do you declare the following: An array of three pointers to chars An array of three char pointers A pointer to array of three chars A pointer to function which receives an int pointer and returns a float pointer A pointer to a function which receives nothing and returns nothing 80. What do the functions atoi(), itoa() and gcvt() do? 81. Does there exist any other function which can be used to convert an integer or a float to a string? 82. How would you use qsort() function to sort an array of structures? 83. How would you use qsort() function to sort the name stored in an array of pointers to string? 84. How would you use bsearch() function to search a name stored in array of pointers to string? 85. How would you use the functions sin(), pow(), sqrt()? 86. How would you use the functions memcpy(), memset(), memmove()? 87. How would you use the functions fseek(), freed(), fwrite() and ftell()? 88. How would you obtain the current time and difference between two times? 89. How would you use the functions randomize() and random()? 90. How would you implement a substr() function that extracts a sub string from a given string?

91. What is the difference between the functions rand(), random(), srand() and randomize()? 92. What is the difference between the functions memmove() and memcpy()? 93. How do you print a string on the printer? 94. Can you use the function fprintf() to display the output on the screen? 95. Gautam Pagedar adds this question: What is a linklist and why do we use it when we have arrays? - I feel the correct answer should be linklist is used in cases where you dont know the memory required to store a data structure and need to allocate is dynamically on demand. 96. How do you detect a loop in linked list? 97. Sunil asks: What is the difference between main() in C and main() in C+ +? 98. ajz at his interviews asks what will be printed out when the following code is executed: main() { printf("%x",-1<<4); }

C interview questions part1 1. What will print out? main() { char *p1=name; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(%sn,p2); } Answer:empty string. 2. What will be printed as the result of the operation below: main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(%d%dn,x,y); } Answer : 5794 3. What will be printed as the result of the operation below: main()

{ int x=5; printf(%d,%d,%dn,x,x< <2,x>>2); } Answer: 5,20,1 4. What will be printed as the result of the operation below: #define swap(a,b) a=a+b;b=a-b;a=a-b; void main() { int x=5, y=10; swap (x,y); printf(%d %dn,x,y); swap2(x,y); printf(%d %dn,x,y); } int swap2(int a, int b) { int temp; temp=a; b=a; a=temp; return 0; } Answer: 10, 5 10, 5 5. What will be printed as the result of the operation below: main() { char *ptr = Cisco Systems; *ptr++; printf(%sn,ptr); ptr++; printf(%sn,ptr); } Answer:Cisco Systems isco systems 6. What will be printed as the result of the operation below: main() { char s1[]=Cisco; char s2[]= systems; printf(%s,s1); } Answer: Cisco 7. What will be printed as the result of the operation below: main() {

char *p1; char *p2; p1=(char *)malloc(25); p2=(char *)malloc(25); strcpy(p1,Cisco); strcpy(p2,systems); strcat(p1,p2); printf(%s,p1); } Answer: Ciscosystems 8. The following variable is available in file1.c, who can access it?: 9. static int average; Answer: all the functions in the file1.c can access the variable. 10. WHat will be the result of the following code? #define TRUE 0 // some code while(TRUE) { // some code } Answer: This will not go into the loop as TRUE is defined as 0. 11. What will be printed as the result of the operation below: int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output:%dn",x); x++; changevalue(x); printf("Second output:%dn",x); modifyvalue(); printf("Third output:%dn",x); } Answer: 12 , 13 , 13 12. What will be printed as the result of the operation below:

main() { int x=10, y=15; x = x++; y = ++y; printf(%d %dn,x,y); } Answer: 11, 16 13. What will be printed as the result of the operation below: main() { int a=0; if(a==0) printf(Cisco Systemsn); printf(Cisco Systemsn); } Answer: Two lines with Cisco Systems will be printed. Embedded systems interview questions 1. Can structures be passed to the functions by value? 2. Why cannot arrays be passed by values to functions? 3. Advantages and disadvantages of using macro and inline functions? 4. What happens when recursion functions are declared inline? 5. Scope of static variables? 6. Difference between object oriented and object based languages? 7. Multiple inheritance - objects contain howmany multiply inherited ancestor? 8. What are the 4 different types of inheritance relationship? 9. How would you find out the no of instance of a class? 10. Is java a pure object oriented language? Why? 11. Order of constructor and destructor call in case of multiple inheritance? 12. Can u have inline virtual functions in a class? 13. When you inherit a class using private keyword which members of base class are visible to the derived class? 14. What is the output of printf("\nab\bcd\ref"); -> ef 15. #define cat(x,y) x##y concatenates x to y. But cat(cat(1,2),3) does not expand but gives preprocessor warning. Why? 16. Can you have constant volatile variable? Yes, you can have a volatile pointer? 17. ++*ip increments what? it increments what ip points to 18. Operations involving unsigned and signed unsigned will be converted to signed 19. a+++b -> (a++)+b 20. malloc(sizeof(0)) will return valid pointer 21. main() {fork();fork();fork();printf("hello world"); } will print 8 times. 22. Array of pts to functions void (*fptr[10])() 23. Which way of writing infinite loops is more efficient than others? there are 3ways.

24. # error what it does? 25. How is function itoa() written? 26. Who to know wether systemuses big endian or little endian format and how to convert among them? 27. What is interrupt latency? 28. What is forward reference w.r.t. pointers in c? 29. How is generic list manipulation function written which accepts elements of any kind? 30. What is the difference between hard real-time and soft real-time OS? 31. What is interrupt latency? How can you recuce it? 32. What is the differnce between embedded systems and the system in which rtos is running? 33. How can you define a structure with bit field members? 34. What are the features different in pSOS and vxWorks? 35. How do you write a function which takes 2 arguments - a byte and a field in the byte and returns the value of the field in that byte? 36. What are the different storage classes in C? 37. What are the different qualifiers in C? 38. What are the different BSD and SVR4 communication mechanisms

1. The C language terminator is (a) semicolon (b) colon (c) period (d) exclamation mark 2. What is false about the following -- A compound statement is (a) A set of simple statments (b) Demarcated on either side by curly brackets (c) Can be used in place of simple statement (d) A C function is not a compound statement. 3. What is true about the following C Functions (a) Need not return any value (b) Should always return an integer (c) Should always return a float (d) Should always return more than one value 4. Main must be written as (a) The first function in the program (b) Second function in the program (c) Last function in the program (d) Any where in the program

5. Which of the following about automatic variables within a function is correct ? (a) Its type must be declared before using the variable (b) Tthey are local (c) They are not initialised to zero (d) They are global 6. Write one statement equivalent to the following two statements x=sqr(a); return(x); Choose from one of the alternatives (a) return(sqr(a)); (b) printf("sqr(a)"); (c) return(a*a*a); (d) printf("%d",sqr(a)); 7. Which of the following about the C comments is incorrect ? (a) Ccommentscan go over multiple lines (b) Comments can start any where in the line (c) A line can contain comments with out any language statements (d) Comments can occur within comments 8. What is the value of y in the following code? x=7; y=0; if(x=6) y=7; else y=1; (a) 7 (b) 0 (c) 1 (d) 6 9. Read the function conv() given below conv(int t){ int u; u=5/9 * (t-32); return(u); } What is returned (a) 15 (b) 0 (c) 16.1 (d) 29 10. Which of the following represents true statement either x is in the range of 10 and 50 or y is zero

(a) x >= 10 && x <= 50 || y = = 0 (b) x<50 (c) y!=10 && x>=50 (d) None of these 11. Which of the following is not an infinite loop ? (a) while(1)\{ ....} (b) for(;;) { ... } (c) x=0; do{ /*x unaltered within the loop*/ .....} while(x = = 0); (d) # define TRUE 0 ... while(TRUE){ ....} 12. What does the following function print? func(int i) { if(i%2)return 0; else return 1;} main() { int =3; i=func(i); i=func(i); printf("%d",i); } (a) 3 (b) 1 (c) 0 (d) 2 13. How does the C compiler interpret the following two statements p=p+x; q=q+y; (a) p=p+x; q=q+y (b)p=p+xq=q+y (c)p=p+xq; q=q+y (d)p=p+x/q=q+y

For questions 14,15,16,17 use the following alternatives b.char c.string d.float 14. '9' 15. "1 e 02" 16. 10e05 17. 15 18. Read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the value after executing this code? (a) 200 (b) 1 (c) -1 (d) 50 19. A memory of 20 bytes is allocated to a string declared as char *s then the following two statements are executed: s="Entrance" l=strlen(s); what is the value of l ? (a)20 (b)8 (c)9 (d)21 20. Given the piece of code int a[50]; int *pa; pa=a; To access the 6th element of the array which of the following is incorrect? (a) *(a+5) (b) a[5] (c) pa[5] (d) *(*pa + 5}

21. Consider the following structure: struct num nam{ int no; char name[25]; } struct num nam n1[]={{12,"Fred"},{15,"Martin"},{8,"Peter"},{11,Nicholas"}}; ..... ..... printf("%d%d",n1[2],no,(*(n1 + 2),no) + 1); What does the above statement print? (a) 8,9 (b) 9,9 (c) 8,8 (d) 8,unpredictable value 22. Identify the in correct expression (a) a=b=3=4; (b) a=b=c=d=0; (c)float a=int b=3.5; (d)int a; float b; a=b=3.5; 23. Regarding the scope of the varibles;identify the incorrect statement: (a)automatic variables are automatically initialised to 0 (b)static variables are are automatically initialised to 0 (c)the address of a register variable is not accessiable (d)static variables cannot be initialised with any expression 24. cond 1?cond 2?cond 3?:exp 1:exp 2:exp 3:exp 4; is equivalent to which of the following? (a)if cond 1 exp 1; else if cond 2 exp 2; else if cond 3 exp 3; else exp 4; (b) if cond 1 if cond 2 if cond 3 exp 1; else exp 2; else exp 3; else exp 4; (c) if cond 1 && cond 2 && cond 3 exp 1 |exp 2|exp 3|exp 4;

(d) if cond 3 exp 1; else if cond 2 exp 2; else if cond 3 exp 3; else exp 4; 25. The operator for exponencation is (a) ** (b) ^ (c) % (d) not available 26. Which of the following is invalid (a) a+=b (b) a*=b (c) a>>=b (d) a**=b 27. What is y value of the code if input x=10 y=5; if (x==10) else if(x==9) else y=8; (a)9 (b)8 (c)6 (d)7 28. What does the following code do? fn(int n,int p,int r){ static int a=p; switch(n){ case 4:a+=a*r; case 3:a+=a*r; case 2:a+=a*r; case 1:a+=a*r;}} (a)computes simple interest for one year (b)computes amount on compound interest for 1 to 4 years (c)computes simple interest for four year (d)computes compound interst for 1 year 29. a=0; while(a<5) printf("%d\\n",a++); How many times does the loop occurs?

(a)infinite (b)5 (c)4 (d)6 30. How many times does the loop iterated ? for (i=0;i=10;i+=2) printf("Hi\\n"); (a)10 (b) 2 (c) 5 (d) None of these 31. What is incorrect among the following A recursive function (a) calls itself (b) is equivalent to a loop (c) has a termination condition (d) does not have a return value at all 32. Which of the following go out of the loop if expn 2 becoming false (a) while(expn 1)\{...if(expn 2)continue;} (b) while(!expn 1)\{if(expn 2)continue;...} (c) do{..if(expn 1)continue;..}while(expn 2); (d) while(!expn 2)\{if(expn 1)continue;..\} 33. Consider the following program main() {unsigned int i=10; while(i>=0){ printf("%u",i) i--;} } How many times the loop will get executed (a)10 (b)9 (c)11 (d)infinite 34.Pick out the add one out (a) malloc() (b) calloc() (c) free() (d) realloc() 35.Consider the following program

main(){ int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1] is (a) 1 (b) 3 (c) -6 (d) none 36. # define prod(a,b)=a*b main(){ int x=2; int y=3; printf("%d",prod(x+2,y-10)); } the output of the program is (a) 8 (b) 6 (c) 7 (d) None 37.Consider the following program segment int n,sum=1; switch(n){ case 2:sum=sum+2; case 3:sum*=2; break; default:sum=0;} If n=2, what is the value of sum (a) 0 (b) 6 (c) 3 (d) None of these 38. Identify the incorrect one 1.if(c=1) 2.if(c!=3) 3.if(a<b)then 4.if(c==1) (a) 1 only (b) 1&3 (c) 3 only (d) All of the above 39. The format specified for hexa decimal is

(a) %d (b) %o (c) %x (d) %u 40. Find the output of the following program main(){ int x=5, *p; p=&x printf("%d",++*p); } (a) 5 (b) 6 (c) 0 (d) none of these 41.Consider the following C code main(){ int i=3,x; while(i>0){ x=func(i); i--; } int func(int n){ static sum=0; sum=sum+n; return(sum);} The final value of x is (a) 6 (b) 8 (c) 1 (d) 3 43. Int *a[5] refers to (a) array of pointers (b) pointer to an array (c) pointerto a pointer (d) none of these 44.Which of the following statements is incorrect (a) typedef struct new{ int n1; char n2; } DATA; (b) typedef struct { int n3; char *n4;}ICE;

(c) typedef union{ int n5; float n6;} UDT; (d) #typedef union { int n7; float n8;} TUDAT;