Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : if else - 1Next Topic : switch statement - 1

This section focuses on C interview questions, tricky questions, and puzzles, and viva questions based on “if else statement”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. It will improve their understanding of C Language. Here is a listing of C questions and puzzles on “if else statement” along with answers and/or explanations.

Q1: What will be the output of the following C code?

    #include <stdio.h>
    void main()
    {
        int x = 5;
        if (x + 1)
            printf("hello");
        if (x == 5)
            printf(" hi");
        else
            printf(" no");
    }
a)  hello
b)  hello hi
c)  hello no
d)  Compilation error
Correct Answer : b
Explanation : There are two if statements. Condition of first if is true so it will print hello, similarly condition of second if is also true so it will print hi. Learn c from scratch


Q2: What will be the output of the following c code?
#include <stdio.h>
int main()
{       int x=3,y=5;
	if(3==3)
		printf("%d ",x);
	else;
		printf("%d",y);	
	return 0;
}
a)  3
b)  5
c)  3 5
d)  Syntax Error
Correct Answer : c
Explanation : The condition of if is true so it prints the value of x, that i 3. The else is terminated by ; so the statement after the else is not controlled by else, it will print value of y, that is 5.
Interview questions with solution


Q3: What will be the output of the following C code?

    #include <stdio.h>
    void main()
    {
        int n = 10;
        if (n < 5);
            printf("India",n);
 
    }
a)  No output
b)  India
c)  India5
d)  Syntax Error
Correct Answer : b
Explanation : The if statement is terminated by ;, so the printf() after if is not controlled by the if statement. Sp printf() will be executed regardless the result of the condition.


Q4: What will be the output of the following c code?
#include<stdio.h>
int main()
{	int a = 10, b = 20, c = 30;
	if(a++ < b-10 == c-30)
		printf("%d %d %d", a++, ++b, --c);
	else	
		printf("%d %d %d", a, b, c);
	return 0;
}
a)  11 0 0
b)  11 20 30
c)  11 21 29
d)  10 20 30
Correct Answer : c
Explanation : The condition of if statement is true, because a++ < b-10 yields 0, similarly c-30 is also 0, so 0==0 results in 1.
Watch this video to understand post and pre increment/decrement
Learn if else from scratch
Learn c from scratch


Q5: What will be the output of following c code?
#include"stdio.h" 
int main() 
{   int num; 
    if(num=0,2,4) 
        printf("C Institute!"); 
    else
        printf("How is it?"); 
    printf(" %d",num); 
} 
a)  How is it? 4
b)  C Institute!
c)  How is it? 0
d)  C Institute! 0
Correct Answer : d
Explanation : At first zero will assign in ‘num’ then comma operator returns the last value which is 4 and condition becomes true. So output will be C Institute! 0.
Learn C In Depth


Q6: What will be the output of the C program?
#include<stdio.h>
void main()
{    int a = 15;
     if(a=a-5>4)
	  printf("Hows the josh");
     else
	  printf("High Sir");
}
a)  Hows the josh
b)  High Sir
c)  Hows the joshHigh Sir
d)  Hows the josh High Sir
Correct Answer : a
Explanation : The expression evaluates in following way. a=15-5>4
a=10>4
a=1
So the condition of if is true.


Q7: What will be the output of the C program?
#include<stdio.h>
int main()
{	int i = 1, j = 0 ;
	if(i-- == j)
		printf("i=%d", --i);
	else
		printf("j=%d", ++j);
	return 0;
}
a)  i=0
b)  i=-1
c)  j=0
d)  j=1
Correct Answer : d
Explanation : The if(i-- == j) will be if(1 == 0). It uses post decrement operator so, first the value i is used then it decrementd by one. i.e) if(1 == 0) condition is false so it executes the else block. in the printf inside else part pre increment is used so value of j is increased by 1, so j=1 is printed.


Q8: What will be the output of the C program?
#include<stdio.h>
int main()
{	int  a=5;
	if(a==50);
		a=25;
	if(a==25) 
		a=a+a;
	else;
		a=a+1;
	printf("%d", a);
	return 0;
}
a)  6
b)  50
c)  51
d)  Syntax Error
Correct Answer : c
Explanation : First if is terminated by ;, so 25 is assigned into a. Condition of second if is true, so a stores 50, here else is terminated by ;, so a is increased by 1, the final value of a will be 51;


Q9: What will happen when we compile and run the below c code?
#include<stdio.h>
int main()
{
   float a=23.45,b=23.54;
   if(a=b)
     printf("a and b are equals");
   else
     printf("a and b are not equals");
}
a)  a and b are not equals
b)  a and b are equals
c)  Runtime errror
d)  Compile time error
Correct Answer : b
Explanation : In if assignment operator is used, so value of b is assigned to a, the value of a is non zero, so it will be treated as condition true.
Find explanation here


Previous Topic : if else - 1Next Topic : switch statement - 1