Bird
0
0

What will be printed by this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Control Flow as Expressions
What will be printed by this Kotlin code?
fun printInfo(value: Any) {
  when (value) {
    is Double -> println(value * 2)
    is String -> println(value.reversed())
    else -> println("Unsupported")
  }
}
printInfo("Kotlin")
AUnsupported
Bniltok
CKotlin
DError
Step-by-Step Solution
Solution:
  1. Step 1: Identify the input

    The function is called with a String value "Kotlin".
  2. Step 2: Check the when branches

    Since value is a String, the is String branch executes.
  3. Step 3: Evaluate output

    value.reversed() returns "niltok" (case sensitive).
  4. Final Answer:

    niltok -> Option B
  5. Quick Check:

    Smart cast allows calling reversed() on String [OK]
Quick Trick: Smart cast enables calling type-specific methods [OK]
Common Mistakes:
MISTAKES
  • Confusing reversed string output
  • Expecting uppercase or unchanged string
  • Ignoring smart cast effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes