Bird
0
0

You want to assign a value based on multiple conditions using expressions. Which Kotlin code correctly uses expressions to assign a grade based on score?

hard📝 Application Q8 of 15
Kotlin - Control Flow as Expressions
You want to assign a value based on multiple conditions using expressions. Which Kotlin code correctly uses expressions to assign a grade based on score?
val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C"
ACorrect use of nested if expressions
BIncorrect, should use statements with braces
CIncorrect, else if is not allowed in Kotlin
DIncorrect, must use when expression instead
Step-by-Step Solution
Solution:
  1. Step 1: Analyze nested if expressions

    Kotlin allows nested if expressions to return values inline.
  2. Step 2: Confirm assignment correctness

    Code assigns grade based on conditions correctly using expressions.
  3. Final Answer:

    Correct use of nested if expressions -> Option A
  4. Quick Check:

    Nested if expressions valid = A [OK]
Quick Trick: Nested if expressions return values inline [OK]
Common Mistakes:
MISTAKES
  • Thinking else if is invalid
  • Believing braces are mandatory
  • Assuming when is required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes