Bird
0
0

Which of the following is a valid way to assign a value using if-else expression in Kotlin?

easy📝 Conceptual Q2 of 15
Kotlin - Control Flow as Expressions
Which of the following is a valid way to assign a value using if-else expression in Kotlin?
Aval x = if (a > b) a else b
Bval x if (a > b) = a else b
Cval x = if a > b then a else b
Dval x = if (a > b) { a } else { b } else
Step-by-Step Solution
Solution:
  1. Step 1: Review Kotlin if-else assignment syntax

    The correct syntax uses if(condition) value else value without extra keywords.
  2. Step 2: Check each option

    val x = if (a > b) a else b matches correct syntax; others have syntax errors or invalid keywords.
  3. Final Answer:

    val x = if (a > b) a else b -> Option A
  4. Quick Check:

    Correct if-else assignment syntax = D [OK]
Quick Trick: Use parentheses and else keyword correctly in if-else assignment [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses around condition
  • Using 'then' keyword which Kotlin does not have
  • Adding extra else keyword at the end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes