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

25 comments:

Unknown said...

usefull

Unknown said...

its useless

Anonymous said...

lavanya dont u knoe anything.i thing u have two tunk

Anonymous said...

very
usefull

loga said...

regarding the modification of a constant variable using pointer, i tried the same code.. but it is not being changed.. can u help me pls...

Unknown said...

yes loga is write,its not changing,i have also tried it,can u help as? plz

Priyanka kumari said...

Hi Loga and Ravi,
Could you please tell me the c compiler where its not cahnging.

RAHUL said...

this is very gud......

Chhote Lal said...

Please add new questions with answer.

AKHIL VM said...

onnum parayanillaa...heavvy

Nupur said...

Excellent to learn.. Thanku.

Anonymous said...

how the ans is 6not 5 in que. 10

Priyanka kumari said...

In Question no. 10 when value of i =5 then loop condition is true so value of i will be incremented once again i.e. i=6. Now control will come out the for loop since loop condition is false.

Anonymous said...

the answer for 10th will be 5
because of post increment...

kalai said...

main()
{
char ch[10];
int a=6,b;
printf("Enter value for b:");
scanf("%d",&b);
printf("\nEnter your choice:");
ch=getchar();
switch(ch)
{
case '+':printf("a=%d",a);
break;
case '-':printf("b=%d",b);
break;
default:printf("check the input");
}
getch();
}
what is output for this program?

kalai said...

answer for 10th ques is "infinite loop".becoz loop ends with semicolon..

sathya said...

can u explain me 18th question?

Deepesh said...

Ritesh kumar is right..ans is 6..n his explaination is also satisfactory..
@kalai..who told u dt a loop which ends wid semocolon is infinite loop.??
such loop incr/decr until it satisfies the condition..
like i=5 in 10th question..
bt when it again incremented,i become equal to 6..and condition failed..bt the value i=6 resists..

Deepesh said...

@kalai- dude..in your question ..u hv used character array ch..bt getchar can take 1 value at a time..so,jst make "ch" a character constant...i.e char ch;

also operators lyk +,- or expressions lyk a+b or float values are not allowed as a value to switch..
whereas 3+4 as value is perfectly fine..

Anonymous said...

its usefull

ARVIND YADAV said...

thankxxxxxxxxxxxx

SBM.blogspot.com said...

write the program for using for loop[perfect logic]
Example:Enter the value:5
* * * * *
* *
* *
* *
* * * * *

Anonymous said...

#include
int main()
{
extern int i;
i=20;
printf("%d/n",size of(i));
return 0;
}

Subhajit Roy said...

can any one pls explain the working of '!' operator in Q.14??
int x = sizeof(!5.856);

and also Q 18....

sandeep kumar said...

(!operand)=0 if operand is non zero.
(!operand)=1 if operand is zero.
In Q.14 operand is non zero so
int x=sizeof(0) is 2 for 16 bit.