0
0
Kotlinprogramming~20 mins

If-else expression assignment in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using if-else expression assignment?

Consider the following Kotlin code snippet:

val x = 10
val result = if (x > 5) "Greater" else "Smaller or equal"
println(result)

What will be printed?

Kotlin
val x = 10
val result = if (x > 5) "Greater" else "Smaller or equal"
println(result)
AGreater
BSmaller or equal
CCompilation error
D10
Attempts:
2 left
💡 Hint

Remember that if-else in Kotlin can be used as an expression that returns a value.

Predict Output
intermediate
2:00remaining
What is the value of 'message' after this Kotlin code runs?

Look at this Kotlin code:

val number = 3
val message = if (number % 2 == 0) "Even" else "Odd"
println(message)

What will be printed?

Kotlin
val number = 3
val message = if (number % 2 == 0) "Even" else "Odd"
println(message)
AEven
BRuntime exception
C3
DOdd
Attempts:
2 left
💡 Hint

Check the condition for even numbers using modulo operator.

Predict Output
advanced
2:00remaining
What does this Kotlin code print when using nested if-else expressions?

Analyze this Kotlin snippet:

val score = 75
val grade = if (score >= 90) "A"
            else if (score >= 80) "B"
            else if (score >= 70) "C"
            else "F"
println(grade)

What is the output?

Kotlin
val score = 75
val grade = if (score >= 90) "A"
            else if (score >= 80) "B"
            else if (score >= 70) "C"
            else "F"
println(grade)
AA
BB
CC
DF
Attempts:
2 left
💡 Hint

Check each condition in order from top to bottom.

Predict Output
advanced
2:00remaining
What error does this Kotlin code produce?

Consider this Kotlin code snippet:

val x = 5
val y = if x > 3 "Yes" else "No"
println(y)

What error will occur?

Kotlin
val x = 5
val y = if x > 3 "Yes" else "No"
println(y)
ASyntaxError: Missing parentheses in if condition
BTypeError: Cannot assign String to Int
CNo error, prints "Yes"
DRuntimeException: Null pointer
Attempts:
2 left
💡 Hint

Check the syntax of the if expression in Kotlin.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting list after this Kotlin code runs?

Examine this Kotlin code:

val numbers = listOf(1, 2, 3, 4, 5)
val results = numbers.map { if (it % 2 == 0) it * 2 else it * 3 }
println(results.size)

How many items does results contain?

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val results = numbers.map { if (it % 2 == 0) it * 2 else it * 3 }
println(results.size)
A2
B5
C3
D0
Attempts:
2 left
💡 Hint

Remember that map transforms each item but keeps the list size.