Tuesday, July 3, 2007

C Interview Questions

1. What will print out?
main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%s\n”,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%d\n”,x,y);
}
Answer : 5794

3. What will be printed as the result of the operation below:
main()
{
int x=5;
printf(“%d,%d,%d\n”,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 %d\n”,x,y);
swap2(x,y);
printf(“%d %d\n”,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(“%s\n”,ptr);
ptr++;
printf(“%s\n”,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?
static int average;
Answer: all the functions in the file1.c can access the variable.

9. 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.

10. 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:%d\n",x);
x++;
changevalue(x);
printf("Second output:%d\n",x);
modifyvalue();
printf("Third output:%d\n",x);
}
Answer: 12 , 13 , 13
11. What will be printed as the result of the operation below:
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %d\n”,x,y);
}
Answer: 11, 16
12. What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0)
printf(“Cisco Systems\n”);
printf(“Cisco Systems\n”);
}
Answer: Two lines with “Cisco Systems” will be printed.

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?
57. Left shifting a number by 1
58. Left shifting an unsigned int or char by 1?
59. Write a program to compare two strings without using the strcmp() function.
60. Write a program to concatenate two strings.

17 comments:

InterviewHelpC++ said...

Question No. 5th Answer is incorrect.
It should be "isco Systems"
and for the second one "sco Systems"

naveenchandra said...

Check the space between C and " buddy... " Cis" not "Cisco"

Anonymous said...

2 nd one answer is wrong it will print 5793

Anonymous said...

2nd one is CORRECT. It will print 5794. You are forgetting to increment y by 1 after this line (y=++y + ++x).

Anonymous said...

Actually, he probably doesn't realize x is incremented *after* it is assigned the value 55, thus it becomes 56. Then, y = ++y + ++x; becomes y=(37 + 57);

Anonymous said...

#10 is wrong. modifyvalue() does indeed increment the global x by 10.

Farnaz said...
This comment has been removed by the author.
Farnaz said...

Q (30)

float ( *f[] )( int, int ) = { function1, function2, function3 );

Farnaz said...
This comment has been removed by the author.
Nethaji Karuppasami said...

Q:5,

Ans is:

isco systems
sco systems

Anonymous said...

KRITICA said'
output of 11 question is 11 16 instead of 11,16.

Anonymous said...

anser 11 is wrong its 11 16 instead of 11,16

Anonymous said...

answer 2 is correct y=++y + ++x means 58+36 becoz first increment y by 1 i.e 35+1=36 then increment x by 1 (at this time value in x is 57 instead of 20) i.e 57+1=58 now 58+36=97

Anonymous said...

any one explain me q: 10

Anonymous said...

can any one explain me question no. 10

sudu271 said...

Ya, question 10 is so complex..
Can some one do a explanation??

Anonymous said...

no question 10 is simple....look close at the code..the function "int changevalue(int x)" do returns a value but this value is NEVER assigned to the varible x (one which we are printing at our main program)
Additionally, function "int modifyvalue()" simply dont takes a value so it will change the Global value...the one garbage value...
So only increaments we get are the of simple postfix increaments of x...as simple it was..just look closer..