0
0
Cprogramming~20 mins

Ternary operator in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ternary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested ternary operator
What is the output of this C code snippet?
C
int x = 5, y = 10;
int result = x > y ? x : y > 7 ? y : x;
printf("%d", result);
A5
B10
C7
DCompilation error
Attempts:
2 left
💡 Hint
Remember how nested ternary operators are evaluated from left to right.
Predict Output
intermediate
2:00remaining
Ternary operator with side effects
What will be printed by this code?
C
int a = 3, b = 4;
int c = (a > b) ? (a += 2) : (b += 3);
printf("%d %d %d", a, b, c);
A3 4 4
B5 4 5
CCompilation error
D3 7 7
Attempts:
2 left
💡 Hint
Check which condition is true and how the variables change.
Predict Output
advanced
2:00remaining
Ternary operator with pointer dereference
What is the output of this code?
C
int x = 10, y = 20;
int *p = (x > y) ? &x : &y;
printf("%d", *p);
A10
B0
C20
DSegmentation fault
Attempts:
2 left
💡 Hint
Check which pointer is assigned based on the condition.
Predict Output
advanced
2:00remaining
Ternary operator with different types
What will this code print?
C
int a = 5;
float b = 3.5;
float c = (a > 3) ? a : b;
printf("%.1f", c);
A5.0
BCompilation error
C5
D3.5
Attempts:
2 left
💡 Hint
The ternary operator promotes types to a common type.
🧠 Conceptual
expert
2:00remaining
Ternary operator associativity
Consider the expression: int x = 1 ? 0 ? 2 : 3 : 4; What is the value of x after this executes?
A3
B2
C4
DCompilation error
Attempts:
2 left
💡 Hint
Ternary operators associate right to left in C.