Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : if statement - 2Next Topic : if else - 2

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 C program?
#include<stdio.h>
int main()
{       if(sizeof(0))
		printf("Hello");
	else
		printf("Bye");
	return 0;
}
a)  Bye
b)  Hello
c)  Bye Hello
d)  Syntax Error
Correct Answer : b
Explanation : The sizeof(0) gives the sizeof int, that is either 2 or 4. In c if treats non zero value as true, so the condition of if statement is true.


Q2: What will be the output of the C program?

#include<stdio.h>
int main()
{	int a=5,b=5;
	if(a == b);
		printf("Same");
	else
		printf("Not Same");
	return 0;
}
a)  Same
b)  Not Same
c)  Syntax Error
d)  Runtime Error
Correct Answer : c
Explanation : else block needs to follow directly after the statement of if block. In the above program, the if statement ends with semicolon so there is no block of statements under if block. The printf statement after if is normal one. So else block is misplaced in this program.


Q3: What will be the result of compiling and executing the following code?
#include <stdio.h>
int main()
{   int a=0;
    if (a);
    else
        printf("Pappu");
    return 0;
}
a)  Pappu
b)  Nothing will be printed
c)  Runtime Error
d)  Syntax Error
Correct Answer : a
Explanation : The variable a is initialized with value 0. The Else block is executed as the expression within if evaluates to FALSE. Remember that the empty block is equivalent to a semi-colon(;). So the statements if (a); and if (a) {} are equivalent.


Previous Topic : if statement - 2Next Topic : if else - 2