Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Variables and Type System
Identify the error in this Kotlin code snippet:
val count = 5
println("Count is $count+1")
AIt will print 'Count is 5+1' literally, not sum
BRuntime error because of invalid string
CSyntax error due to missing braces
DIt will print 'Count is 6' correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand how $count+1 is interpreted

    Kotlin treats $count as variable and +1 as plain text, so it prints "5+1" literally.
  2. Step 2: Correct way to calculate sum in string

    Use ${count + 1} to evaluate the sum inside the string.
  3. Final Answer:

    It will print 'Count is 5+1' literally, not sum -> Option A
  4. Quick Check:

    Use ${} for expressions, else prints text [OK]
Quick Trick: Use ${} to calculate expressions, else prints text [OK]
Common Mistakes:
MISTAKES
  • Assuming $count+1 calculates sum automatically
  • Missing braces for expressions
  • Confusing string concatenation with templates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes