Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which operator has the highest precedence in the expression 3 + 4 * 5?
easy
A. Addition (+)
B. Subtraction (-)
C. Multiplication (*)
D. Division (/)

Solution

  1. Step 1: Identify operators in the expression

    The expression has addition (+) and multiplication (*).
  2. Step 2: Recall operator precedence rules

    Multiplication (*) has higher precedence than addition (+), so it is evaluated first.
  3. Final Answer:

    Multiplication (*) -> Option C
  4. Quick Check:

    Multiplication before addition = Multiplication (*) [OK]
Hint: Multiplication and division come before addition and subtraction [OK]
Common Mistakes:
  • Thinking addition runs before multiplication
  • Ignoring operator precedence rules
  • Assuming left-to-right always applies
2. Which of the following expressions is syntactically correct in C++?
easy
A. int x = 5 + (3 * 2);
B. int x = 5 + 3 *;
C. int x = 5 + *3;
D. int x = 5 + 3 * 2);

Solution

  1. Step 1: Check each expression for syntax errors

    int x = 5 + *3; has an invalid use of * before 3. int x = 5 + 3 *; ends with * which is incomplete. int x = 5 + 3 * 2); has an unmatched closing parenthesis.
  2. Step 2: Confirm correct syntax

    int x = 5 + (3 * 2); uses parentheses correctly and has valid operators and operands.
  3. Final Answer:

    int x = 5 + (3 * 2); -> Option A
  4. Quick Check:

    Valid syntax with parentheses and operators = int x = 5 + (3 * 2); [OK]
Hint: Check for balanced parentheses and proper operator placement [OK]
Common Mistakes:
  • Misplacing operators without operands
  • Unbalanced parentheses
  • Using operators incorrectly
3. What is the output of the following code?
int a = 2, b = 3, c = 4;
int result = a + b * c / a - b;
std::cout << result;
medium
A. 5
B. 2
C. 4
D. 3

Solution

  1. Step 1: Evaluate multiplication and division first

    b * c = 3 * 4 = 12; then 12 / a = 12 / 2 = 6.
  2. Step 2: Evaluate addition and subtraction left to right

    a + 6 = 2 + 6 = 8; then 8 - b = 8 - 3 = 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Multiplication/division before addition/subtraction = 5 [OK]
Hint: Do * and / before + and -, left to right [OK]
Common Mistakes:
  • Adding before multiplying
  • Ignoring division precedence
  • Calculating subtraction before addition
4. Find the error in the following code snippet:
int x = 10;
int y = 5;
int z = x + y * / 2;
std::cout << z;
medium
A. Division by zero error
B. Syntax error due to misplaced operator
C. Logical error in calculation
D. No error, code runs fine

Solution

  1. Step 1: Analyze the expression for syntax correctness

    The expression x + y * / 2 has two operators (*) and (/) together without an operand, which is invalid syntax.
  2. Step 2: Confirm the error type

    This causes a syntax error because the compiler cannot parse the expression correctly.
  3. Final Answer:

    Syntax error due to misplaced operator -> Option B
  4. Quick Check:

    Invalid operator sequence causes syntax error [OK]
Hint: Check operator placement for missing operands [OK]
Common Mistakes:
  • Assuming code runs despite syntax errors
  • Confusing syntax error with runtime error
  • Ignoring operator adjacency rules
5. Consider the expression int result = 10 - 3 * 2 + 4 / 2;. How can you change it using parentheses to make addition happen before multiplication and division?
hard
A. int result = 10 - ((3 * 2) + 4) / 2;
B. int result = ((10 - 3) * 2 + 4) / 2;
C. int result = 10 - ((3 * 2) + (4 / 2));
D. int result = 10 - (3 * (2 + 4)) / 2;

Solution

  1. Step 1: Understand original precedence

    Original expression evaluates multiplication and division before addition and subtraction.
  2. Step 2: Use parentheses to force addition before multiplication/division

    int result = 10 - (3 * (2 + 4)) / 2; groups (2 + 4) first, so addition happens before multiplication and division.
  3. Final Answer:

    int result = 10 - (3 * (2 + 4)) / 2; -> Option D
  4. Quick Check:

    Parentheses change order to add before multiply/divide [OK]
Hint: Use parentheses to group additions before other ops [OK]
Common Mistakes:
  • Misplacing parentheses changing meaning
  • Not grouping addition properly
  • Assuming left-to-right overrides precedence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard