C Programs Interview Questions and Answers with Explanations:

C Interview Questions Programs with Solutions

This is the post full of problems…..  Yes, this is an exercise, you have to solve your own and find the output of the programs give below.

Following are the set of C programs. Just try to find the output for them on your own, then check for answers.

Nowadays, in many interviews you can find such programs asked to know your technical skill.

You may also take this as such a test. Lets Test Your Technical and Programming Knowledge……….

Interview Questions C Programs Solved:

[Question – 1]
main()

{
char ch=291;
printf(“%d %d %c”,3200,ch,ch);
}

ANSWER:   3200 291 #

EXPLANATION:
For first variable the value displayed as it is : 3200
For next %d, the character is converted to integer 291 is displayed.
for last %c the character equal to 291 # is displayed.

[Question – 2]
main()

{
int a,b;
a=-3 – -3;
b=-3 – -(-3);
printf(“a=%d b=%d b=%d”, a, b);
}

ANSWER:  a=0 b=- 6

EXPLANATION:
For a=%d,  the value of a (-3 – -3) = -3+3 =0 is displayed.
For  b=%d, the value of b (-3 – – (-3)) = -3-3 = -6  is displayed.
For the last b=%d, the value of nothing is specified. Hence ignored. The final output will be  a=0, b=-6.

[Question – 3]
main()

{
int x;
x=3*4%5;
printf(“x=%d”, x);
}

ANSWER: x=2

EXPLANATION:
The value of x= 3*4%5 = 12 %(mod)  5= remainder 2;  x=2;

Here as per operator precedence, both * and % have equal precedence. As we have higher preference from left to right.
Hence * (multiplication) is done at first , then modulo operation is performed.

[Question – 4]
main()

{
float a=5,b=2;
int c;
c=a/b;
printf(“%d”, c);
}

ANSWER: Error Message : Illegal use of floating point in function main

EXPLANATION:
c=5/2 = 0.5. Here the float value cannot be converted into int without any explicit information.
Hence error message will be the output.

[Question – 5]
main()

{
int  x=10, y=5, i,j;
i=x>9;
j=x>3&&y!=3;
printf(“i =%d, j=%d”, i,j);
}

ANSWER: i=1 j=1

EXPLANATION:
i= x> 9 = 10>9 = true. Hence output i=1 (the value of true is 1; false is 0;)
j= x>3 && y!=3  =  10(the value of  x) > 3 && (AND condition: both conditions must true)  5(the value of y) not equals to 3.
Here both conditions are true . So the output also true: the output i=1 and j=1.

[Question – 6]
main()

{
int a=100, b=200;
int c;
c= (a= = 100 || b>200);
printf(“c=%d”,c);
}

ANSWER:  c=1

EXPLANATION:
c= (a == 100 || b>200)

Here || is an OR condition symbol which means if either one of the condition is true then the answer is true.
Now c = (100[the value of a] = 100 is true;  200[the value of b] > 200 is false). As per OR condition, the output is TRUE: 1. 

[Question – 7]
main()

{
int a=300, b= 10, c=20;
if(!a>=400)
b=300;
c=200;
printf(“b=%d, c=%d”,b,c);
}

ANSWER:  b=300, c=200

EXPLANATION:
In the condition (!a>=400), ‘!’  means NOT condition. Here the condition: ( 300 (the value of a) is not greater than 400) is true.
Hence the printf statement displayed: b = 300 and c = 200.

Here, even though the value of b and c is declared before if condition, it won’t taken into consideration. Because, the if condition is true.
If the if condition is false, then the values of b will 10 and c will be 20. 

[Question – 8]
main()

{
if(!3.14)
printf(“Its not the pie value”);
else
printf(“It is the pie value”);
}

ANSWER: It is the pie value

EXPLANATION:
Here the if condition itself  can’t check. Hence, the else part will be taken into account.
The output will be the else part printf statement: ” It is the pie value”.

[Question – 9]
main()

{
float a=12.25, b= 13.65;
if(a==b)
printf(“a and b are equal”);
else
printf(“a and b are not equal”);
}

ANSWER: a and b are equal

EXPLANATION:
Here, the  if condition: (a = =b) : (12.25 = = 13.65) is not true.
Hence the output will be the else part output statement: a and b are not equal. 

[Question – 10]
main()

{
int i=10, j=40;
if(((j-i) % 10) = = 3 )
printf(“The right program”);
else
printf(“The Wrong program”);
}

ANSWER: The wrong program.

EXPLANATION:
Here the if condition: (((j-i) % 10)=3) : (30%10)=3  is false.

Hence the output will be the printf of else part: “The Wrong Program”.

Thanks for reading about C Programming Interview Questions and Answers with Explanations

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *