Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Basics and JVM Runtime
What is the output of this Kotlin code?
fun main() {
  val x = 10
  val y = 20
  val max = if (x > y) x else y
  println(max)
}
A10
Btrue
C20
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the if expression

    Since x (10) is not greater than y (20), else branch returns y (20).
  2. Step 2: Print the max value

    max is assigned 20, so println outputs 20.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    If expression result = B [OK]
Quick Trick: if in Kotlin returns a value, not just a statement [OK]
Common Mistakes:
MISTAKES
  • Assuming if returns boolean only
  • Confusing variable values
  • Expecting compilation error due to if expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes