Bird
Raised Fist0
Cprogramming~20 mins

Operator precedence - 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 this C code snippet?
C
int main() {
    int a = 5, b = 3, c = 2;
    int result = a + b << c;
    printf("%d", result);
    return 0;
}
A14
B16
C32
D13
Attempts:
2 left
💡 Hint
Remember that bitwise shift operators have lower precedence than addition.
❓ Predict Output
intermediate
2: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;
}
A0
B1
C2
D3
Attempts:
2 left
💡 Hint
Check operator precedence between && and & operators.
❓ Predict Output
advanced
2: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;
}
A4
B7
C3
D9
Attempts:
2 left
💡 Hint
Remember multiplication and division have higher precedence than addition and subtraction.
❓ Predict Output
advanced
2: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;
}
A5
B10
C20
D0
Attempts:
2 left
💡 Hint
Ternary operators associate right to left. Evaluate carefully.
❓ Predict Output
expert
3: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;
}
A2
B1
C4
D3
Attempts:
2 left
💡 Hint
Remember the order of evaluation and side effects of post and pre increments.

Practice

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

Solution

  1. Step 1: Recall operator precedence rules

    Multiplication and division have higher precedence than addition and subtraction in C.
  2. Step 2: Identify operators in the expression

    The expression has addition (+) and multiplication (*). Multiplication runs first.
  3. Final Answer:

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

    Multiplication > Addition [OK]
Hint: Multiplication and division run before addition and subtraction [OK]
Common Mistakes:
  • Thinking addition runs before multiplication
  • Ignoring operator precedence
  • Assuming left to right always applies
2. Which of the following expressions is correctly written to add 2 and 3, then multiply the result by 4?
easy
A. (2 + 3) * 4
B. 2 + (3 * 4)
C. 2 + 3 * 4
D. 2 * 3 + 4

Solution

  1. Step 1: Understand the desired calculation

    Add 2 and 3 first, then multiply by 4.
  2. Step 2: Check parentheses usage

    Only (2 + 3) * 4 uses parentheses to add 2 and 3 first, then multiply by 4.
  3. Final Answer:

    (2 + 3) * 4 -> Option A
  4. Quick Check:

    Parentheses change order to add first [OK]
Hint: Use parentheses to force addition before multiplication [OK]
Common Mistakes:
  • Ignoring parentheses and trusting default precedence
  • Misreading the expression order
  • Assuming left to right overrides precedence
3. What is the output of this C code?
int x = 5 + 3 * 2 - 4 / 2;
printf("%d", x);
medium
A. 10
B. 9
C. 11
D. 8

Solution

  1. Step 1: Apply operator precedence

    Multiplication and division first: 3 * 2 = 6, 4 / 2 = 2.
  2. Step 2: Calculate the expression left to right

    Expression becomes 5 + 6 - 2 = 9.
  3. Final Answer:

    9 -> Option B
  4. Quick Check:

    5 + 6 - 2 = 9 [OK]
Hint: Calculate * and / before + and - [OK]
Common Mistakes:
  • Adding before multiplying
  • Dividing after subtraction
  • Calculating left to right ignoring precedence
4. Identify the error in this expression:
int result = 10 / 2 * 3 + 4 - 5;
medium
A. Multiplication and division have the same precedence and are left to right
B. Subtraction should be done before multiplication
C. Addition has higher precedence than multiplication
D. Division should be done after addition

Solution

  1. Step 1: Check operator precedence and associativity

    Multiplication (*) and division (/) have the same precedence and are evaluated left to right.
  2. Step 2: Verify the expression evaluation order

    Expression evaluates as ((10 / 2) * 3) + 4 - 5, which is correct.
  3. Final Answer:

    Multiplication and division have the same precedence and are left to right -> Option A
  4. Quick Check:

    * and / same precedence, left to right [OK]
Hint: Remember * and / are equal precedence, evaluated left to right [OK]
Common Mistakes:
  • Thinking division happens after addition
  • Assuming addition has higher precedence
  • Ignoring left-to-right rule for same precedence
5. Given the expression int y = 8 - 3 + 2 * 4 / 2;, which parentheses placement changes the result to 14?
hard
A. 8 - 3 + (2 * 4) / 2
B. 8 - (3 + 2) * 4 / 2
C. (8 - 3 + 2) * 4 / 2
D. (8 - (3 + 2)) * 4 / 2

Solution

  1. Step 1: Calculate original expression

    * and / first left-to-right: 2 * 4 = 8, 8 / 2 = 4; then 8 - 3 + 4 = 9.
  2. Step 2: Evaluate (8 - 3 + 2) * 4 / 2

    (8 - 3 + 2) = 7, 7 * 4 = 28, 28 / 2 = 14.
  3. Step 3: Confirm others

    8 - (3 + 2) * 4 / 2 = 8 - 20 / 2 = 8 - 10 = -2; 8 - 3 + (2 * 4) / 2 = 9; (8 - (3 + 2)) * 4 / 2 = 3 * 4 / 2 = 6.
  4. Final Answer:

    (8 - 3 + 2) * 4 / 2 -> Option C
  5. Quick Check:

    Parentheses force addition before multiplication [OK]
Hint: Try parentheses around addition to change multiplication order [OK]
Common Mistakes:
  • Assuming original order always applies
  • Not recalculating expression after adding parentheses
  • Misplacing parentheses without recalculating
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