Control Flow & when Questions with Answers

24 practice questions on Control Flow & when (Kotlin) with answers and step-by-step solutions. Try 10 samples below, then practise all 24 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

for (i in 1..10 step 4) print(i)
  1. 1234
  2. 15
  3. 159
  4. 1591

Answer: C. 159

Explanation

step 4 adds 4 each time: 1, 5, 9. 13 exceeds 10.

Step-by-step solution

  1. 1.
  2. 1+4 = 5.
  3. 5+4 = 9; next 13 > 10, stop.

Why the other options are wrong

  • A. step 4 skips values.
  • B. 9 is still within 10.
  • D. No extra element.

Question 2Easy

In a Kotlin when, how do you match several values in one branch?

  1. Use a pipe: 1 | 2 -> ...
  2. Write two branches with fallthrough
  3. Separate them with commas: 1, 2 -> ...
  4. Use case 1, case 2:

Answer: C. Separate them with commas: 1, 2 -> ...

Explanation

when (x) { 1, 2 -> ... } matches either value.

Step-by-step solution

  1. Branches do not fall through.
  2. Combine conditions with commas.
  3. One branch handles both.

Why the other options are wrong

  • A. | is not valid there.
  • B. Kotlin when has no fallthrough.
  • D. case is not a Kotlin keyword.

Question 3Easy

What is the output?

for (i in 1..4) print(i)
  1. 123
  2. 12345
  3. 1234
  4. 4321

Answer: C. 1234

Explanation

1..4 is inclusive of both ends, so the loop prints 1, 2, 3, 4.

Step-by-step solution

  1. Range 1..4 holds 1,2,3,4.
  2. print does not add newlines.
  3. Output is 1234.

Why the other options are wrong

  • A. The range 1..4 includes 4.
  • B. The range stops at 4.
  • D. The range counts upward.

Question 4Easy

What is the output?

val x = 7
val r = when {
    x < 5 -> "low"
    x < 10 -> "mid"
    else -> "high"
}
println(r)
  1. mid
  2. low
  3. high
  4. mid high

Answer: A. mid

Explanation

when without a subject evaluates conditions in order. 7 < 5 is false, 7 < 10 is true, giving "mid".

Step-by-step solution

  1. Check x < 5: false.
  2. Check x < 10: true.
  3. Result is mid.

Why the other options are wrong

  • B. 7 is not less than 5.
  • C. The second condition matches first.
  • D. Only the first matching branch runs.

Question 5Medium

What is the output?

var n = 0
do {
    n++
} while (n < 0)
println(n)
  1. 0
  2. -1
  3. It loops forever
  4. 1

Answer: D. 1

Explanation

A do-while runs once before testing the condition. n becomes 1, then 1 < 0 is false.

Step-by-step solution

  1. Body runs first: n=1.
  2. Test 1 < 0 is false.
  3. Print 1.

Why the other options are wrong

  • A. do-while runs the body at least once.
  • B. n only increments.
  • C. The condition is false after the first pass.

Question 6Medium

What is the output?

outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (j == 2) continue@outer
        print("$i$j ")
    }
}
  1. 11 12 13 21 22 23 31 32 33
  2. 11 12 21 22 31 32
  3. 11
  4. 11 21 31

Answer: D. 11 21 31

Explanation

continue@outer jumps to the next i whenever j is 2, so only j=1 is printed for each i.

Step-by-step solution

  1. For each i, j=1 prints.
  2. At j=2 jump to next i.
  3. Output: 11 21 31.

Why the other options are wrong

  • A. continue@outer abandons the inner loop at j == 2.
  • B. j=2 triggers the jump before print.
  • C. Every i still runs.

Question 7Medium

What is the output?

val n = 15
val s = when {
    n % 15 == 0 -> "FizzBuzz"
    n % 3 == 0 -> "Fizz"
    n % 5 == 0 -> "Buzz"
    else -> "$n"
}
println(s)
  1. Fizz
  2. Buzz
  3. FizzBuzz
  4. 15

Answer: C. FizzBuzz

Explanation

15 % 15 == 0 is the first true condition so "FizzBuzz" is chosen.

Step-by-step solution

  1. 15 % 15 = 0.
  2. First branch matches.
  3. FizzBuzz.

Why the other options are wrong

  • A. The 15 check comes first.
  • B. The 15 check comes first.
  • D. Branches match before else.

Question 8Medium

What is the output?

val x: Any = 3.5
val r = when (x) {
    is Int -> "int"
    is Double -> "double"
    else -> "other"
}
println(r)
  1. int
  2. other
  3. double
  4. 3.5

Answer: C. double

Explanation

is checks the runtime type; 3.5 is a Double, so the second branch is taken.

Step-by-step solution

  1. x holds a Double.
  2. is Int false.
  3. is Double true -> "double".

Why the other options are wrong

  • A. 3.5 is a Double.
  • B. Double matches a branch.
  • D. The branch returns a string.

Question 9Hard

What does val r = for (i in 1..3) i do?

  1. Does not compile: for is a statement, not an expression
  2. r becomes 3
  3. r becomes the list [1, 2, 3]
  4. r becomes Unit

Answer: A. Does not compile: for is a statement, not an expression

Explanation

Unlike if and when, a for loop is a statement and cannot be used as a value.

Step-by-step solution

  1. if/when are expressions.
  2. for/while are statements.
  3. Assigning one to a val is a compile error.

Why the other options are wrong

  • B. A for loop returns no value.
  • C. That would need map.
  • D. It is not allowed at all, not even as Unit.

Question 10Hard

What is the output?

val r = (1..3).map { i ->
    when {
        i % 2 == 0 -> "E"
        else -> "O"
    }
}
println(r)
  1. [1, 2, 3]
  2. [O, O, O]
  3. [O, E, O]
  4. [E, O, E]

Answer: C. [O, E, O]

Explanation

1 is odd, 2 is even, 3 is odd, so the list prints as [O, E, O].

Step-by-step solution

  1. map transforms each element.
  2. 1->O, 2->E, 3->O.
  3. List prints as [O, E, O].

Why the other options are wrong

  • A. The lambda maps to letters.
  • B. 2 is even.
  • D. 1 and 3 are odd.

Practise all 24 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Kotlin topics · All topics