Bird
0
0

Which of the following is the correct syntax for assigning a value using if-else expression in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Control Flow as Expressions
Which of the following is the correct syntax for assigning a value using if-else expression in Kotlin?
Aval x = if (a > b) 10 else 20
Bval x if (a > b) = 10 else = 20
Cval x = if a > b then 10 else 20
Dval x = if (a > b) { 10 } else { 20 } else
Step-by-Step Solution
Solution:
  1. Step 1: Review Kotlin if-else expression syntax

    The correct syntax uses if(condition) value1 else value2 without extra keywords or misplaced braces.
  2. Step 2: Compare options with correct syntax

    val x = if (a > b) 10 else 20 matches the correct syntax: val x = if (a > b) 10 else 20.
  3. Final Answer:

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

    Correct if-else assignment syntax is val x = if (cond) val1 else val2 [OK]
Quick Trick: Remember: if-else expression is inside assignment with = [OK]
Common Mistakes:
MISTAKES
  • Using 'then' keyword which Kotlin does not have
  • Misplacing equals signs around else
  • Adding extra else after braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes