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;
}
A
0, 40, 1
B
0, 1, 1
C
1, 1, 1
D
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;
}
A
12, 10
B
12, 12
C
Output may Vary from Compiler to Compiler
D
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;
}
A
300
B
200
C
500
D
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;
}
A
5
B
2
C
4
D
1
Question 4 Explanation: 
  1. The comma operator has left-right associativity.
  2. The left operand is always evaluated first, and the result of evaluation is discarded before the right operand is evaluated.
  3. 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;
}
A
4
B
2
C
5
D
1
Question 5 Explanation: 
  1. Comma Operator has lowest precedence than Assignment operator.
  2. Value 1 is assigned first to variable 'k' and then comma operators will be evaluated.
There are 5 questions to complete.