Here is a listing of C test questions on “If-then-else Statements” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 1;
-
if (x > 0)
-
printf("inside ifn");
-
else if (x > 0)
-
printf("inside elseifn");
-
}
a) inside if
b) inside elseif
c)
inside if inside elseif
d) compile time error
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 0;
-
if (x++)
-
printf("truen");
-
else if (x == 1)
-
printf("falsen");
-
}
a) true
b) false
c) compile time error
d) undefined behaviour
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 0;
-
if (x == 1)
-
if (x == 0)
-
printf("inside ifn");
-
else -
printf("inside else ifn");
-
else -
printf("inside elsen");
-
}
a) inside if
b) inside else if
c) inside else
d) compile time error
Answer: c
Clarification: None.
4. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 0;
-
if (x == 0)
-
printf("true, ");
-
else if (x = 10)
-
printf("false, ");
-
printf("%dn", x);
-
}
a) false, 0
b) true, 0
c) true, 10
d) compile time error
Answer: b
Clarification: None.
5. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int x = 0;
-
if (x == 1)
-
if (x >= 0)
-
printf("truen");
-
else -
printf("falsen");
-
}
a) true
b) false
c) Depends on the compiler
d) No print statement
Answer: d
Clarification: None.
6. The C statement “”if (a == 1 || b == 2) {}”” can be re-written as ___________
a)
if (a == 1) if (b == 2){}
b)
if (a == 1){} if (b == 2){}
c)
if (a == 1){} else if (b == 2){}
d) none of the mentioned
Answer: d
Clarification: None.
7. Which of the following is an invalid if-else statement?
a) if (if (a == 1)){}
b) if (func1 (a)){}
c) if (a){}
d) if ((char) a){}
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int a = 1;
-
if (a--)
-
printf("True");
-
if (a++)
-
printf("False");
-
}
a) True
b) False
c) True False
d) No Output
Answer: a
Clarification: None.
9. What will be the output of the following C code?
-
#include -
int main()
-
{ -
int a = 1;
-
if (a)
-
printf("All is Well ");
-
printf("I am Welln");
-
else -
printf("I am not a Rivern");
-
}
a) Output will be All is Well I am Well
b) Output will be I am Well I am not a River
c) Output will be I am Well
d) Compile time errors during compilation
Answer: d
Clarification: None.
10. What will be the output of the following C code?
-
#include -
int main()
-
{ -
if (printf("%d", printf(")))
-
printf("We are Happy");
-
else if (printf("1"))
-
printf("We are Sad");
-
}
a) 0We are Happy
b) 1We are Happy
c) 1We are Sad
d) compile time error
Answer: d
Clarification: None.
