C interview questions and answers -1-

1. Write a c program without using any semicolon which output will : Hello word.

Answer:
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.   
 
Answer:
#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);
    getch();
}
 
3. What is wild pointer in c ? 

Answer:
A pointer in c which has not been initialized is known as wild pointer.

Example:

(q)What will be output of following c program?

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

}

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 segmentwhile wild pointer doesn’t point any specific memory location. 

4. What is and why array in c ?

Answer:
 An array is derived data type in c programming language which can store similar type of data in continuous memory location. Data may be primitive type (int, char, float, double…), address of union, structure, pointer, function or another array.
Example of array declaration:
int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];
Array is useful when:
(a) We have to store large number of data of similar type. If we have large number of similar kind of variable then it is very difficult to remember name of all variables and write the program. For example:
//PROCESS ONE
void main(){
    int ax=1;
    int b=2;
    int cg=5;
    int dff=7;
    int am=8;
    int raja=0;
    int rani=11;
    int xxx=5;
    int yyy=90;
    int p;
    int q;
    int r;
    int avg;
    avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
    printf("%d",avg);
}
If we will use array then above program can be written as:
//PROCESS TWO
void main(){
    int arr[]={1,2,5,7,8,0,11,5,50};
    int i,avg;
    for(int i=0;i<12;i++){
         avg=avg+arr[i];
    }
            printf("%d",avg/12);
}
Question: Write a C program to find out average of 200 integer number using process one and two.
(b) We want to store large number of data in continuous memory location. Array always stores data in continuous memory location.
(q) What will be output when you will execute the following program?
void main(){
int arr[]={0,10,20,30,40};
    char *ptr=arr;
    arr=arr+2;
    printf("%d",*arr);
}
Advantage of using array:
1. An array provides singe name .So it easy to remember the name of all element of an array.
2. Array name gives base address of an array .So with the help increment operator we can visit one by one all the element of an array.
3. Array has many application data structure.
Array of pointers in c:
        Array whose content is address of another variable is known as array pointers.  For example:
void main(){
float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
}
 
5. Why we use static variable in c? Tell me all properties of static variables which you know? 

Answer:
Keyword static is used for declaring static variables in c. This modifier is used with all data types like int, float, double, array, pointer, structure, function etc.Important points about static keyword:
1. It is not default storage class of global variables. For example, analyze the following three programs and its output.
(a)
#include<stdio.h>
int a;
int main(){
    printf("%d",a);
    return 0;
}
Output: 0
(b)
#include<stdio.h>
static int a;
int main(){
    printf("%d",a);
    return 0;
}
Output: 0
(c)
#include<stdio.h>
extern int a;
int main(){
    printf("%d",a);
    return 0;
}
Output: Compilation error
At first glance if you will observe the output of above three codes you can say default storage class of global variable is static. But it is not true. Why? Read extern storage class.
2. Default initial value of static integral type variables are zero otherwise null. For example:
#include <stdio.h>
static char c;
static int i;
static float f;
static char *str;  
int main(){
    printf("%d %d %f %s",c,i,f,str);
    return 0;
}
Output: 0 0 0.000000 (null)
3. A same static variable can be declared many times but we can initialize at only one time. For example:
(a)
#include <stdio.h>
static int i;        //Declaring the variable i.
static int i=25;     //Initializing the variable.
static int i;        //Again declaring the variable i.
int main(){
    static int i;    //Again declaring the variable i.
    printf("%d",i);
    return 0;
}
Output: 25
(b)
#include <stdio.h>
static int i;        //Declaring the variable
static int i=25;     //Initializing the variable
int main(){
         printf("%d",i);
    return 0;
}
static int i=20;     //Again initializing the variable
Output: Compilation error: Multiple initialization variable i.
 
6. What is the meaning of prototype of a function ? 

Answer:
Prototype of a function
Answer: Declaration of function is known as prototype of a function. Prototype of a function means
(1) What is return type of function?
(2) What parameters are we passing?
(3) For example prototype of printf function is:
int printf(const char *, …);
I.e. its return type is int data type, its first parameter constant character pointer and second parameter is ellipsis i.e. variable number of arguments.


7.  Write a c program to modify the constant variable in c? 

Answer:
You can modify constant variable with the help of pointers. For example:
#include<stdio.h>
int main(){
    int i=10;
    int *ptr=&i;
    *ptr=(int *)20;
    printf("%d",i);
}
Output: 20 

8. What is the meaning of scope of a variable?  

Answer:
Meaning of scope is to check either variable is alive or dead. Alive means data of a variable has not destroyed from memory. Up to which part or area of the program a variable is alive, that area or part is known as scope of a variable. In the above figure scope of variable a represented outer red box i.e. whole program.
Note: If any variable is not visible it may have scope i.e. it is alive or may not have scope. But if any variable has not scope i.e. it is dead then variable must not to be visible.
There are four type of scope in c:
1. Block scope.
2. Function scope.
3. File scope.
3. Program scope.

9. What is pointer to a function? 


(1) What will be output if you will execute following code?
int * function();
void main(){
auto int *x;
int *(*ptr)();
ptr=&function;
x=(*ptr)();
printf("%d",*x);
}
int *function(){
static int a=10;
return &a;
}
Output: 10
Explanation: Here function is function whose parameter is void data type and return type is pointer to int data type.
x=(*ptr)()
=> x=(*&functyion)() //ptr=&function
=> x=function() //From rule *&p=p
=> x=&a
So, *x = *&a = a =10
(2) What will be output if you will execute following code?
int find(char);
int(*function())(char);
void main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
}
int find(char c){
return c;
}
int(*function())(char){
return find;
}
Output: 65
Explanation: Here function whose name is function which passing void data type and returning another function whose parameter is char data type and return type is int data type.
x=(*ptr)(‘A’)
=> x= (*function ()) (‘A’) //ptr=function ()
//&find=function () i.e. return type of function ()
=> x= (* &find) (‘A’)
=> x= find (‘A’) //From rule*&p=p
=> x= 65
(3) What will be output if you will execute following code?
char * call(int *,float *);
void main(){
char *string;
int a=2;
float b=2.0l;
char *(*ptr)(int*,float *);
ptr=&call;
string=(*ptr)(&a,&b);
printf("%s",string);
}
char *call(int *i,float *j){
static char *str="c-pointer.blogspot.com";
str=str+*i+(int)(*j);
return str;
}
Output: inter.blogspot.com
Explanation: Here call is function whose return type is pointer to character and one parameter is pointer to int data type and second parameter is pointer to float data type and ptr is pointer to such function.
str= str+*i+ (int) (*j)
=”c-pointer.blogspot.com” + *&a+ (int) (*&b)
//i=&a, j=&b
=”c-pointer.blogspot.com” + a+ (int) (b)
=”c-pointer.blogspot.com” +2 + (int) (2.0)
=”c-pointer.blogspot.com” +4
=”inter.blogspot.com”
(4) What will be output if you will execute following code?
char far * display(char far*);
void main(){
char far* string="cquestionbank.blogspot.com";
char far *(*ptr)(char far *);
ptr=&display;
string=(*ptr)(string);
printf("%s",string);
}
char far *display(char far * str){
char far * temp=str;
temp=temp+13;
*temp='\0';
return str;
}
Output: cquestionbak
Explanation: Here display is function whose parameter is pointer to character and return type is also pointer to character and ptr is its pointer.
temp is char pointer
temp=temp+13
temp=’\0’
Above two lines replaces first dot character by null character of string of variable string i.e.
"cquestionbank\0blogspot.com"
As we know %s print the character of stream up to null character.
 
10.  Write a c program to find size of structure without using sizeof operator ? 

Answer:
struct  ABC{
    int a;
    float b;
    char c;
};
void main(){
    struct ABC *ptr=(struct ABC *)0;
    ptr++;
    printf("Size of structure is: %d",*ptr);
}

11. What is size of void pointer ? 

Answer:
Size of any type of pointer in c is independent of data type which is pointer is pointing i.e. size of all type of pointer (near) in c is two byte either it is char pointer, double pointer, function pointer or null pointer.  Void pointer is not exception of this rule and size of void pointer is also two byte.

12. Do you know nested loop. Explain by an example ?

Answer:
A loop inside another loop is known as nested loop. We can write any loop inside any loop in c i.e. we can write for loop inside the loop or while loop or do while loop etc. For example:
(a)
void main(){
       int i,j,k;
       for(i=0;i<3;i++){
         for(j=0;j<3;j++){
             printf(" %d",i+j);
         }
       }
    getch();
}
(b)
void main(){
       int i,j,k;
       do
     while(0)
         for(;0;)
             printf("cbyexample");
       while(0);
    getch();
}
 
 
 
 

1 comentarii:

Anonymous said...

good job with this questions.. I wait for the next set of questions

Post a Comment