Bird
0
0

What will this Kotlin code print?

medium📝 Predict Output Q5 of 15
Kotlin - Control Flow as Expressions
What will this Kotlin code print?
val number = 7
val parity = if (number % 2 == 0) "Even" else "Odd"
println(parity)
AEven
BError
C7
DOdd
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition for even number

    7 % 2 == 0 is false because 7 divided by 2 leaves remainder 1.
  2. Step 2: Determine the assigned string

    Since condition is false, else branch "Odd" is assigned to parity.
  3. Final Answer:

    Odd -> Option D
  4. Quick Check:

    If expression returns else value when condition false [OK]
Quick Trick: Use modulo to check even/odd with if expression [OK]
Common Mistakes:
MISTAKES
  • Assuming 7 is even
  • Printing number instead of parity
  • Syntax errors in if expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes