Bird
Raised Fist0
Kotlinprogramming~20 mins

When without argument in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
When Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of when without argument with conditions
What is the output of this Kotlin code using when without an argument?
Kotlin
fun main() {
    val x = 15
    val result = when {
        x % 3 == 0 -> "Divisible by 3"
        x % 5 == 0 -> "Divisible by 5"
        else -> "Not divisible by 3 or 5"
    }
    println(result)
}
ACompilation error
BDivisible by 3
CNot divisible by 3 or 5
DDivisible by 5
Attempts:
2 left
💡 Hint
Check which condition matches first in the when block.
❓ Predict Output
intermediate
2:00remaining
When without argument with Boolean expressions
What will this Kotlin program print?
Kotlin
fun main() {
    val a = 10
    val b = 20
    val output = when {
        a > b -> "a is greater"
        a == b -> "a equals b"
        else -> "a is smaller"
    }
    println(output)
}
Aa is smaller
Ba equals b
Ca is greater
DRuntime exception
Attempts:
2 left
💡 Hint
Compare the values of a and b carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in when without argument
What error does this Kotlin code produce?
Kotlin
fun main() {
    val num = 5
    val result = when {
        num > 0 -> "Positive"
        num < 0 -> "Negative"
    }
    println(result)
}
APrints "Negative"
BRuntime exception: NullPointerException
CPrints "Positive"
DCompilation error: 'when' must be exhaustive
Attempts:
2 left
💡 Hint
Check if all possible cases are covered in the when expression.
🧠 Conceptual
advanced
1:30remaining
Behavior of when without argument with multiple true conditions
In Kotlin, when using when without an argument, what happens if multiple conditions are true?
AOnly the first true condition's branch executes
BAll matching branches execute sequentially
CThe last true condition's branch executes
DCompilation error due to ambiguity
Attempts:
2 left
💡 Hint
Think about how when expressions work as a control flow.
🚀 Application
expert
2:30remaining
Using when without argument to classify numbers
What is the output of this Kotlin program?
Kotlin
fun main() {
    val n = -7
    val classification = when {
        n > 0 && n % 2 == 0 -> "Positive even"
        n > 0 && n % 2 != 0 -> "Positive odd"
        n < 0 && n % 2 == 0 -> "Negative even"
        n < 0 && n % 2 != 0 -> "Negative odd"
        else -> "Zero"
    }
    println(classification)
}
ANegative even
BPositive even
CNegative odd
DZero
Attempts:
2 left
💡 Hint
Check the sign and parity of n carefully.

Practice

(1/5)
1. What does when without argument do in Kotlin?
easy
A. It matches a value against constants only.
B. It checks boolean conditions one by one until one is true.
C. It creates a loop that runs multiple times.
D. It declares a variable with multiple types.

Solution

  1. Step 1: Understand when without argument

    When used without an argument, when evaluates each condition as a boolean expression.
  2. Step 2: Compare with other uses

    Unlike when with argument, it does not match values but checks conditions one by one.
  3. Final Answer:

    It checks boolean conditions one by one until one is true. -> Option B
  4. Quick Check:

    when without argument = boolean checks [OK]
Hint: Remember: no argument means boolean conditions checked [OK]
Common Mistakes:
  • Thinking it matches values like switch-case
  • Confusing it with loops
  • Assuming it declares variables
2. Which of the following is the correct syntax for when without argument in Kotlin?
easy
A. when { condition1 -> action1 condition2 -> action2 else -> defaultAction }
B. when(value) { 1 -> action1 2 -> action2 else -> defaultAction }
C. when (condition) { true -> action1 false -> action2 }
D. when (value) { condition1 -> action1 condition2 -> action2 }

Solution

  1. Step 1: Identify when without argument syntax

    The correct syntax omits the argument and uses boolean conditions directly inside curly braces.
  2. Step 2: Check options

    when { condition1 -> action1 condition2 -> action2 else -> defaultAction } matches this pattern with conditions and an else branch; others use an argument or incorrect structure.
  3. Final Answer:

    when { condition1 -> action1 condition2 -> action2 else -> defaultAction } -> Option A
  4. Quick Check:

    Correct syntax = when { condition1 -> action1 condition2 -> action2 else -> defaultAction } [OK]
Hint: No argument means conditions directly inside braces [OK]
Common Mistakes:
  • Adding argument inside parentheses
  • Missing else branch
  • Using values instead of conditions
3. What is the output of this Kotlin code?
val x = 10
when {
  x < 5 -> println("Less than 5")
  x in 5..10 -> println("Between 5 and 10")
  else -> println("More than 10")
}
medium
A. More than 10
B. Less than 5
C. Between 5 and 10
D. No output

Solution

  1. Step 1: Evaluate conditions in order

    First condition: x < 5 is false (10 < 5 is false). Second condition: x in 5..10 is true (10 is in 5 to 10).
  2. Step 2: Execute matching branch

    Since second condition is true, it prints "Between 5 and 10" and stops checking further.
  3. Final Answer:

    Between 5 and 10 -> Option C
  4. Quick Check:

    10 in 5..10 = true [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Assuming x < 5 is true
  • Ignoring range check
  • Expecting else branch output
4. Find the error in this Kotlin code using when without argument:
val y = 3
val result = when {
  y > 5 -> println("Greater than 5")
  y < 5 -> println("Less than 5")
}
medium
A. Missing else branch for unmatched cases
B. Incorrect use of parentheses after when
C. Conditions must be constants, not expressions
D. Cannot use println inside when

Solution

  1. Step 1: Check when branches

    The code has two conditions but no else branch to handle all other cases.
  2. Step 2: Understand Kotlin requirement

    When used without argument, Kotlin requires an else branch to cover all possibilities or the compiler errors about non-exhaustive when.
  3. Final Answer:

    Missing else branch for unmatched cases -> Option A
  4. Quick Check:

    Always add else branch [OK]
Hint: Always include else branch in when without argument [OK]
Common Mistakes:
  • Ignoring else branch requirement
  • Adding parentheses after when
  • Thinking conditions must be constants
5. You want to print a message based on temperature:
val temp = 25
when {
  temp <= 0 -> println("Freezing")
  temp in 1..15 -> println("Cold")
  temp in 16..25 -> println("Warm")
  temp > 25 -> println("Hot")
  else -> println("Unknown")
}

What will be printed and why is this when usage correct?
hard
A. Compilation error due to overlapping ranges
B. "Cold"; because 25 is in 1..15 range
C. "Hot"; because 25 is greater than 25
D. "Warm"; because conditions cover all ranges with else as fallback

Solution

  1. Step 1: Check which condition matches temp = 25

    temp <= 0 is false; temp in 1..15 is false; temp in 16..25 is true (25 is included).
  2. Step 2: Confirm correctness of when usage

    All possible values are covered by conditions and else branch ensures fallback; no overlap causes error.
  3. Final Answer:

    "Warm"; because conditions cover all ranges with else as fallback -> Option D
  4. Quick Check:

    25 in 16..25 = true, else covers all [OK]
Hint: Check ranges carefully; else handles unexpected values [OK]
Common Mistakes:
  • Misreading range boundaries
  • Assuming 25 > 25 is true
  • Thinking else is optional
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