Bird
0
0

Why does when without argument in Kotlin not require an argument, and how does it decide which branch to execute?

hard📝 Conceptual Q10 of 15
Kotlin - Control Flow as Expressions
Why does when without argument in Kotlin not require an argument, and how does it decide which branch to execute?
ABecause it requires a default argument passed implicitly
BBecause it compares a hidden variable automatically
CBecause it loops through all branches and executes all true ones
DBecause it evaluates boolean expressions and executes the first true branch
Step-by-Step Solution
Solution:
  1. Step 1: Understand when without argument behavior

    It works like an if-else chain evaluating boolean conditions.
  2. Step 2: Explain branch execution

    It executes only the first branch whose condition is true and skips others.
  3. Final Answer:

    Because it evaluates boolean expressions and executes the first true branch -> Option D
  4. Quick Check:

    when without argument = first true condition runs [OK]
Quick Trick: when without argument acts like if-else chain [OK]
Common Mistakes:
MISTAKES
  • Thinking it compares a hidden variable
  • Assuming all true branches run
  • Believing it needs an implicit argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes