Sunteți pe pagina 1din 14

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

Consider getting Kernighan and Ritchie title if you find many things puzzl ing here. What does static variable mean? What is a pointer? What is a structure ? What are the differences between structures and a...rrays? In header files whe ther functions are declared or defined? What are the differences between malloc( ) and calloc()? What are macros? What are the advantages and disadvantages? Diff erence between pass by reference and pass by value? What is static identifier? 1 0. Where are the auto variables stored? 11. Where does global, static, local, register variables, free memory and C Pro gram 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, loca l and register variables? 15. What are register variables? What are the advantage of using register varia bles? 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 functio n 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 N ULL 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 m emory 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 o r 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 shoul d be appropriately expanded, are we required to make any special provision? If y es, which? 49. Does there exist any way to make the command line arguments available to ot her functions without passing them as arguments to the function? 50. What are bit fields? What is the use of bit fields in a Structure declarati on? 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 i s on or off? 53. Which bit wise operator is suitable for turning off a particular bit in a n umber? 54. Which bit wise operator is suitable for putting on a particular bit in a nu mber? 55. Which bit wise operator is suitable for checking whether a particular bit i s 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. Wri te 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 two-dimensional ar ray 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 a utomatically? 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 pa ssed 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 re turns 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 p ointers to string? 84. How would you use bsearch() function to search a name stored in array of po inters 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 c ases where you don t know the memory required to store a data structure and need t o 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 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. 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 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 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 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 What will be printed as the result of the operation below: main() { char s1[]= Cisco ; char s2[]= systems ; printf( %s ,s1); } Answer: Cisco 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 The following variable is available in file1.c, who can access it?: static int a verage; 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

Can structures be passed to the functions by value? Why cannot arrays be passed by values to functions? Advantages and disadvantages of using macro and inline f unctions? What happens when recursion functions are declared inline? Scope of st atic variables? Difference between object oriented and object based languages? M ultiple inheritance - objects contain howmany multiply inherited ancestor? What are the 4 different types of inheritance relationship? How would you find out th e 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 exp and but gives preprocessor warning. Why? 16. Can you have constant volatile variable? Yes, you can have a volatile point er? 17. ++*ip increments what? it increments what ip points to 18. Operations involving unsigned and signed unsigned will be converted to sign ed 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 ar e 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) (b) (c) (d) Ccommentscan go over multiple lines Comments can start any where in the line A line can contain comments with out any language statements 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) (b) (c) (d) 7 0 1 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) (d) 10. 10 (a) (b) (c) (d) 11. (a)

16.1 29 Which of the following represents true statement either x is in the range of and 50 or y is zero x >= 10 && x <= 50 y = = 0 x<50 y!=10 && x>=50 None of these Which of the following is not an infinite loop ? 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) (b) (c) (d) 3 1 0 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 a.int

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) (b) (c) (d) 200 1 -1 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) (b) (c) (d) 21. *(a+5) a[5] pa[5] *(*pa + 5} 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) (b) (c) (d) 22.

8,9 9,9 8,8 8,unpredictable value 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) 26. (a) (b) (c) (d) 27. y=5; ** ^ % not available Which of the following is invalid a+=b a*=b a>>=b a**=b What is y value of the code if input x=10

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 (b)computes (c)computes (d)computes simple interest for one year amount on compound interest for 1 to 4 years simple interest for four year 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) (b) (c) (d) calls itself is equivalent to a loop has a termination condition does not have a return value at all

32. Which of the following go out of the loop if expn 2 becoming false (a) (b) (c) (d) 33. while(expn 1)\{...if(expn 2)continue;} while(!expn 1)\{if(expn 2)continue;...} do{..if(expn 1)continue;..}while(expn 2); while(!expn 2)\{if(expn 1)continue;..\} 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) (b) (c) (d) 1 3 -6 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) (b) (c) (d) 38. 0 6 3 None of these Identify the incorrect one

1.if(c=1) 2.if(c!=3) 3.if(a<b)then

4.if(c==1) (a) (b) (c) (d) 39. (a) (b) (c) (d) 40. 1 only 1&3 3 only All of the above The format specified for hexa decimal is %d %o %x %u 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) (b) (c) (d) 43. 6 8 1 3 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; See More .

S-ar putea să vă placă și