Bird
0
0

How does Kotlin evaluate this expression?

hard📝 Application Q9 of 15
Kotlin - Operators and Expressions
How does Kotlin evaluate this expression?
val x = 2
val y = 3
val z = x + y * x - y / x

What is the value of z?
A4
B7
C5
D6
Step-by-Step Solution
Solution:
  1. Step 1: Calculate multiplication and division first

    y * x = 3 * 2 = 6; y / x = 3 / 2 = 1 (integer division).
  2. Step 2: Evaluate addition and subtraction left to right

    x + 6 - 1 = 2 + 6 - 1 = 7.
  3. Step 3: Re-check integer division

    In Kotlin, integer division truncates, so 3 / 2 = 1.
  4. Final Answer:

    7 -> Option B
  5. Quick Check:

    Multiplication/division before addition/subtraction [OK]
Quick Trick: Integer division truncates decimals [OK]
Common Mistakes:
MISTAKES
  • Treating division as float
  • Adding before multiplying
  • Ignoring integer division behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes