Challenge - 5 Problems
Operator Precedence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mixed arithmetic and bitwise operators
What is the output of this C code snippet?
C
int main() { int a = 5, b = 3, c = 2; int result = a + b << c; printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Remember that bitwise shift operators have lower precedence than addition.
✗ Incorrect
The expression is evaluated as (a + b) << c = (5 + 3) << 2 = 8 << 2 = 32, since + has higher precedence than <<. The output is 32.
❓ Predict Output
intermediate2:00remaining
Output of logical and bitwise operators combined
What is the output of this C code?
C
int main() { int x = 1, y = 0; int z = x && y & x; printf("%d", z); return 0; }
Attempts:
2 left
💡 Hint
Check operator precedence between && and & operators.
✗ Incorrect
The & operator has higher precedence than &&, so y & x is evaluated first: 0 & 1 = 0. Then x && 0 is evaluated: 1 && 0 = 0. So the output is 0.
❓ Predict Output
advanced2:00remaining
Result of complex expression with mixed operators
What is the output of this C program?
C
int main() { int a = 4, b = 2, c = 3; int result = a - b * c / b + c; printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Remember multiplication and division have higher precedence than addition and subtraction.
✗ Incorrect
First, b * c / b = (2 * 3) / 2 = 6 / 2 = 3 (left-to-right). Then a - 3 + c = 4 - 3 + 3 = 4 (left-to-right for - and +). The output is 4.
❓ Predict Output
advanced2:00remaining
Output of expression with ternary and logical operators
What does this C code print?
C
int main() { int x = 0, y = 5; int z = x ? y : y > 3 ? 10 : 20; printf("%d", z); return 0; }
Attempts:
2 left
💡 Hint
Ternary operators associate right to left. Evaluate carefully.
✗ Incorrect
Since x is 0 (false), the expression evaluates the second ternary: y > 3 ? 10 : 20. y is 5, which is greater than 3, so the result is 10.
❓ Predict Output
expert3:00remaining
Value of variable after complex expression with increments and precedence
What is the value of variable x after running this C code?
C
int main() { int x = 3; int y = x++ * ++x + x-- - --x; printf("%d", x); return 0; }
Attempts:
2 left
💡 Hint
Remember the order of evaluation and side effects of post and pre increments.
✗ Incorrect
This expression has undefined behavior due to multiple modifications to x without sequence points. Assuming left-to-right operand evaluation (common in many compilers):
- x++: uses 3, x=4
- ++x: x=5, uses 5
- x++ * ++x = 3*5=15
- x--: uses 5, x=4
- --x: x=3, uses 3
- 15 + 5 - 3 = 17 (y=17)
Final x=3, prints 3.