Bird
0
0

Which of the following is the correct syntax to include an expression inside a Kotlin string template?

easy📝 Syntax Q12 of 15
Kotlin - Variables and Type System
Which of the following is the correct syntax to include an expression inside a Kotlin string template?
Aprintln("Sum is ${a + b}")
Bprintln("Sum is $a + $b")
Cprintln("Sum is $[a + b]")
Dprintln("Sum is $(a + b)")
Step-by-Step Solution
Solution:
  1. Step 1: Recall expression syntax in string templates

    Kotlin uses ${expression} to evaluate expressions inside strings.
  2. Step 2: Identify correct expression usage

    ${a + b} correctly calculates the sum and inserts it.
  3. Final Answer:

    println("Sum is ${a + b}") -> Option A
  4. Quick Check:

    Use ${expression} for calculations inside strings [OK]
Quick Trick: Use ${} to insert expressions inside strings [OK]
Common Mistakes:
MISTAKES
  • Using $a + $b prints variables separately, not sum
  • Using square brackets [] instead of curly braces
  • Using parentheses () without $

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes