c interview questions



         COMMONLY ASKED QUESTIONS IN INTERVIEW

1. In the following declaration statement
    char c=’A’;
Variable c stores one byte of memory space while character constants ‘A’ stores one byte memory space. How one byte variables can stores two byte character constant?
2. What is automatic type promotion in c?
3. Swap two variables without using third variable.
4. Write a c program without using any semicolon which output is: Hello word.
5. How will you modify a const variable in c?
6. Write a c program to find out factorial of given number using function recursion.
7. Give an example in which comma is behaving as separator as well as operator in c.
8.  What is variable number of arguments in c?
9. What is command line argument?
10. What will be output of following c code?
void main(){
    int i;
    for(i=0;i<=5;i++);
         printf("%d",i);
    getch();
}

11. Subtract two integer numbers without using subtraction operator.
12. What are differences between sizeof operator and strlen function?
13. Declared a variable in c without defining it.
14. int x=sizeof(!5.856);

What will value of variable x?
15. What is difference between initialization and assignment of a variable?
16.  Tell me something about auto storage class.
17. What will be output of following c code?
extern int a;
void main(){
    int a=5;
    {
         int a=10;
         printf("%d",a++);
    }
    printf(" %d",a);
    getch();
}
int a=20;

18. Declare a pointer which can point printf function in c.
19. What are merits and demerits of array in c?
20.  Tell me a all sorting algorithm which you know.







Answer:

1.  Character constant reserve two byte of memory space to represent octal or hexadecimal character constant but char variable stores only its one byte ASCII value.
2. In c if two operands are of different data type in a binary operation then before performing any operation compiler will automatically convert the operand of lower data type to higher data type .This phenomenon is known as automatic type conversion. For example:
int a=10,c;
float b=5.5f;
c=a+b;
Here a int variable while b is float variable. So before performing addition operation value of the variable a (Lower data type) will automatically convert into float constant (higher data type) then it will perform addition operation.
3.
Solution: 1  
void main(){
    int x,y;
    scanf("%d%d",&x,&y);
    //swapping
    x=x+y;
    y=x-y;
    x=x-y;
    printf("%d %d",x,y);
}
Solution: 2

void main(){
    int x,y;
    scanf("%d%d",&x,&y);
    //swapping
    x=x^y;
    y=y^x;
    x=x^y;
    printf("%d %d",x,y);
}
4.
Solution: 1
void main(){
    if(printf("Hello world")){
    }
}

Solution: 2

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

Solution: 3

void main(){
    switch(printf("Hello world")){
    }
}

5.

We can modify the const variable with the help of pointer.

void main(){
    const int a=10;
    int *ptr=(int *)&a;
    *ptr=20;
    clrscr();
    printf("%d",a);
    getch();
}

Output: 20

6.
void main()
{
  long num,f;
  clrscr();
  printf("Input a number: ");
  scanf("%ld",&num);
  f=fact(num);
  printf("\nFactorial is %ld",f);
  getch();
}
int fact(long n)
{
   if(n==0)
            return 1;
   else
            return(n*fact(n-1));
}

7.
void main(){
    int x=5,y=10,z=15,val;
    val=sum(x,(y=0,z=0,y),z);
    clrscr();
    printf("%d",val);
    getch();
}
sum(int x,int y,int z){
    return x+y+z;
}

Output: 20
Note: In the above program comma in red color are behaving as operator.

8.

A function in which we can pass variables numbers of argument in function call statement such functions are known as function with variable number of arguments. For example:

void display(int,...);
void main(){
    display(1,2);
    display(1,2,3,4);
    display(1,2,3,4,5,6,7,8,9);
    getch();
}
void display(int x,...){
}

Note: There consecutive dot is known as ellipsis in c.
9.
Getting the arguments from command prompt in c is known as command line arguments.  In c main function has three arguments. They are:
(a)Argument counter
(b)Argument vector
(c)Environment vector
For example:
void main(int argc,char *argv[],char *env[]){
    int i;
    for(i=1;i<argc;i++){
         printf("%s\n",argv[i]);
    }
}

10. 6
11.
void main(){
    int a,b,d;
    scanf("%d%d",&a,&b);
    d=a+~b+1;
    printf("%d",d);
    getch();
}

12.
sizeof is keyword of c which can find size of a string constant including null character but strlen is function which has been defined string.h and can find number of characters in a string excluding null character.  
#include<string.h>
void main(){
    int a,b;
    a=strlen("cquestionbank");
    b=sizeof("cquestionbank");
    printf("%d  %d",a,b);
    getch();
}

13. extern int a;
Note: Uninitialized extern variables are example of declaration of variables.
14. 2
17. 10 5
18. int (*ptr)(char const,…);

19.
(a) We can easily access each element of array.
(b) Not necessity to declare two 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.

20.
(a)Bubble sort
(b)Selection sort
(c)Insertion sort
(d)Quick sort
(e)Merge sort

Post a Comment