Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Variables and Type System
What is the output of this Kotlin code?
fun test(x: Any) {
    if (x is String) println("String: $x")
    else println("Not a String")
}
test(123)
AString: 123
BNot a String
CCompilation error
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the input and type check

    The function receives 123, which is an Int, not a String.
  2. Step 2: Determine which branch runs

    Since 123 is not a String, the else branch runs, printing "Not a String".
  3. Final Answer:

    Not a String -> Option B
  4. Quick Check:

    Type check false leads to else = A [OK]
Quick Trick: If is fails, else branch runs [OK]
Common Mistakes:
MISTAKES
  • Assuming Int is String
  • Expecting compilation error
  • Confusing output text

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes