The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C if else Tricky Questions

In most of the MNC interview questions such as in ZOHO interview question, IVTL Infoview interview questions, Amazon interview questions, GOOGLE interview questions, Infosys interview questions and even in Voonik interview questions, We come across several Tricky C Questions about which 2:5 of the questions are from if else in c. Solving that kind of tricky C questions is not an easy task for all C programmers. We need more practices to solve it with ease. So we provide 25+ interesting C questions in if else to make your MNC interview very easy.

C if else Questions

16. What will be the output of the C program?

#include<stdio.h>
int main()
{
	int i = 5, j = 6, k = 7;
	if(i > j == k)
		printf("%d %d %d", i++, ++j, --k);
	else	
		printf("%d %d %d", i, j, k);
	return 0;
}

A. 5 7 6

B. 5 6 7

C. 6 6 6

D. 5 7 7

x

 

Option: B

Explanation

The if statement condition is (i > j == k) becomes (5 > 6 == 7) its looks like (expression1 == expression2), expression1 becomes 0 and expression2 is a non-zero value so expression2 returns 1. Finally the condition is False (0 == 1) so else block gets executed.

For Example

*(ptr + 0) = 2
*(ptr + 1) = 3
*(ptr + 2) = 5
*(ptr + 3) = 7
*(ptr + 4) = 11
*(ptr + 5) = 13

Answer


17. What will be the output of the C program?

#include<stdio.h>
int main()
{
	int i = 2;
	if(i == (1, 2))
		printf("Hai");
	else
		printf("No Hai");
	return 0;
}

A. Compilation Error

B. Runtime Error

C. Hai

D. No Hai

x

 

Option: C

Explanation

The condition if(i == (1, 2)) is if(expression1 == (expression2, expression3)). The expression3 is evaluated last so the if block gets executed.

Answer


18. What will be the output of the C program?

#include<stdio.h>
int main(){
char str[8] = "2braces";
char str1[8] = "2braces";
   if(str == str1)
	   printf("Strings are Equal");
   else
	   printf("Not Equal");
return 0;
}

A. Compilation Error

B. Run time error

C. Strings are Equal

D. Not Equal

x

 

Option: D

Explanation

String cannot be compared normally using == operator, if we compared two strings using == operator, a character alone will be compared which usually some random special character.

#include<stdio.h>
int main(){
	char str[8] = "2braces";
	printf("%c",str);
	return 0;
}

Answer


19. What will be the output of the C program?

#include<stdio.h>
int main(){
	int i = 5;
    if(i == 3, 4)
     	printf("Hai");
    else
		printf("No Hai");
	return 0;
}

A. Hai

B. No Hai

C. Compilation Error

D. None of the Above

x

 

Option: A

Explanation

The if statement in the above C program is if(i == 3, 4) which means if(expression1 == expression2), expression1 is a direct non-zero value so it always returns 1 and expression 2 has a , operator it acts as a seperate expression and returns 1. So the condition is true and if block gets executed. But if you run the below program:

#include<stdio.h>
int main(){
	int i = 5;
	if(i == 3)
		printf("Hai");
	else
		printf("No Hai");
	return 0;
}

This C program will print No Hai.

Answer


20. What will be the output of the C program?

#include<stdio.h>
int main(){
	char *str = {"2braces"};
	char *str1 = {"2braces"};
	if(*str == *str1)
		printf("inside if block");
	else 
		printf("inside else block");
	return 0;
}

A. Runtime Error

B. Compilation Error

C. inside if block

D. inside else block

x

 

Option: C

Explanation

In the above C program, while comparing two strings using pointer character variables, the *str and *str1 will takes complete memory location to compare two strings.

Answer


Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report