Bird
0
0

Why does Kotlin require curly braces around expressions inside string templates but not around simple variables?

hard📝 Conceptual Q10 of 15
Kotlin - Variables and Type System
Why does Kotlin require curly braces around expressions inside string templates but not around simple variables?
ATo improve runtime performance
BTo clearly separate complex expressions from plain text
CBecause variables cannot be used without braces
DTo allow multiline strings only
Step-by-Step Solution
Solution:
  1. Step 1: Understand string template syntax rules

    Simple variables can be referenced with $variable, but expressions need ${expression} to clearly mark their boundaries.
  2. Step 2: Analyze options

    To clearly separate complex expressions from plain text correctly explains that braces separate complex expressions from plain text. Other options are incorrect or unrelated.
  3. Final Answer:

    To clearly separate complex expressions from plain text -> Option B
  4. Quick Check:

    Braces clarify expression boundaries in templates [OK]
Quick Trick: Use ${} to avoid confusion with complex expressions [OK]
Common Mistakes:
MISTAKES
  • Using braces for simple variables unnecessarily
  • Thinking braces improve performance
  • Confusing braces with multiline string syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes