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 the following C++ code snippet?
C++
#include <iostream> int main() { int a = 5, b = 3, c = 2; int result = a + b << c; std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that '+' has higher precedence than '<<'.
✗ Incorrect
The expression is evaluated as (a + b) << c = (5 + 3) << 2 = 8 << 2 = 32.
❓ Predict Output
intermediate2:00remaining
Output of logical and bitwise operators combined
What is the output of this C++ code?
C++
#include <iostream> int main() { int x = 1, y = 2, z = 3; int result = x & y || z; std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check operator precedence between '&' and '||'.
✗ Incorrect
Bitwise AND '&' has higher precedence than logical OR '||'. So x & y is 1 & 2 = 0, then 0 || 3 is true (1). So output is 1.
❓ Predict Output
advanced2:00remaining
Result of complex expression with multiple operators
What is the output of this C++ program?
C++
#include <iostream> int main() { int a = 4, b = 2, c = 3; int result = a - b * c >> 1 + 1; std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember operator precedence: '*' then '-' then '>>' then '+'.
✗ Incorrect
Multiplication first: b * c = 2 * 3 = 6. Then subtraction: a - 6 = 4 - 6 = -2. Then addition in shift amount: 1 + 1 = 2. Then right shift: -2 >> 2. In C++ right shift of negative numbers is implementation-defined but usually arithmetic shift keeps sign, resulting in -1.
❓ Predict Output
advanced2:00remaining
Output of expression with ternary and logical operators
What is the output of this C++ code?
C++
#include <iostream> int main() { int x = 0, y = 5; int result = x ? y : y > 3 ? 10 : 20; std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Ternary operators associate right to left.
✗ Incorrect
Expression is x ? y : (y > 3 ? 10 : 20). Since x is 0 (false), evaluate y > 3 ? 10 : 20. y=5 > 3 is true, so result is 10.
🧠 Conceptual
expert3:00remaining
Operator precedence and associativity in complex expression
Consider the expression:
Which of the following correctly describes the order in which the operations are performed?
int x = 2, y = 3, z = 4; int result = x + y * z / y - z % x;
Which of the following correctly describes the order in which the operations are performed?
Attempts:
2 left
💡 Hint
Check operator precedence table: *, /, % have same precedence and associate left to right.
✗ Incorrect
Multiplication (*), division (/), and modulo (%) have the same precedence and associate left to right, so they are evaluated first in order from left to right. Then addition (+) and subtraction (-) are evaluated left to right.