Bird
0
0

What is the main advantage of using if-else as an expression in Kotlin?

easy📝 Conceptual Q1 of 15
Kotlin - Control Flow as Expressions
What is the main advantage of using if-else as an expression in Kotlin?
AIt returns a value that can be assigned to a variable
BIt can only be used inside functions
CIt does not allow else branch
DIt cannot be nested inside other expressions
Step-by-Step Solution
Solution:
  1. Step 1: Understand if-else expression behavior

    In Kotlin, if-else can return a value, unlike in some languages where it is only a statement.
  2. Step 2: Identify the advantage

    This allows assigning the result of if-else directly to a variable, making code concise.
  3. Final Answer:

    It returns a value that can be assigned to a variable -> Option A
  4. Quick Check:

    If-else expression returns value = A [OK]
Quick Trick: If-else in Kotlin returns a value usable in assignments [OK]
Common Mistakes:
MISTAKES
  • Thinking if-else cannot return values
  • Assuming else branch is optional in expression
  • Believing if-else expressions can't be nested

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes