The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
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.
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; }
Option: B
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.
*(ptr + 0) = 2
*(ptr + 1) = 3
*(ptr + 2) = 5
*(ptr + 3) = 7
*(ptr + 4) = 11
*(ptr + 5) = 13
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; }
Option: C
The condition if(i == (1, 2)) is if(expression1 == (expression2, expression3)). The expression3 is evaluated last so the if block gets executed.
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; }
Option: D
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; }
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; }
Option: A
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; }
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; }
Option: C
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.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019