Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Variables and Type System
What will be the output of this Kotlin code?
fun checkType(x: Any) {
    if (x is Int) {
        println(x + 5)
    } else {
        println("Not an Int")
    }
}

checkType(10)
checkType("Hello")
AError at runtime
B10 Not an Int
C15 Hello5
D15 Not an Int
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the first call checkType(10)

    10 is an Int, so inside the if block, x + 5 equals 15, printed as "15".
  2. Step 2: Analyze the second call checkType("Hello")

    "Hello" is not an Int, so else block runs printing "Not an Int".
  3. Final Answer:

    15 Not an Int -> Option D
  4. Quick Check:

    Int check prints sum, else prints message = A [OK]
Quick Trick: If is true, use variable as that type inside block [OK]
Common Mistakes:
MISTAKES
  • Assuming string "Hello" can be added to 5
  • Expecting runtime error instead of else output
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes