Loops Questions with Answers

92 practice questions on Loops (Pseudocode & Code Tracing) with answers and step-by-step solutions. Try 10 samples below, then practise all 92 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output of the following pseudocode?

Integer i, c
Set c = 0
for (each i from 3 to 12)
    c = c + 2
End for
Print c
  1. 21
  2. 20
  3. 18
  4. 22

Answer: B. 20

Explanation

The loop runs once for every integer from 3 to 12 inclusive, adding 2 each time: 20.

Step-by-step solution

  1. I goes from 3 to 12 inclusive, which is 10 values.
  2. C adds 2 on each pass.
  3. 10 × 2 = 20. The output is 20: option B.

Why the other options are wrong

  • A. 21 is odd, but each pass adds 2, so the total is always even.
  • C. 18 counts 9 passes, but the range 3 to 12 has 10 values.
  • D. 22 counts 11 passes, but the range has only 10 values.

Tip: The count of integers from a to b inclusive is b - a + 1.

Question 2Easy

What will be printed by the following pseudocode?

Integer i, c
Set c = 0
for (each i from 1 to 20)
    if (i mod 15 == 0)
        c = c + 3
    else if (i mod 5 == 0)
        c = c + 2
    else if (i mod 3 == 0)
        c = c + 1
    End if
End for
Print c
  1. 47
  2. 15
  3. 12
  4. 14

Answer: D. 14

Explanation

Multiples of 15 add 3, other multiples of 5 add 2 and other multiples of 3 add 1: total 14.

Step-by-step solution

  1. 15 is a multiple of 15, so it adds 3.
  2. 5, 10 and 20 are other multiples of 5, so they add 2 each, which is 6.
  3. 3, 6, 9, 12 and 18 are other multiples of 3, so they add 1 each, which is 5. The total is 14: option D.

Why the other options are wrong

  • A. 47 is far too large; the total is 3 + 6 + 5, which is 14.
  • B. 15 is one more than the correct total of 14.
  • C. 12 is 2 short; the multiples of 5 and 3 add up to more than that.

Tip: In an if-else chain, check the most specific case first, so 15 is not counted as only a multiple of 3 or 5.

Question 3Easy

Predict the output of the following pseudocode.

Integer i, p
Set p = 1
for (each i from 1 to 9)
    if (i mod 2 == 0)
        p = p * i
    End if
End for
Print p
  1. 384
  2. 3840
  3. 1
  4. 385

Answer: A. 384

Explanation

Only even values of i are multiplied in, so p = 2 * 4 * 6 * 8 = 384.

Step-by-step solution

  1. P starts at 1.
  2. Only the even values of i from 1 to 9 multiply p: 2, 4, 6, 8.
  3. p = 1 × 2 × 4 × 6 × 8 = 384.
  4. Print p gives 384, option A.

Why the other options are wrong

  • B. 3840 has an extra factor of 10; the even values are only 2, 4, 6 and 8.
  • C. 1 would be the result only if no even value matched; several do here.
  • D. 385 is not the product; the product of 2, 4, 6 and 8 is 384.

Tip: Read the if condition carefully; only the matching values change the running product.

Question 4Easy

Predict the output of the following pseudocode.

Integer i, x
Set x = 10
for (each i from 1 to 4)
    x = x * 2 - i
End for
Print x
  1. 263
  2. 69
  3. 135
  4. 134

Answer: D. 134

Explanation

Each pass doubles x and subtracts i: x = 134 after four passes.

Step-by-step solution

  1. X starts at 10.
  2. Pass 1: 10 × 2 − 1 = 19. Pass 2: 19 × 2 − 2 = 36.
  3. Pass 3: 36 × 2 − 3 = 69. Pass 4: 69 × 2 − 4 = 134.
  4. The output is 134, the correct answer.

Why the other options are wrong

  • A. 263 is wrong; there are only four passes, and the last one gives 134.
  • B. 69 is only the value after pass 3.
  • C. 135 is wrong; pass 4 gives 69 × 2 − 4 = 134.

Tip: Trace each pass using the value from the pass before, not the starting value.

Question 5Medium

What is the output of the following pseudocode?

Integer i, s, t
Set s = 0, t = 1
for (each i from 1 to 10)
    s = s + t * i
    t = 0 - t
End for
Print s
  1. 6
  2. -5
  3. 5
  4. -4

Answer: B. -5

Explanation

t alternates between 1 and -1, so s = 1 - 2 + 3 - 4 + ... up to 10 = -5.

Step-by-step solution

  1. T starts at 1 and flips sign on every pass, so the added terms are 1 × 1, then minus 2, plus 3, minus 4, and so on.
  2. The terms pair up as (1 - 2) + (3 - 4) + (5 - 6) + (7 - 8) + (9 - 10), and each pair is -1.
  3. Five pairs give -5. The output is -5: option B.

Why the other options are wrong

  • A. 6 has the wrong sign; the alternating sum of 1 to 10 is -5.
  • C. 5 has the wrong sign; each pair gives -1, so the total is -5.
  • D. -4 is one off; the five pairs add up to -5.

Tip: An alternating sum of 1 to 2n is -n, because each pair of terms gives -1.

Question 6Medium

What will be the output of the following pseudocode?

Integer x, c
Set x = 1000, c = 0
while (x != 0)
    x = x / 2
    c = c + 1
End while
Print c

/ denotes integer division (the fractional part is discarded).

  1. 10
  2. 9
  3. 1
  4. 11

Answer: A. 10

Explanation

c counts how many halvings bring 1000 to 0, i.e. the number of bits in 1000: 10.

Step-by-step solution

  1. x = 1000 is halved with integer division on each pass.
  2. The values are 500, 250, 125, 62, 31, 15, 7, 3, 1 and then 0.
  3. That is 10 halvings, so the output is 10: option A.

Why the other options are wrong

  • B. 9 misses the last halving, which is the one that reaches 0.
  • C. 1 counts only one halving; the loop runs 10 times.
  • D. 11 counts one halving too many; the value reaches 0 on the tenth pass.

Tip: Halving with integer division until 0 counts the number of bits in the number.

Question 7Medium

What is the output of the following pseudocode?

Integer i, j
Set i = 0, j = 20
while (i < j)
    i = i + 2
    j = j - 1
End while
Print i, j

Printed values are separated by spaces.

  1. 14 12
  2. 15 13
  3. 14 14
  4. 14 13

Answer: D. 14 13

Explanation

i rises by 2 and j falls by 1 until i >= j:
they end at 14 13.

Step-by-step solution

  1. I rises by 2 and j falls by 1 on each pass, starting from i = 0 and j = 20.
  2. After the passes the values are (2, 19), (4, 18), (6, 17), (8, 16), (10, 15), (12, 14) and (14, 13).
  3. At (14, 13) the test 14 < 13 is false, so the loop stops. The output is 14 13: option D.

Why the other options are wrong

  • A. J is 13, not 12, because j falls by 1 on each of the 7 passes.
  • B. I is 14, not 15, because i rises by 2 on each of the 7 passes.
  • C. J is 13, not 14, because j falls by 1 on each pass.

Tip: When two counters move in opposite directions, write one row per pass until the test fails.

Question 8Medium

Predict the output of the following pseudocode.

Integer n, steps
Set n = 12, steps = 0
while (n != 1)
    if (n mod 2 == 0)
        n = n / 2
    else
        n = 3 * n + 1
    End if
    steps = steps + 1
End while
Print steps

/ denotes integer division (the fractional part is discarded).

  1. 9
  2. 11
  3. 8
  4. 10

Answer: A. 9

Explanation

This is the Collatz process starting from 12; it reaches 1 after 9 steps.

Step-by-step solution

  1. n = 12 and steps = 0; each pass changes n and adds 1 to steps.
  2. Even n is halved, odd n becomes 3n + 1: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
  3. That is 9 changes before n reaches 1.
  4. Print steps gives 9, option A.

Why the other options are wrong

  • B. 11 counts two extra changes; the chain from 12 to 1 has 9 changes.
  • C. 8 stops one change early; 1 is reached on the 9th change.
  • D. 10 counts one change too many.

Tip: Count the passes through the loop body, not the numbers in the sequence.

Question 9Hard

What will be printed by the following pseudocode?

Integer n, best, c, i
Set n = 1000, best = 0, c = 0
for (each i from 0 to 10)
    if ((n mod 2) == 1)
        c = c + 1
        if (c > best)
            best = c
        End if
    else
        c = 0
    End if
    n = n / 2
End for
Print best

/ denotes integer division (the fractional part is discarded).

  1. 0
  2. 5
  3. 6
  4. 1

Answer: B. 5

Explanation

The loop scans the binary digits of the starting value and tracks the longest run of 1s: 5.

Step-by-step solution

  1. 1000 is 1111101000 in binary.
  2. The loop reads the bits from the lowest end: 0, 0, 0, 1, 0, then five 1s, then 0.
  3. The longest run of 1s is five, so best = 5, the correct answer.

Why the other options are wrong

  • A. 0 is wrong; 1000 has runs of 1s.
  • C. 6 is wrong; the longest run is 5.
  • D. 1 is wrong; the 1 bits at positions 5 to 9 form a run of five.

Tip: To track the longest run, reset the count to 0 on every 0 bit and keep the biggest count seen.

Question 10Hard

What will be the output of the following pseudocode?

Integer n, c, d
Set n = 28, c = 0
for (each d from 1 to n)
    if (n mod d == 0)
        c = c + d
    End if
End for
Print c - n
  1. 0
  2. -28
  3. 27
  4. 28

Answer: D. 28

Explanation

c is the sum of all divisors of 28 (56); subtracting n leaves the sum of proper divisors, 28.

Step-by-step solution

  1. c adds up all divisors of 28.
  2. 1+2+4+7+14+28 = 56.
  3. Print 56 - 28 = 28.

Why the other options are wrong

  • A. 0 is the answer only if c were 28.
  • B. -28 assumes c = 0.
  • C. 27 is off by one.

Practise all 92 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 Pseudocode & Code Tracing topics · All topics