0
0
C++programming~20 mins

Operator precedence in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Operator Precedence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A32
B14
C13
D16
Attempts:
2 left
💡 Hint
Remember that '+' has higher precedence than '<<'.
Predict Output
intermediate
2: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;
}
A3
B0
C1
D2
Attempts:
2 left
💡 Hint
Check operator precedence between '&' and '||'.
Predict Output
advanced
2: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;
}
A-1
B1
C0
D2
Attempts:
2 left
💡 Hint
Remember operator precedence: '*' then '-' then '>>' then '+'.
Predict Output
advanced
2: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;
}
A5
B0
C20
D10
Attempts:
2 left
💡 Hint
Ternary operators associate right to left.
🧠 Conceptual
expert
3:00remaining
Operator precedence and associativity in complex expression
Consider the expression:
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?
AMultiplication and division first (left to right), then addition and subtraction (left to right), then modulo last
BMultiplication, division, and modulo first (left to right), then addition and subtraction (left to right)
CAddition first, then multiplication and division, then subtraction and modulo
DModulo first, then multiplication and division, then addition and subtraction
Attempts:
2 left
💡 Hint
Check operator precedence table: *, /, % have same precedence and associate left to right.