Thursday, October 3, 2013

10 C Interview Questions and Answers

c interview questions and answers
In this post you can find 10 Latest c interview questions and answers. These are some of the best c language questions asked  to freshers in technical interviews. I have tried to provide answers with full explanation. If you are looking for simple c questions then you can have look at my previous posts. You may ask all your doubts by commenting in the post. I'll try my best to answer your questions at earliest.

C Placement questions answers for beginners

Question 53. Compare arrays and pointers {Give both similarities and difference}.

Answer: Array is nothing but a pointer in disguise. Array name stores base address of array. Thus we can say array points to a memory location of fixed size of a particular data type.
arr[1] is same as *(arr+1)
{In fact 1[arr] is same as arr[1] because they both are converted into *(1+arr) and *(arr+1)}
However array is not same as pointer. We may make a pointer point to an arbitrary location however an array always points to a constant memory location.
Eg.
int a[5]={1,2,3,4,5};
int *p=a;
=>p=&a[2]; or p++; are both correct but similar statements for array 'a' will invoke error.

C Pointer placement question answer with explanation

Question 54. Find output of C snippet given below (Assume address of variable ‘a’ is 100):

int main(){
  int a=5;
  int *p=&a;
  int x=(*p)++;// P
 printf("%d %d %u ",a,x,p);
  x=*p++; //Q
  printf("%d %d %u",a,x,p);
  getch();
}

Output
6 5 100
6 6 104

Explanation:
P: Increments a by one and stores old value in x => x=5 a incremented to 6
Q: This will make pointer move ahead and give value at the location pointed by it previously =>x=6, a remains 6

Advanced c programming question answer on strings

 Question 55. Explain the difference between a character array and a character pointer.

Answer:
int main(){
    char *cp = "Hello world";//Character pointer
 
    printf("%s\n",cp);
    //will place Hello world in the read-only parts of the memory
    //and making cp a pointer to that, making any writing operation on this memory illegal. While doing:
    //*(cp+1)='a'; is runtime error
 
    char s[] = "Hello world";//array of characters
    //puts the literal string in read-only memory and copies the string to newly allocated memory on the stack.
   //Making
 
    s[0] = 'J';//correct
    printf("%s",s);
    getch();
}

Tricky C Interview question on dangling pointers


Question 56. Find output of following C code:

char *getString()
{
    char str[] = "Will I be printed?";   
    return str;
}
int main()
{
    printf("%s", getString());
    getchar();
}

Output: Some garbage value
(PS:On running in Dev C++ I am getting warning in compilation "Function returns address of local variable" and no output)
The above program doesn't work because array variables are stored in Stack Section and have a definite lifetime. So, when getString returns, value at str are deleted and str becomes <dangling pointer>.

C Interview question on library function


Question 57. C library function to find the data type of variable

Answer:

int main(){
 
    int a=2;
    typeof(a) b=1;
    printf("%d %d",a,b);//2 1
    getch();
}

C Tricky interview question with answer and explanation

Question 58

#include<stdio.h>
void change()
{
  // Add a statement here such that printf in main prints 5
}


int main()
{
int i;
i=5;
change();
i=10;
printf("%d",i); /* this should print 5*/
getch();
}

Answer:

#define printf(x,y) printf("5")
You can use macro to change the default behaviour of library functions as well.

C interview question answer on pointer to function

 Question 59. Find Output of following code:

int fun(int a){
    printf("In fun %d\n",a);
    return a+1;
}
int fun2(int a){
    printf("In fun %d\n",a);
    return a-1;
}

int main(){
    int (*ptr)(int);
    ptr=fun;
    int x=(*ptr)(3);
   
    printf("In main %d\n",x);
   
    ptr=fun2;
     x=(*ptr)(3);
   
    printf("In main %d\n",x);
    getch();
}

Output:
In main 4
In main 2
27

C Interview question on structure

Question 60. Find output of following C code

int main(){
 struct test{
    char a;
    int b;   
 }t;
 printf("%d",sizeof(t)); 
 getch();     
}

Answer: Size might be 8 and not 5 as expected

Explanation: Size of structure might NOT be sum of size of its elements. It is large enough to hold all the members. There might be unnamed holes in memory due to alignment requirements.
28

C placement question answer for technical interviews

 Question 61. Find output of following code:

short func(short x) { 
   printf(“Hello World");    // P
   return x;
}

int main() {
   printf("%d", sizeof(func(3)));
   return 0;
}

Output
2
Explanation:
P: This statement is not executed since fun() is not called.
sizeof is a compile time operator. Operand is evaluated only if it is a variable length array. In above case all that matters to sizeof is the return type of the function.

C Interview question answer on structure pointer


Question 62. Find output of following C code
struct a{
   int b;
   int c;
   int d;
}i={7,2,3};
int main(){
    struct a *p=&i;
    printf("%d %d ",*p);
    getch();
}

Answer:
7 2
If we change the printf statement to printf("%d %d %d",*p); then 7 2 3 is displayed


1 comment:

  1. Sir, nowadays in our college there is a trend going on . Interviewers are asking c programming questions on optimizations. can you suggest some optimizing tecjniques?

    ReplyDelete