0
0
Kotlinprogramming~10 mins

If-else expression assignment 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.

Kotlin
val max = if (a > b) [1] else b
Drag options to blanks, or click blank then click option'
Aa + b
Ba
C0
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Using b in the if part instead of a.
Trying to use statements instead of expressions.
2fill in blank
medium

Complete the code to assign a message based on the score.

Kotlin
val message = if (score >= 50) [1] else "Try again"
Drag options to blanks, or click blank then click option'
A"Incomplete"
B"Failed"
C"Excellent"
D"Passed"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the else message in the if part.
Forgetting to put quotes around the string.
3fill in blank
hard

Fix the error in the if-else expression to assign the correct value.

Kotlin
val result = if (num % 2 == 0) [1] else "Odd"
Drag options to blanks, or click blank then click option'
A"Even"
BEven
Cnum
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using Even without quotes causing a compile error.
Returning a boolean or number instead of a string.
4fill in blank
hard

Fill both blanks to assign a grade based on the score.

Kotlin
val grade = if (score >= [1]) "A" else if (score >= [2]) "B" else "C"
Drag options to blanks, or click blank then click option'
A90
B80
C70
D60
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the thresholds for A and B.
Using thresholds that overlap or are out of order.
5fill in blank
hard

Fill all three blanks to assign a description based on temperature.

Kotlin
val description = if (temp < [1]) "Cold" else if (temp < [2]) "Warm" else [3]
Drag options to blanks, or click blank then click option'
A10
B25
C"Hot"
D"Cool"
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers instead of strings for descriptions.
Mixing up the order of temperature thresholds.