Saturday 31 January 2015

C interview questions and answers




C interview questions and answers for freshers. It is basic c language technical frequently asked interview questions and answers. It includes data structures, pointers interview questions and answers for experienced
Interview questions and answer of C with explanation for fresher

1
Write a c program to print Hello world without using any semicolon.
Explanation:
Solution: 1
void main(){
    if(printf("Hello world")){
    }
}

Solution: 2
void main(){
    while(!printf("Hello world")){
    }
}

Solution: 3
void main(){
    switch(printf("Hello world")){
    }
}

2
Swap two variables without using third variable.
Explanation:
#include<stdio.h>
int main(){
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    return 0;
}

3
What is dangling pointer in c? 
Explanation:
Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Initially:

Later:
For example:

What will be output of following c program?

#include<stdio.h>

int *call();
int main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){

int x=25;
++x;

return &x;
}

Output: Garbage value
Note: In some compiler you may get warning message returning address of local variable or temporary

Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Solution of this problem: 
Make the variable x is as static variable. In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

#include<stdio.h>

int *call();
int main(){
int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){

static int x=25;
++x;

return &x;
}

Output: 26

4
What is wild pointer in c?  
Explanation:
A pointer in c which has not been initialized is known as wild pointer.

Example:

What will be output of following c program?

int main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
return 0;
}

Output: Any address
Garbage value

Here ptr is wild pointer because it has not been initialized. There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segment while wild pointer doesn’t point any specific memory location.

5
What are merits and demerits of array in c?
Explanation:
Merits:

(a) We can easily access each element of array.
(b) Not necessity to declare too many variables.
(c) Array elements are stored in continuous memory location.

Demerit:

(a) Wastage of memory space. We cannot change size of array at the run time. 
(b) It can store only similar type of data.

6
Do you know memory representation of int a = 7 ?   
Answer

7
What is and why array in c?
Answer

8
Why we use do-while loop in c? Also tell any properties which you know?  
Explanation:
It is also called as post tested loop. It is used when it is necessary to execute the loop at least one time. Syntax:

do {
Loop body
} while (Expression);

Example:

int main(){
    int num,i=0;
   
    do{
         printf("To enter press 1\n");
         printf("To exit press  2");
         scanf("%d",&num);
         ++i;
         switch(num){
             case 1:printf("You are welcome\n");break;
             default : exit(0);
         }
    }
    while(i<=10);
    return 0;
}

Output: 3 3 4 4

If there is only one statement in the loop body then braces is optional. For example:

(a)
int main(){
    double i=5.5678;
    do
         printf("hi");
    while(!i);
    return 0;
}

Output: 3 3 4 4

(b)
int main(){
    double i=5.63333;
    do
         printf("hi");
    while(!i);
    return 0;
}

Output: hi

(c)
int main(){
     int x=25,y=1;
     do
       if(x>5)
         printf(" ONE");
       else if(x>10)
         printf(" TWO");
       else if(x==25)
         printf(" THREE");
       else
         printf(" FOUR");
       while(y--);
return 0;
}

Output: ONE ONE

No comments:

Post a Comment