C Programming MCQ : Conditional Operators (Multiple Choice Questions)
Question 1 |
Guess the output of the following program ?
#include<stdio.h> int main() { int x=10; printf("%d, %d, %d\n", x <= 10, x = 40, x >= 10); return 0; }
0, 40, 1 | |
0, 1, 1 | |
1, 1, 1 | |
1, 40, 1 |
Question 1 Explanation:
x <= 10 //Returns True i.e 1 x = 40 //Assigns 40 i.e True x >= 10 //Returns True i.e 1
Question 2 |
Predict the output of the following code ?
#include<stdio.h> int main() { int i = 10; printf("%d, %d\n", ++i, i++); return 0; }
12, 10 | |
12, 12 | |
Output may Vary from Compiler to Compiler | |
12, 11 |
Question 2 Explanation:
In case of GCC Compiler Output will be 12,10. Output may very from compiler to compiler because order of evaluation inside printf is different for different compiler.
Question 3 |
Guess the output ?
#include<stdio.h> int main() { int k, num = 100; k = (num > 50 ? (num <= 10 ? 100 : 200): 500); printf("%d\n", num); return 0; }
300 | |
200 | |
500 | |
100 |
Question 4 |
Guess the output of the following program ?
#include<stdio.h> int main() { int k; k = (1,2,3,4,5); printf("%d\n", k); return 0; }
5 | |
2 | |
4 | |
1 |
Question 4 Explanation:
- The comma operator has left-right associativity.
- The left operand is always evaluated first, and the result of evaluation is discarded before the right operand is evaluated.
- Comma Operator returns rightmost operator i.e 5
Question 5 |
Guess the output of the following program ?
#include<stdio.h> int main() { int k; k = 1,2,3,4,5; printf("%d\n", k); return 0; }
4 | |
2 | |
5 | |
1 |
Question 5 Explanation:
- Comma Operator has lowest precedence than Assignment operator.
- Value 1 is assigned first to variable 'k' and then comma operators will be evaluated.
There are 5 questions to complete.