Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Variables and Type System
What will be the output of the following Kotlin code?
val x = 5
val y = 10
println("Sum: ${x + y}")
ASum: ${x + y}
BSum: x + y
CSum: 15
DSum: 510
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the expression inside string template

    The expression ${x + y} calculates the sum of 5 and 10, which is 15.
  2. Step 2: Understand string output

    The println prints "Sum: " followed by the result of the expression, so output is "Sum: 15".
  3. Final Answer:

    Sum: 15 -> Option C
  4. Quick Check:

    Expression inside ${} is evaluated [OK]
Quick Trick: Expressions inside ${} are calculated before printing [OK]
Common Mistakes:
MISTAKES
  • Printing expression as literal text
  • Concatenating variables without evaluation
  • Confusing string concatenation with template evaluation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes