Bird
Raised Fist0
Kotlinprogramming~10 mins

When with multiple conditions in Kotlin - Interactive Code Practice

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

Complete the code to print "Weekend" when day is Saturday or Sunday.

Kotlin
val day = "Saturday"
when(day) {
    [1] -> println("Weekend")
    else -> println("Weekday")
}
Drag options to blanks, or click blank then click option'
A"Saturday", "Sunday"
B"Saturday", "Sunday", "Friday"
C"Saturday"
D"Sunday"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting multiple values without commas
Using multiple when branches instead of one with multiple values
2fill in blank
medium

Complete the code to print "Vowel" when letter is 'a', 'e', 'i', 'o', or 'u'.

Kotlin
val letter = 'e'
when(letter) {
    [1] -> println("Vowel")
    else -> println("Consonant")
}
Drag options to blanks, or click blank then click option'
A'x', 'y', 'z'
B'a', 'e', 'i'
C'a', 'b', 'c'
D'a', 'e', 'i', 'o', 'u'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for characters
Listing only some vowels
3fill in blank
hard

Fix the error in the when expression to match multiple numbers for "Small number".

Kotlin
val num = 3
when(num) {
    [1] -> println("Small number")
    else -> println("Other number")
}
Drag options to blanks, or click blank then click option'
A(1, 2, 3)
B1, 2, 3
C[1, 2, 3]
D"1, 2, 3"
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets around multiple values
Putting multiple values inside a string
4fill in blank
hard

Fill both blanks to print "Weekend" for Saturday or Sunday and "Weekday" otherwise.

Kotlin
val day = "Sunday"
when(day) {
    [1] -> println("Weekend")
    [2] -> println("Weekday")
}
Drag options to blanks, or click blank then click option'
A"Saturday", "Sunday"
Belse
C"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
D"Holiday"
Attempts:
3 left
💡 Hint
Common Mistakes
Using else in the first branch
Listing all days explicitly instead of else
5fill in blank
hard

Fill all three blanks to print "Vowel", "Digit", or "Other" based on the input character.

Kotlin
val ch = '7'
when(ch) {
    [1] -> println("Vowel")
    [2] -> println("Digit")
    [3] -> println("Other")
}
Drag options to blanks, or click blank then click option'
A'a', 'e', 'i', 'o', 'u'
B'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
Celse
D'b', 'c', 'd'
Attempts:
3 left
💡 Hint
Common Mistakes
Using else in the middle branch
Not listing all digits
Using quotes incorrectly

Practice

(1/5)
1.

What does the when expression do when multiple conditions are separated by commas in Kotlin?

easy
A. It throws an error if multiple conditions are used.
B. It checks if the value matches any of the listed conditions and executes the corresponding block.
C. It only checks the first condition and ignores the rest.
D. It requires all conditions to be true at the same time to execute the block.

Solution

  1. Step 1: Understand how when handles multiple conditions

    In Kotlin, when multiple conditions are separated by commas, it means the when expression checks if the value matches any one of those conditions.
  2. Step 2: Interpret the behavior of multiple conditions

    If the value matches any condition in the list, the corresponding block runs. It does not require all conditions to be true simultaneously.
  3. Final Answer:

    It checks if the value matches any of the listed conditions and executes the corresponding block. -> Option B
  4. Quick Check:

    Multiple conditions = any match triggers block [OK]
Hint: Multiple conditions mean OR logic, not AND [OK]
Common Mistakes:
  • Thinking all conditions must be true at once
  • Believing only the first condition is checked
  • Assuming multiple conditions cause errors
2.

Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?

when (num) {
    ? -> println("Number is 1, 2, or 3")
    else -> println("Other number")
}
easy
A. 1, 2, 3
B. 1 || 2 || 3
C. 1 && 2 && 3
D. [1, 2, 3]

Solution

  1. Step 1: Recall Kotlin when syntax for multiple conditions

    In Kotlin, multiple conditions in a when case are separated by commas without operators.
  2. Step 2: Match the correct syntax

    1, 2, 3 uses commas to separate values: 1, 2, 3, which is correct. Other options use logical operators or list syntax, which are invalid here.
  3. Final Answer:

    1, 2, 3 -> Option A
  4. Quick Check:

    Multiple conditions use commas [OK]
Hint: Use commas, not operators, to separate conditions [OK]
Common Mistakes:
  • Using || or && inside when conditions
  • Trying to use list syntax like [1, 2, 3]
  • Missing commas between values
3.

What will be the output of the following Kotlin code?

val x = 5
when (x) {
    1, 2, 3 -> println("Low")
    4, 5, 6 -> println("Medium")
    else -> println("High")
}
medium
A. Medium
B. High
C. Low
D. No output

Solution

  1. Step 1: Identify the value of x

    The variable x is assigned the value 5.
  2. Step 2: Match x with when conditions

    The second case checks if x is 4, 5, or 6. Since x is 5, this condition matches and prints "Medium".
  3. Final Answer:

    Medium -> Option A
  4. Quick Check:

    5 in 4,5,6 prints Medium [OK]
Hint: Check which group contains the value [OK]
Common Mistakes:
  • Choosing the first matching group incorrectly
  • Confusing else with matching values
  • Assuming no output if multiple conditions exist
4.

Find the error in this Kotlin when expression:

val grade = 'B'
when (grade) {
    'A', 'B' -> println("Excellent")
    'C' || 'D' -> println("Good")
    else -> println("Try again")
}
medium
A. The variable grade should be an Int, not Char.
B. Using commas to separate 'A' and 'B' is wrong.
C. Missing parentheses around conditions.
D. Using '||' inside when conditions is invalid syntax.

Solution

  1. Step 1: Review the when condition syntax

    In Kotlin, multiple conditions in a when case must be separated by commas, not logical operators like '||'.
  2. Step 2: Identify the incorrect usage

    The line 'C' || 'D' uses '||', which is invalid syntax in when. It should be 'C', 'D'.
  3. Final Answer:

    Using '||' inside when conditions is invalid syntax. -> Option D
  4. Quick Check:

    Use commas, not logical operators [OK]
Hint: Never use || or && in when conditions [OK]
Common Mistakes:
  • Using logical operators instead of commas
  • Thinking commas are wrong separators
  • Confusing variable types with syntax errors
5.

You want to write a Kotlin when expression that prints "Weekend" if the day is either "Saturday" or "Sunday", "Weekday" if the day is any other day of the week, and "Invalid" for any other input. Which code correctly implements this?

hard
A. when(day) { in listOf("Saturday", "Sunday") -> println("Weekend") else -> println("Weekday") }
B. when(day) { "Saturday" || "Sunday" -> println("Weekend") else -> println("Weekday") }
C. when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") }
D. when(day) { "Saturday" && "Sunday" -> println("Weekend") else -> println("Weekday") }

Solution

  1. Step 1: Check correct multiple condition syntax for weekend days

    when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } correctly uses commas to separate "Saturday" and "Sunday" to match either day.
  2. Step 2: Verify handling of other days and else case

    when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } lists all weekdays explicitly and uses else for invalid inputs, matching the problem requirements.
  3. Final Answer:

    when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } -> Option C
  4. Quick Check:

    Use commas for multiple matches and else for fallback [OK]
Hint: List all matches with commas; use else for others [OK]
Common Mistakes:
  • Using || or && instead of commas
  • Missing else for invalid inputs
  • Not listing all weekdays explicitly
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