Operator precedence is the set of rules that determines the order in which different operators are evaluated in an expression.
Click to reveal answer
beginner
Which operator has higher precedence: multiplication (*) or addition (+)?
Multiplication (*) has higher precedence than addition (+), so it is evaluated first.
Click to reveal answer
beginner
How does parentheses affect operator precedence?
Parentheses change the order of evaluation by forcing the expression inside them to be evaluated first, regardless of normal precedence rules.
Click to reveal answer
intermediate
What is the precedence order between logical AND (&&) and logical OR (||) in R?
Logical AND (&&) has higher precedence than logical OR (||), so && is evaluated before ||.
Click to reveal answer
advanced
In the expression 3 + 4 * 2 / (1 - 5)^2^3, which operation is evaluated first?
The expression inside parentheses (1 - 5) is evaluated first, then exponentiation (^), then multiplication (*) and division (/), and finally addition (+).
Click to reveal answer
In R, which operator is evaluated first in the expression 5 + 3 * 2?
ADepends on variable types
BAddition (+)
CMultiplication (*)
DBoth at the same time
✗ Incorrect
Multiplication (*) has higher precedence than addition (+), so 3 * 2 is evaluated first.
What does parentheses do in an expression like (2 + 3) * 4?
AThey change the operator precedence to default
BThey make the expression inside evaluated first
CThey have no effect
DThey delay evaluation until later
✗ Incorrect
Parentheses force the expression inside to be evaluated first, so 2 + 3 is done before multiplication.
Which has higher precedence in R: logical AND (&&) or logical OR (||)?
ALogical OR (||)
BDepends on the expression
CThey have equal precedence
DLogical AND (&&)
✗ Incorrect
Logical AND (&&) has higher precedence than logical OR (||).
In the expression 2^3^2, which exponentiation is done first?
A3^2 first
B2^3 first
CBoth at the same time
DExponentiation is left to right
✗ Incorrect
Exponentiation in R is right-associative, so 3^2 is evaluated first.
What is the result of 4 + 6 / 3 in R?
A6
B3.33
C7
D6 + 4 / 3
✗ Incorrect
Division (/) has higher precedence, so 6 / 3 = 2, then 4 + 2 = 6.
Explain how operator precedence affects the evaluation of the expression 7 + 3 * 2 in R.
Think about which operation R does first without parentheses.
You got /3 concepts.
Describe how parentheses can change the result of an expression with mixed operators in R.
Consider how you can force R to do addition before multiplication.
You got /3 concepts.
Practice
(1/5)
1. In R, which operator has the highest precedence among these: +, *, ^, -?
easy
A. Subtraction (-)
B. Multiplication (*)
C. Addition (+)
D. Exponentiation (^)
Solution
Step 1: Recall operator precedence rules in R
Exponentiation (^) has higher precedence than multiplication, addition, and subtraction.
Step 2: Compare given operators
Among +, *, ^, and -, ^ is evaluated first.
Final Answer:
Exponentiation (^) -> Option D
Quick Check:
^ > * > +, - [OK]
Hint: Remember: powers before multiply, multiply before add [OK]
Common Mistakes:
Thinking multiplication is higher than exponentiation
Confusing addition and subtraction precedence
Ignoring operator precedence order
2. Which of the following R expressions is syntactically correct and respects operator precedence?
easy
A. 3 + 4 ** 2
B. 3 + 4 * 2
C. 3 + (4 * ) 2
D. 3 + * 4 2
Solution
Step 1: Check each expression for syntax correctness
3 + 4 * 2 is a valid expression: addition and multiplication with correct syntax. Options A, B, and C have syntax errors.
Step 2: Confirm operator precedence in 3 + 4 * 2
Multiplication happens before addition, so 4 * 2 is evaluated first, then added to 3.
Final Answer:
3 + 4 * 2 -> Option B
Quick Check:
Valid syntax and precedence respected [OK]
Hint: Check for valid operator placement and parentheses [OK]
Common Mistakes:
Using invalid operators like ** in R
Misplacing operators causing syntax errors
Incorrect parentheses usage
3. What is the output of this R code?
result <- 2 + 3 * 4 ^ 2 print(result)
medium
A. 50
B. 74
C. 200
D. 100
Solution
Step 1: Evaluate exponentiation first
4 ^ 2 equals 16.
Step 2: Multiply then add
Multiply 3 by 16 to get 48, then add 2: 2 + 48 = 50.
Step 3: Confirm the calculation
2 + 3 * 4 ^ 2 = 2 + 3 * 16 = 2 + 48 = 50.
Final Answer:
50 -> Option A
Quick Check:
^ then * then + = 50 [OK]
Hint: Calculate powers first, then multiply, then add [OK]
Common Mistakes:
Adding before multiplying
Ignoring exponentiation precedence
Misreading the expression order
4. Identify the error in this R expression:
value <- 5 + 2 * (3 - 1
medium
A. Missing closing parenthesis
B. Incorrect operator usage
C. Multiplication should be addition
D. No error, expression is correct
Solution
Step 1: Check parentheses balance
The expression has an opening parenthesis after 2 * but no closing parenthesis.
Step 2: Confirm syntax error due to missing parenthesis
R requires matching parentheses; missing one causes syntax error.
Final Answer:
Missing closing parenthesis -> Option A
Quick Check:
Parentheses must be balanced [OK]
Hint: Count opening and closing parentheses carefully [OK]
Common Mistakes:
Ignoring missing parentheses
Assuming operators are wrong instead
Not checking syntax errors first
5. Given the vector v <- c(1, 2, 3), what is the result of this expression in R?
result <- 10 / 2 * v + 1
Choose the correct output.
hard
A. [1] 6 7 8
B. [1] 11 12 13
C. [1] 6 11 16
D. [1] 5 10 15
Solution
Step 1: Evaluate division and multiplication first
10 / 2 equals 5, then multiply by vector v: 5 * c(1,2,3) = c(5,10,15).
Step 2: Add 1 to each element
Add 1 to each element: c(5,10,15) + 1 = c(6,11,16).
Final Answer:
[1] 6 11 16 -> Option C
Quick Check:
Division and multiplication before addition [OK]
Hint: Do division and multiplication before addition on vectors [OK]
Common Mistakes:
Adding 1 before multiplication
Ignoring vectorized operations
Misordering division and multiplication
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.