Bird
0
0

Find the error in this Kotlin code snippet:

medium📝 Debug Q7 of 15
Kotlin - Variables and Type System
Find the error in this Kotlin code snippet:
val price = 100
println("Price is $${price}")
AExtra $ before ${price} causes syntax error
BMissing curly braces around price
CCorrect syntax, prints price with $ sign
DShould use + operator to concatenate
Step-by-Step Solution
Solution:
  1. Step 1: Analyze use of $ signs

    In Kotlin, to include a literal $, use \$. The $${price} is invalid syntax because after the first $, the next $ does not start a valid identifier or {expression}.
  2. Step 2: Correct way to print $ with variable

    Use "Price is \$${price}" to output "Price is $100".
  3. Final Answer:

    Extra $ before ${price} causes syntax error -> Option A
  4. Quick Check:

    Use \$ to print literal $ in strings [OK]
Quick Trick: Escape $ with \$ to print literal dollar sign [OK]
Common Mistakes:
MISTAKES
  • Using double $$ incorrectly
  • Not escaping $ when printing literal
  • Confusing variable interpolation with literal $

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes