Bird
0
0

Given the Kotlin expression val result = 4 + 3 * 2 - 8 / 4, how can you change it to make addition happen first without changing the numbers?

hard📝 Application Q8 of 15
Kotlin - Operators and Expressions
Given the Kotlin expression val result = 4 + 3 * 2 - 8 / 4, how can you change it to make addition happen first without changing the numbers?
AUse parentheses: <code>val result = (4 + 3 * 2 - 8) / 4</code>
BUse parentheses: <code>val result = 4 + (3 * 2 - 8) / 4</code>
CUse parentheses: <code>val result = 4 + (3 * (2 - 8)) / 4</code>
DUse parentheses: <code>val result = (4 + 3) * 2 - 8 / 4</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand current precedence

    Multiplication and division happen before addition and subtraction.
  2. Step 2: To make addition first, group 4 + 3 first

    Using parentheses around (4 + 3) forces addition before multiplication.
  3. Final Answer:

    Use parentheses: val result = (4 + 3) * 2 - 8 / 4 -> Option D
  4. Quick Check:

    Parentheses change precedence to add first [OK]
Quick Trick: Parentheses override default precedence [OK]
Common Mistakes:
MISTAKES
  • Misplacing parentheses
  • Changing numbers instead of order
  • Ignoring operator precedence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes