Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Control Flow as Expressions
What is the output of this Kotlin code?
val score = 75
val grade = if (score >= 90) "A" else if (score >= 80) "B" else if (score >= 70) "C" else "F"
println(grade)
AA
BC
CB
DF
Step-by-Step Solution
Solution:
  1. Step 1: Check conditions in order

    score is 75, so first two conditions fail, third condition score >= 70 is true.
  2. Step 2: Assign grade based on first true condition

    Grade "C" is assigned and printed.
  3. Final Answer:

    C -> Option B
  4. Quick Check:

    First true condition returns "C" = B [OK]
Quick Trick: Check conditions top to bottom; first true branch wins [OK]
Common Mistakes:
MISTAKES
  • Ignoring order of if-else if conditions
  • Assigning wrong grade for score 75
  • Assuming all conditions run

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes