0
0
Kotlinprogramming~10 mins

If as an expression returning value in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the maximum of two numbers to max using if as an expression.

Kotlin
val max = if (a > b) [1] else b
Drag options to blanks, or click blank then click option'
A0
Bb
Ca + b
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using b instead of a in the true branch.
Trying to use if as a statement without returning a value.
2fill in blank
medium

Complete the code to assign a message based on the number using if as an expression.

Kotlin
val message = if (num % 2 == 0) [1] else "Odd number"
Drag options to blanks, or click blank then click option'
A"Positive"
B"Number"
C"Even number"
D"Zero"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Returning the wrong message for even numbers.
3fill in blank
hard

Fix the error in the code by completing the if expression to return the correct value.

Kotlin
val result = if (score >= 50) [1] else "Fail"
Drag options to blanks, or click blank then click option'
APass
B"Pass"
Ctrue
D50
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Pass without quotes causing a compile error.
Returning a boolean or number instead of a string.
4fill in blank
hard

Fill both blanks to create a variable that holds a description based on age using if as an expression.

Kotlin
val description = if (age [1] 18) [2] else "Adult"
Drag options to blanks, or click blank then click option'
A<
B"Minor"
C>=
D"Child"
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= instead of < for the condition.
Returning "Child" instead of "Minor".
5fill in blank
hard

Fill all three blanks to create a variable that returns a grade description based on score using if as an expression.

Kotlin
val grade = if (score [1] 90) [2] else if (score [3] 60) "Fail" else "Pass"
Drag options to blanks, or click blank then click option'
A>=
B"Excellent"
C<
D"Good"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up comparison operators.
Returning wrong strings for the conditions.