Challenge - 5 Problems
Operator Precedence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this MATLAB code?
Consider the following MATLAB code snippet. What value does variable
result hold after execution?MATLAB
result = 3 + 4 * 2;
Attempts:
2 left
💡 Hint
Remember that multiplication (*) has higher precedence than addition (+).
✗ Incorrect
In MATLAB, multiplication (*) is done before addition (+). So, 4 * 2 = 8, then 3 + 8 = 11.
❓ Predict Output
intermediate2:00remaining
What is the output of this MATLAB expression with parentheses?
What value does
result hold after running this code?MATLAB
result = (3 + 4) * 2;
Attempts:
2 left
💡 Hint
Parentheses change the order of operations.
✗ Incorrect
Parentheses force MATLAB to add 3 + 4 first, which is 7, then multiply by 2, resulting in 14.
❓ Predict Output
advanced2:00remaining
What is the output of this MATLAB logical expression?
What is the value of
result after running this code?MATLAB
result = 5 > 3 & 2 < 1 | 4 == 4;
Attempts:
2 left
💡 Hint
Logical AND (&) has higher precedence than OR (|).
✗ Incorrect
First, 5 > 3 is true (1), 2 < 1 is false (0), so 1 & 0 = 0. Then 4 == 4 is true (1). Finally, 0 | 1 = 1.
❓ Predict Output
advanced2:00remaining
What is the output of this MATLAB expression with power and negation?
What value does
result hold after running this code?MATLAB
result = -2^2;
Attempts:
2 left
💡 Hint
Exponentiation (^) has higher precedence than unary minus (-).
✗ Incorrect
MATLAB calculates 2^2 first, which is 4, then applies the unary minus, resulting in -4.
❓ Predict Output
expert2:00remaining
What is the output of this complex MATLAB expression?
What value does
result hold after running this code?MATLAB
result = 10 / 2 * 3 ^ 2 - 4 + 1;
Attempts:
2 left
💡 Hint
Remember the order: power (^), then multiplication/division (*,/), then addition/subtraction (+,-).
✗ Incorrect
Operator precedence in MATLAB: ^ > * / > + -
Same precedence operators are evaluated left to right.
Step-by-step:
1. 3 ^ 2 = 9
2. 10 / 2 * 9 = (10 / 2) * 9 = 5 * 9 = 45
3. 45 - 4 + 1 = (45 - 4) + 1 = 41 + 1 = 42