Bird
Raised Fist0
Kotlinprogramming~20 mins

Operator precedence in Kotlin - 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
What is the output of this Kotlin code involving mixed operators?

Consider the following Kotlin code snippet:

val result = 3 + 4 * 2 / (1 - 5) % 2
println(result)

What will be printed?

Kotlin
val result = 3 + 4 * 2 / (1 - 5) % 2
println(result)
A2
B1
C0
D3
Attempts:
2 left
💡 Hint

Remember the order: parentheses, multiplication/division/modulus, then addition/subtraction.

❓ Predict Output
intermediate
2:00remaining
What is the value of x after this Kotlin expression?

Given this Kotlin code:

var x = 10
x += 3 * 2 - 4 / 2

What is the value of x after running this?

Kotlin
var x = 10
x += 3 * 2 - 4 / 2
println(x)
A15
B16
C14
D17
Attempts:
2 left
💡 Hint

Calculate multiplication and division before addition and subtraction.

❓ Predict Output
advanced
2:00remaining
What does this Kotlin code print with logical and arithmetic operators?

Analyze this Kotlin code:

val a = true
val b = false
val c = 5
val d = 10
val result = a && b || c < d && !b
println(result)

What is the output?

Kotlin
val a = true
val b = false
val c = 5
val d = 10
val result = a && b || c < d && !b
println(result)
Afalse
Btrue
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint

Remember that && has higher precedence than ||, and ! has highest precedence.

❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with mixed increment and arithmetic?

Consider this Kotlin snippet:

var x = 5
val y = x++ * 2 + --x
println(y)

What will be printed?

Kotlin
var x = 5
val y = x++ * 2 + --x
println(y)
A15
B23
C29
D25
Attempts:
2 left
💡 Hint

Remember post-increment returns the value before increment, pre-decrement decrements before use.

❓ Predict Output
expert
2:00remaining
What is the output of this Kotlin code with chained assignments and operator precedence?

Analyze this Kotlin code:

var a = 2
var b = 3
var c = 4
val result = a + b * c / a - b % c
println(result)

What will be printed?

Kotlin
var a = 2
var b = 3
var c = 4
val result = a + b * c / a - b % c
println(result)
A5
B7
C6
D4
Attempts:
2 left
💡 Hint

Follow operator precedence: multiplication/division/modulus before addition/subtraction.

Practice

(1/5)
1. In Kotlin, which operator has the highest precedence in the expression 3 + 4 * 2?
easy
A. Multiplication (*)
B. Addition (+)
C. Subtraction (-)
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 runs first.
  3. Final Answer:

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

    Multiplication before addition = Multiplication (*) [OK]
Hint: Multiply and divide before adding or subtracting [OK]
Common Mistakes:
  • Thinking addition runs before multiplication
  • Ignoring operator precedence
  • Assuming left to right always applies
2. Which of the following Kotlin expressions correctly uses parentheses to change the order of operations in 3 + 4 * 2 so that addition happens first?
easy
A. 3 + (4 * 2)
B. (3 + 4) * 2
C. 3 + 4 * 2
D. 3 * (4 + 2)

Solution

  1. Step 1: Understand default precedence

    Without parentheses, multiplication runs before addition.
  2. Step 2: Check which option forces addition first

    (3 + 4) * 2 uses parentheses around (3 + 4), so addition happens before multiplication.
  3. Final Answer:

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

    Parentheses change order = (3 + 4) * 2 [OK]
Hint: Use parentheses to force addition before multiplication [OK]
Common Mistakes:
  • Ignoring parentheses effect
  • Choosing expression without parentheses
  • Assuming left to right overrides precedence
3. What is the output of this Kotlin code?
val result = 10 - 4 + 2 * 3
println(result)
medium
A. 12
B. 18
C. 14
D. 4

Solution

  1. Step 1: Apply operator precedence

    Multiplication (*) runs first: 2 * 3 = 6.
  2. Step 2: Evaluate remaining expression left to right

    10 - 4 + 6 = 6 + 6 = 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    Multiplication before addition/subtraction = 12 [OK]
Hint: Multiply before adding or subtracting [OK]
Common Mistakes:
  • Adding before multiplying
  • Calculating left to right ignoring precedence
  • Miscomputing subtraction and addition order
4. Find the error in this Kotlin expression:
val x = 5 + 3 * 2 && true

What is the problem?
medium
A. Logical AND (&&) has higher precedence than multiplication
B. Missing semicolon at the end
C. Multiplication operator is invalid here
D. Cannot mix arithmetic and boolean operators without parentheses

Solution

  1. Step 1: Identify mixed types in expression

    The expression mixes arithmetic (5 + 3 * 2) and boolean (&& true) without separation.
  2. Step 2: Understand Kotlin type rules

    You cannot combine Int and Boolean directly without explicit logic or parentheses.
  3. Final Answer:

    Cannot mix arithmetic and boolean operators without parentheses -> Option D
  4. Quick Check:

    Mixing Int and Boolean needs care [OK]
Hint: Don't mix numbers and booleans without clear separation [OK]
Common Mistakes:
  • Thinking semicolon is missing
  • Assuming && works with numbers
  • Ignoring type mismatch errors
5. Given the Kotlin expression:
val result = true || false && false || true && false

What is the value of result considering operator precedence?
hard
A. false
B. Syntax error
C. true
D. null

Solution

  1. Step 1: Recall logical operator precedence

    Logical AND (&&) has higher precedence than OR (||), so evaluate && first.
  2. Step 2: Evaluate all AND operations

    false && false = false, true && false = false.
  3. Step 3: Replace and evaluate OR operations left to right

    true || false || false = true || false = true.
  4. Final Answer:

    true -> Option C
  5. Quick Check:

    AND before OR, final result true [OK]
Hint: Evaluate && before || in logical expressions [OK]
Common Mistakes:
  • Evaluating OR before AND
  • Confusing operator precedence
  • Assuming expression returns null or error
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