Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the following Kotlin code do?
val result = if (score > 50) "Pass" else "Fail"
easy
A. Assigns "Pass" to result if score is greater than 50, otherwise "Fail"
B. Assigns true if score is greater than 50, otherwise false
C. Throws an error because else is missing
D. Assigns the value of score to result

Solution

  1. Step 1: Understand the if-else expression

    The code uses if-else as an expression to assign a value to result based on the condition score > 50.
  2. Step 2: Evaluate the condition and assignment

    If score is greater than 50, result gets "Pass"; otherwise, it gets "Fail".
  3. Final Answer:

    Assigns "Pass" to result if score is greater than 50, otherwise "Fail" -> Option A
  4. Quick Check:

    If-else expression assigns value based on condition [OK]
Hint: If-else expression returns a value, not just controls flow [OK]
Common Mistakes:
  • Forgetting the else part causes a syntax error
  • Thinking if-else is only for flow, not value assignment
  • Confusing condition with assignment
2. Which of the following is the correct syntax for assigning a value using if-else expression in Kotlin?
easy
A. val x = if (a > b) 10 else 20
B. val x if (a > b) = 10 else = 20
C. val x = if a > b then 10 else 20
D. val x = if (a > b) { 10 } else { 20 } else

Solution

  1. Step 1: Review Kotlin if-else expression syntax

    The correct syntax uses if(condition) value1 else value2 without extra keywords or misplaced braces.
  2. Step 2: Compare options with correct syntax

    val x = if (a > b) 10 else 20 matches the correct syntax: val x = if (a > b) 10 else 20.
  3. Final Answer:

    val x = if (a > b) 10 else 20 -> Option A
  4. Quick Check:

    Correct if-else assignment syntax is val x = if (cond) val1 else val2 [OK]
Hint: Remember: if-else expression is inside assignment with = [OK]
Common Mistakes:
  • Using 'then' keyword which Kotlin does not have
  • Misplacing equals signs around else
  • Adding extra else after braces
3. What is the output of this Kotlin code?
val age = 18
val status = if (age >= 18) "Adult" else "Minor"
println(status)
medium
A. Minor
B. Adult
C. 18
D. Compilation error

Solution

  1. Step 1: Check the condition age >= 18

    Since age is 18, the condition age >= 18 is true.
  2. Step 2: Determine the assigned value to status

    Because the condition is true, status is assigned "Adult".
  3. Final Answer:

    Adult -> Option B
  4. Quick Check:

    Condition true assigns "Adult" [OK]
Hint: If condition true, if-else expression returns first value [OK]
Common Mistakes:
  • Assuming >= 18 is false for 18
  • Printing age instead of status
  • Confusing syntax causing compile error
4. Identify the error in this Kotlin code:
val x = if (num > 0) "Positive"
medium
A. Incorrect variable declaration
B. Wrong comparison operator
C. Missing else branch in if-else expression
D. No error, code is correct

Solution

  1. Step 1: Check if-else expression completeness

    An if-else expression used for assignment must have both if and else branches.
  2. Step 2: Identify missing else branch

    The code only has if (num > 0) "Positive" but no else part, causing a syntax error.
  3. Final Answer:

    Missing else branch in if-else expression -> Option C
  4. Quick Check:

    If-else expression must include else [OK]
Hint: Always include else when assigning with if-else expression [OK]
Common Mistakes:
  • Omitting else branch
  • Thinking if alone can assign value
  • Ignoring compiler error messages
5. You want to assign a grade based on score using if-else expression assignment in Kotlin. Which code correctly assigns "A" if score >= 90, "B" if score >= 80, else "C"?
hard
A. val grade = if (score >= 90) "A" else if score >= 80 "B" else "C"
B. val grade = if (score >= 90) "A" else (score >= 80) "B" else "C"
C. val grade = if (score >= 90) { "A" } else if (score >= 80) { "B" } else { "C" } else
D. val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C"

Solution

  1. Step 1: Understand nested if-else expression syntax

    Kotlin allows nested if-else expressions to assign values based on multiple conditions.
  2. Step 2: Check each option for correct syntax and logic

    val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C" correctly nests if-else expressions with proper syntax and covers all cases.
  3. Final Answer:

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

    Nested if-else expressions assign multiple conditions [OK]
Hint: Use nested if-else expressions for multiple conditions [OK]
Common Mistakes:
  • Missing parentheses in nested if
  • Incorrect else placement
  • Adding extra else after braces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard