Bird
0
0

Which of the following is the correct way to include the value of variable count inside a Kotlin string?

easy📝 Conceptual Q2 of 15
Kotlin - Variables and Type System
Which of the following is the correct way to include the value of variable count inside a Kotlin string?
A"Total count is \$count"
B"Total count is count"
C"Total count is $count"
D"Total count is ${count.toString}"
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct syntax for variable interpolation

    In Kotlin, you can embed a variable directly using $variableName inside a string.
  2. Step 2: Evaluate options

    "Total count is $count" uses $count correctly. "Total count is \$count" escapes $, printing literal text. "Total count is count" treats count as plain text. "Total count is ${count.toString}" misses parentheses for toString().
  3. Final Answer:

    "Total count is $count" -> Option C
  4. Quick Check:

    Variable interpolation = $variable [OK]
Quick Trick: Use $variable for simple variables inside strings [OK]
Common Mistakes:
MISTAKES
  • Forgetting $ before variable name
  • Using variable name without $ inside string
  • Calling methods without parentheses inside ${}

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes