Sie sind auf Seite 1von 14

[FAQ] C Interview Questions :: Frequently asked questions ------------------------------------------------------------C questions Asked in interviews : ------------------------------------------------------------1) for(i=0;i<=5;i++) { printf("%d",i); } what is the

output, if i++ is changed to ++i whta will be the output. 2) for(i=0; ;i++) { i=i+2; break; printf("%d",i); } what is the output. if break is replaced by continue what is the output. 3) Difference between memcopy and strcpy. 4) which of the foll is correct in order to terminate the string at 4th byte ? char *p; p=(char *)malloc(10); strcpy(p,"hello"); p+4 =0; or char *p; p=(char *)malloc(10); strcpy(p,"hello"); p+4 ='\0'; Give reasons. 5) What is a Lvalue ? 6) char a[ ][ ], **p; p=(char **)malloc(sizeof(100); a=p; Is this correct ? If not what r the errors. 7) What are the different pointer models in C ? ( LP32 , LP64) 8) how many bytes are allocated for this structure? struct data { int i; char c; int j; }; What if you use #pragma pack 9) How is the alignment of structures done in Unix ? 10) What are little and big endian ? how do u check whether your machine is little or big endian using a program ? 11) How many bytes are allocated to a int , long , double in 32-bit OS and a 64-bit OS? 12) Explain 3 ways of counting no of bit 1 in a given integer ? 14) Compilation stages? Explain 15) What is sparse files?

16) How do u create a file of 2 GB size? 17) Explain DLKMP 18) Stages during linking? 19) while(c=getchar() != EOF) { } how many times the above loop will be executed if the input is "hello world" CTRL D. 20) explain abt BIT MASK and BIT FIELDS 21) write a function to set the nth bit of an integer. 22) write a function to count the no. of unset bits in an integer. 23) switch(num % 10){ case 1 : printf("one\n"); case 2 : printf("two\n"); case 3 : printf("three\n"); } what is the output if num = 1 24) Initialize a pointer to two dimensional array.(allocate in a ragged fashion) 25) how to set a variable in the environment list 26) how is it set in different shell environments? 27) how can the environment variables used by the other functions in the program. 28) void* X(void *p1, void *p2) { void *t = p2; while(*p2++ = *p1++); return t; } what is the output of the when above function if called as: 29) char *s ,*d; strcpy(s, "hello"); x(s,d); 30) $set a=1 $sh $ehco $a what is the output of executing the above commands at shell.( ans empty string is printed for the command echo $a) what should be done to print value of a? 31) what is the value of i? int i = 5,6; 32) what is wchar_t ? 33) o/p of the prog main() { int *p=5; char *c= 'n'; printf("%d%c\n,*p,c); } 34) write a 2 line prog to generate core dump. 35) Questions on Pointers & Arrays [ie can we add 2 pointers etc]

36) what are static, volatile, global ,static global variables 37) implement queue using linked list & arrays 38) deletion of a node in doubly linked list given the pointer to the node to be deleted , advantages of doubly linked list. 39) explain hashing ,open hash close hash ,implementation 40) WAP for strncpy ,strcmp , how can optimization be done to the code written 41) Question on Balanced Binary trees, B trees. 42) Can we store elements of a balanced binary tree in an Array . If yes, how to access them. 43) how do U delete a node in a B tree , Binary tree 44) Diff b/w structure in C & class of C++. 45) Given a function with hour and minute as 2 parameters , calculate the angle b/w the hour & minute hand. 46) Advantages of hashing when compared to doubly linked list 47) Diff b/w C & C++ , Explain inheritance , polymorphism. 48) what is the difference between variable definition and declaration ? when is the memory allocated to the variable? 49) declare a pointer to array of 10 int , declare 10 pointers to integers , declare a function pointer , declare a pointer to pointer and how do u initialize such a pointer. 50) why u dont use * when dereferencing a function pointer ? 51) write a macro to find square of a no . [ write it as #define SQ(x) (x) * (x) and not #define SQ(x) x * x ] 52) Asked an example as to how do different objects have different characteristics? 53) How do u declare an array of function pointers & How do u call them ? 54) Gave an example and asked about the size of the structure in 32 bit word architecture ? struct a { int a; long b; char c; double d; } 57) what happens when u add a byte to the above structure ? On what is it dependent ? 58) Gave an example and asked what is the difference between this stucture in c and class in c++ ? 59) third arg in main? 60) macro to find the squre of a number 61) different segments of a process, given a C program asked where each of the statements fit into. which segment is dynamically growing & which is static. 62) o/p of i=(5*7,7*6); [regarding this asked what is comma operator] 63) difference b/w declaration & definition 64) write a prototype for a "function taking pointer to a array & an array of pointers as arguments & returing pointer to an array" Ans: int (*fun(int (*a)[],int *b[])[] 65) what is virtual address 66) o/p of #define 0 printf("hello"); #else printf("hi"); #endif

67) why char *str="hello" can't be modified? 68) array of function pointers that takes int as argument and returns int. where such pointrs are used?. 69) for ( ; ; ) printf ("sth"); 70) whats the output ? whats the o/p if theres a semi-colon after for statement 71) while ( 99 ) { ..... ..... } 72) while ( -99 ) { .... .... } what happens in the above 2 cases 73) a[] = "hello"; strlen(a) = ??? sizeof(a) = ??? 74) is this possible: a[10] = "hello"; 75) Write a shared library 76) How do u link 2 obj files ( LD ) 77) Write a function for strcpy (); 78) Diff betn strcpy and memcpy 79) How to access a function defined in some other file ? (extern and static: what happens) 80) Can we print sth before the main function is invoked ? 81) About C-startup function ... 82) Macro to find the larger of two numbers 83) why function pointers are used 84) .Scope of variables 85) .for ( i=0;i<9;i++) { static int j=3; j++; printf( "%d",j); } printf( "%d",j); 86) char *str; str = malloc .... str = 65; str++; str = 66; str++; str = 67; str++; is this code valid ?

87) char *str; str = malloc ( .. ) strcpy( str, "helloworld"); str + 5 = 0; // is this valid 89) int i = 3; int j = i; // is this valid 90) whats a multi way search tree, binary tree order of trees, binary search trees how do u balance a tree - avl trees, red-black trees 91) datastructure for a n-ary tree 92) main() { int i = 3, j; foo( &i ); print i } foo( int *i ) { i = 10; } 93) int i = 1, 2, 3, 4, 5 ; print i; // whats the o/p 94) Explain precedence and associativity. 95) How does the following work - a|b|c a&b&c. 96) Write a program to divide the num by 4 using bitwise operators. 97) write a program to set the perticuler bit in the integer. 98) write a code for creating the singly linked list. What is the significance of typedef here. 99) char a[]={"foo","foo1","foo2"}; what is the size of array, How do you find out number of strings in the array. 100) write your own strcpy. 101) write declarations like pointer to function taking pointer to pointer and retuning a pointer to char. 102) what is a function pointer and declare the same. 103) write a pointer to a function 104) how do u access a pointer to the array of pointers 105) what is a macro in C 106) struct a{ int i; char c; int j; } struct b{ int a; char c; char b; } size of both the structures 107) explained attributepacked option

108) do u know about packing attribute on functions 109) Various segments of a program 110) .j=0; for(i=-2543;i<452;i++) j++; what is the value of j ? ans (2995,1091,0, 2996,1092); 111) class A{.....}; class B{ A a;...}; a)B is inherited from A b)B is Base class of A c)B is polymorphic d)B is subclass of A e)None of above(ans e) 112) class A{ public : int func();}; int A:: func() { static int pp; } scope of pp is ? a)only within the class b)only within func(ans); c)global d)objects of the class e)none of above 113) class A{ public : int func();}; int A:: func() { static int pp; } life of the pp is a)only in objects of class b)only when func is active c)both within and outside func d)when program is running(ans i hope) e)..... 114) int i; char buf[100]; i=sizeof(buf); printf(" i is %d\n",i); a) 100 b)100*sizeof(char)( ans ) c) 0 d) ..? e)none 115) ct x{int i;int j;int k;}; struct x *p; struct x arr[3]; p=&arr[0]; p++; hat will be p pointing to ? a)pointing to i of arr[0];

b)pointing to j of arr[0]; c)pointing to k of arr[1]; d)pointing to i of next element in array( ans) c)........ 116) int i; struct x{int i;int j;int k;}; struct x *p,*p1; struct x arr[3],arr1[3]; p=&arr[0]; p1=&arr1[0]; i=p+p1; what is the value of i? 117) int i; struct x{int i;int j;int k;}; struct x *p,*p1; struct x arr[3],arr1[3]; p=&arr[0]; p1=&arr1[0]; i=p - p1; what is the value of i? 118) what will be the output for func(6); int func(int x) { if(x <= 0) return 1; return x*func(x-1); } 119) List * search(float val) { List *l=LinkHead; for(l=ListHead;l->next!=NULL;l=l->next) if(l->value==val) break; return l; } 120) If the list contains N node what would be the average number of nodes parsed ? a)N*(N+1)/2 b)N*(N-1)/2 c)N d)N/2 e)? 121) int buf[10]; main() { int i; for(i=0;i<9;i++) f(buf[i] > buf[i+1]) swap(i,i+1); } what is maximum number of times swap called?

a)10 b)9 c)45 d)90 e)72 122) int sum=0; for(int i=0;i<51;i++) sum+=i; what is the value of sum? a)20xx b)25xx c)1050 d)1275 e)?? 123) int func(int x) { int count=0; for(i=0;i<x;i++) { if((i%2)==0) continue; count++; } return count; } what will be the value of count for func(6); a)6 b)2 c)3 d)1 e)?? 124) int func(int x) { while(x) { if((i%10)==0) printf("$"); x--; } } How many times will $ be printed for func(256); a)256 b)20 c)25 (ans) d)?? e)?? 125) class A{A(){ printf("4");}; class B:class A{B(){ printf("4");}; class C:class B{C(){ printf("4");}; C cobj; what is the order of the printfs? a)6 5 4 b)5 6 4

c)4 5 6 d)6 4 5 e)>?? (ans: not printed, bcos constructors cannot be private to a class) 126) There a 1024 records which are sorted according to index.For every search it takes 10micro secs.What is the average time saved by using binary search instead of sequential search. a)5 milli secs b)10 milli secs c)1 milli sec d)2.5 milli secs e)3 milli secs 127) what is best suitable for binary search a)Unsorted elements sequential access b)Unsorted elements random access c)sorted elements sequential access d)sorted elements random access(ans) 128) Given the height of binry tree as h what is the maximum number of nodes (ans =( 2^(h+1) - 1 ) 129) In a binary tree If N is the number of internal nodes , what is maximum number of external nodes. (ANS = N + 1) 130) question about critical section 132) questions about semaphores( counting semaphore) 133) question about deadlock( Conditions for deadlock) 134) int i=8; int j=2; int k=(i&j)+(i&&j)+(i^j); what is value of k; (ans k = 11 = 0 + 1 + 10) 135) void func(int i,int &j,int *k) { i=2; j=16; *k=32; } main() { int i=1,j=4,k=16,m; func(i,j,&k); m=i+j+k; } what is value of m? (ans = 49) 136) int (*p)[10] Complex Declaration what is p? (ans : pointer to array of 10 integer) 137) char *buf="Hello\tWorld\n";

int func(char *ptr) { int i; while(*ptr++) i++; return i; } what will func(buf) return? (ans = garbage value) 138) what is a library and what is the difference betw static and shared lybraries? 139) what is the difference betw compiler and linker? 140) what is the difference betw static and other variables? 141) whether this following program is correct or not, if not why? 1.void i; 2.int j=10; 3.float k=20.5; 4.i=j+k; 5.printf("%f",i); 142) whether this following program is correct or not, if not add some code and make it correct. int * i; scanf("%d",i); 143) write a function which takes a number and returns number of "1" in the number. eg: if you pass 10 (binary representation is 1010), it should return 2; 144) write pointer to function that returns pointer to function 145) Write program using bit operators divide two number. 146) Write program for sum of digits using recursion. 147) Int a=10; int b; b = a++; what is b and a? Then b = ++a++; what is a? 148) write pointer function which take two char pointers and returns int. and assign it to function and call it. 149) Access the single array and double array using pointers. 150) char *a="abcd"; strcpy(a,"xyz"); printf("\n %s",a); 151) struct a { int b; }; struct b { int b; }; int main() {

struct a first; struct b second; first.b = 10; second = first; printf("%d",second.b); } what is o/p? 152) in above program if we do (first.b++)++; second = first; printf("%d",second.b); what is o/p? 153) Consider a tree in which children are growing dynamicaly (i.e one node may be have 3 children, second may be have 10 and other may have N) write the datastructure for problem? 154) for(;;) { int i=10; ; ; } printf("%d",i); what is i? 155) for(;;) { static int i=10; ; ; } printf("%d",i); what is i? 156) write prototype for printf explain how it is implimented 157) int main() { extern int i; i = 20; printf("%d",i); } 158) i=5; f1(i++); f1(int i) { print i; }

what is i; 159) i = 10; j = 20; what is (i+j)++; 160) i=5; (i++)++; what is i? 161) i = 1; static int a[10]; a[i] = i++; 162) i = 5; what is i = i++ * i++; 163) can we derefernce the NULL pointer. 164) what is differnce between typedef and #define. 165) When to use static libraries and shared libraries and what is the difference? 166) What is the difference between char *a and char a[]? 167) Which is the data structure in C to store floating point variables? Ans: Union 168) What are the adv and disadv of register? 169) o/p of the prog main() { int *p=5; char *c= 'n'; printf("%d%c\n,*p,c); } 170) write a 2 line prog to generate core dump. 171) can we add 2 pointers why? 172) what are static, volatile, global ,static global variables 174) what is the difference between ANSI C and K&R C? 175) Consider the program: main() { int *pt; Func(pt); printf("%p\n",pt); } Func(int *p) { p=(int *)malloc(100); printf("%p",p); } Whether the above two printf statements will print the same value or not.

176) Where exactly we need structures and unions 177) int i; /* i is a global variables that are shared by many threads */ for(;;) { k = i + 10; } if we do optimization for this program then compiler will place this statement above the for loop : give solution. 178) 1 2 3 4 6 7 8 9 10 11 12 sd write program to copy n (8 numbers) from s to d 179) struct A struct B {{ int a; char a; char b; int b; char c; char c; }; }; sizeof(A) ? sizeof(B) ? 180) what difference between memcopy and memmove ? 181) what is sequence point in C? 182) what is short circuit in C? 183) how do u free the free? how it come to know how much memory to be freed? 184) what happens if we free the memory twise? 185) which is the concatenation operator is in C? Printf("HELLO" "WORLD"); 186) which is efficent swicth or if ? why explain 187) main() { int *p; p = fun(); printf("%d\n", *p); } int * fun() { int a = 10; return &a; } what is the o/p? 188) Why " #" is used is # include.. and # define.... 189) Is sizeof any variable is complier dependent or sataic?. 190) for ( printf("a") ; printf ( "b") ; printf("c")) ; 191) How do u handle the exception in C 192) What are the diiferent tools used to go through the src code of linux (ctags and cscope) 193) Write a fn for bit reversing? Eg: 1010 ? 0101 194) int main() { int *p,*q; p = malloc(10);

*p = 10; q = p; free(p); free(q); printf("%d",*q); } what is the o/p of program? 195) Write the declaration for the following a) A pointer to an array of pointers to array of ints. b) An array of pointers to functions that return pointers to functions that in turn return pointers to an ints c) A function returning an array of pointers to array of floats d) A function returning a pointer to an array of pointers to functions each of which returns a pointer to a char e) A function returning a pointer to an array of pointers to functions that return (pointers to) 10-element array of doubles.

Das könnte Ihnen auch gefallen