Bird
0
0

You want to assign a grade based on score using if as an expression. Which code correctly assigns "A" if score >= 90, "B" if score >= 80, else "C"?

hard📝 Application Q8 of 15
Kotlin - Control Flow as Expressions
You want to assign a grade based on score using if as an expression. Which code correctly assigns "A" if score >= 90, "B" if score >= 80, else "C"?
Aval grade = if (score >= 90) "A" else if score >= 80 "B" else "C"
Bval grade = if (score >= 90) "A" else (score >= 80) "B" else "C"
Cval grade = if (score >= 90) "A" else if (score >= 80) "B" else "C"
Dval grade = if score >= 90 "A" else if (score >= 80) "B" else "C"
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested if expressions

    Kotlin allows nested if expressions to handle multiple conditions.
  2. Step 2: Check syntax correctness

    val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C" correctly nests if expressions with proper parentheses and else branches.
  3. Final Answer:

    val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C" -> Option C
  4. Quick Check:

    Nested if expressions need parentheses and else branches [OK]
Quick Trick: Use nested if expressions with else for multiple conditions [OK]
Common Mistakes:
MISTAKES
  • Missing parentheses in nested if
  • Omitting else branch
  • Incorrect if syntax without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes