Mixed Tracing Questions with Answers

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

Sample questions

Question 1Medium

What will be the output of the following pseudocode?

Integer a, b, i
Set a = 10, b = 4
for (each i from 1 to 3)
    if (a > b)
        a = a - b
    else
        b = b - a
    End if
    a = a + i
End for
Print a, b

Printed values are separated by spaces.

  1. 5 4
  2. 7 4
  3. 8 0
  4. 4 4

Answer: D. 4 4

Explanation

Trace three passes, subtracting the smaller from the larger and then adding i to a: 4 4.

Step-by-step solution

  1. Start with a = 10 and b = 4.
  2. Pass 1: a is bigger, so a = 10 − 4 = 6, then a = 6 + 1 = 7.
  3. Pass 2: a is bigger, so a = 7 − 4 = 3, then a = 3 + 2 = 5.
  4. Pass 3: a is bigger, so a = 5 − 4 = 1, then a = 1 + 3 = 4. The output is 4 4, the correct answer.

Why the other options are wrong

  • A. 5 4 is wrong; after pass 3, a is 4, not 5.
  • B. 7 4 is wrong; a is reduced to 4 in pass 3.
  • C. 8 0 is wrong; b stays 4 because a is always the bigger value here.

Tip: Trace each pass in a small table with the values of a and b. Update a, then add i.

Question 2Medium

What will be the output of the following pseudocode?

Integer i, x, y
Set x = 1, y = 1
for (each i from 1 to 4)
    x = x + y
    y = x - y
End for
Print x, y

Printed values are separated by spaces.

  1. 8 6
  2. 8 5
  3. 5 3
  4. 13 8

Answer: B. 8 5

Explanation

Each pass sets y to the old x and x to the old x + y, a Fibonacci-like step: 8 5.

Step-by-step solution

  1. Start with x = 1 and y = 1. Pass 1: x = 1 + 1 = 2, then y = 2 - 1 = 1.
  2. Pass 2: x = 2 + 1 = 3, then y = 3 - 1 = 2. Pass 3: x = 3 + 2 = 5, then y = 5 - 2 = 3.
  3. Pass 4: x = 5 + 3 = 8, then y = 8 - 3 = 5. The output is 8 5: option B.

Why the other options are wrong

  • A. Y is 5, not 6, because y becomes the old x minus the old y, which is 8 - 3.
  • C. 5 3 is the state after pass 3, not after pass 4.
  • D. 13 8 is the state after pass 5, but the loop runs only four times.

Tip: Trace swapping-style updates in a table, keeping the old value of each variable before it changes.

Question 3Medium

What will be printed by the following pseudocode?

Integer a, b, i
Set a = 20, b = 0
for (each i from 1 to 6)
    if (a mod 2 == 0)
        a = a / 2
    else
        a = a + i
        b = b + 1
    End if
End for
Print a, b

/ denotes integer division (the fractional part is discarded). Printed values are separated by spaces.

  1. 1 1
  2. 7 2
  3. 8 2
  4. 2 1

Answer: A. 1 1

Explanation

Each pass halves an even a or adds i to an odd a (counting those in b): 1 1.

Step-by-step solution

  1. a = 20 is even, so a = 10 (pass 1). Then a = 10 is even, so a = 5 (pass 2).
  2. Pass 3: a = 5 is odd, so a = 5 + 3 = 8 and b = 1.
  3. Pass 4: 8 becomes 4. Pass 5: 4 becomes 2. Pass 6: 2 becomes 1.
  4. The output is 1 1, the correct answer.

Why the other options are wrong

  • B. 7 2 is wrong; a ends at 1, and b counts only one odd step.
  • C. 8 2 is wrong; the last halvings take a down to 1.
  • D. 2 1 is wrong; pass 6 halves 2 to 1, so a ends at 1.

Tip: Trace each pass in order and count only the passes where the odd branch runs.

Question 4Medium

What will be printed by the following pseudocode?

Integer s, i
Set s = 0
for (each i from 1 to 12)
    s = (s * 10 + i) mod 1000
End for
Print s
  1. 11
  2. 122
  3. 233
  4. 123

Answer: B. 122

Explanation

s keeps the last three digits of the number formed by writing 1, 2, ..., 12 one after another (mod 1000 at each step): 122.

Step-by-step solution

  1. S keeps only the last three digits after each step, because of the mod 1000.
  2. After writing 5 the value is 345. Writing 6 to 9 gives 456, 567, 678 and 789. Writing 10 gives 900, and writing 11 gives 11.
  3. Writing 12 gives 11 × 10 + 12 = 122. The output is 122: option B.

Why the other options are wrong

  • A. 11 is the value after writing 11, but the loop also writes 12 last.
  • C. 233 is not the last three digits here; the final value is 122.
  • D. 123 is wrong; the last step is 11 × 10 + 12, which gives 122.

Tip: Mod 1000 after each step keeps only the last three digits of the number built so far.

Question 5Medium

What is the output of the following pseudocode?

Integer x, s
Set x = 11, s = 0
while (x > 0)
    s = s * 2 + (x mod 2)
    x = x / 2
End while
Print s

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

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

Answer: C. 13

Explanation

The loop reads the bits of 11 from least significant and rebuilds them in reverse order: 13.

Step-by-step solution

  1. 11 in binary is 1011.
  2. The loop reads the last bit first: 1, then 1, then 0, then 1.
  3. Each step sets s = s × 2 + bit, giving 1, then 3, then 6, then 13.
  4. The output is 13, the correct answer.

Why the other options are wrong

  • A. 12 is wrong; the bits read are 1, 1, 0, 1, which gives 13.
  • B. 14 is wrong; the last step is 6 × 2 + 1 = 13.
  • D. 50 is wrong; the loop gives 13, which is 1011 reversed as 1101.

Tip: Reading bits from the lowest end and building s = s × 2 + bit reverses the binary digits of x.

Question 6Medium

What will be printed by the following pseudocode?

Integer a, b, i
Set a = 3, b = 20
for (each i from 1 to 3)
    if (a > b)
        a = a - b
    else
        b = b - a
    End if
    a = a + i
End for
Print a, b

Printed values are separated by spaces.

  1. 6 7
  2. 9 7
  3. 9 8
  4. 6 13

Answer: B. 9 7

Explanation

Trace three passes, subtracting the smaller from the larger and then adding i to a: 9 7.

Step-by-step solution

  1. Start with a = 3 and b = 20.
  2. Pass 1: a is not bigger, so b = 20 − 3 = 17, then a = 3 + 1 = 4.
  3. Pass 2: a is not bigger, so b = 17 − 4 = 13, then a = 4 + 2 = 6.
  4. Pass 3: a is not bigger, so b = 13 − 6 = 7, then a = 6 + 3 = 9. The output is 9 7, the correct answer.

Why the other options are wrong

  • A. 6 7 is wrong; a ends at 9, not 6.
  • C. 9 8 is wrong; b is 7 after pass 3, not 8.
  • D. 6 13 is wrong; b drops to 7 in pass 3, and a ends at 9.

Tip: Trace each pass in a small table with the values of a and b. Update a, then add i.

Question 7Medium

Predict the output of the following pseudocode.

Integer a, b, c
Set a = 13, b = 9, c = 0
while (a > 0)
    if ((a & 1) == 1)
        c = c + b
    End if
    a = a >> 1
    b = b + b
End while
Print c

Note: & is bitwise AND, >> is right shift.

  1. 118
  2. 117
  3. 72
  4. 27

Answer: B. 117

Explanation

Each set bit of a adds the matching doubled b, which multiplies: 13 * 9 = 117.

Step-by-step solution

  1. 13 = 1101 in binary. Each set bit adds the current b, which doubles every step.
  2. Bits from low: 1 adds 9; 0; 1 adds 36; 1 adds 72.
  3. c = 9+36+72 = 117.

Why the other options are wrong

  • A. 118 is one more than the true result.
  • C. 72 counts only the highest bit.
  • D. 27 is 3 x 9, not the full product.

Tip: It computes a*b: 13 x 9 = 117.

Question 8Medium

What will be printed by the following pseudocode?

Integer x, i
Set x = 7
for (each i from 1 to 3)
    if (x mod 2 == 0)
        x = x / 2 + i
    else
        x = x * 3 - i
    End if
End for
Print x

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

  1. 12
  2. 54
  3. 9
  4. 23

Answer: C. 9

Explanation

Trace three passes, applying the even or odd rule each time: x = 9.

Step-by-step solution

  1. x = 7 is odd, so x becomes 7 × 3 - 1 = 20 in pass 1.
  2. x = 20 is even, so x becomes 10 + 2 = 12 in pass 2. Then 12 is even, so x becomes 6 + 3 = 9 in pass 3.
  3. The output is 9: option C.

Why the other options are wrong

  • A. 12 is the value after pass 2, not the final value.
  • B. 54 would need the odd rule applied to 18; the code gets 20 after pass 1.
  • D. 23 would come from adding i in the odd rule; the code subtracts i, so pass 1 gives 20.

Tip: Trace each pass in a column, checking the parity of x before applying the rule for that pass.

Question 9Hard

What is the output of the following pseudocode?

Integer a, b, k
Set a = 48, b = 18, k = 0
while (a != b)
    if (a > b)
        a = a - b
    else
        b = b - a
    End if
    k = k + 1
End while
Print a, k

Printed values are separated by spaces.

  1. 7 4
  2. 6 5
  3. 6 4
  4. 6 3

Answer: C. 6 4

Explanation

Subtractive Euclid ends at gcd = 6 after 4 subtractions.

Step-by-step solution

  1. The loop subtracts the smaller number from the larger: (48, 18) gives (30, 18), then (12, 18), then (12, 6), then (6, 6).
  2. That is 4 subtractions, and the loop stops when a and b are both 6.
  3. The output is 6 4: option C.

Why the other options are wrong

  • A. 7 is wrong; the common value of a and b is 6.
  • B. 5 counts one subtraction too many; the loop takes 4.
  • D. 3 counts one subtraction too few; the loop takes 4.

Tip: Subtractive Euclid: repeat subtracting the smaller from the larger until both are equal; that value is the gcd.

Question 10Hard

Predict the output of the following pseudocode.

Integer n, a, b
Set n = 100, a = 0, b = 0
while (n > 0)
    if (n mod 3 == 0)
        a = a + 1
        n = n / 3
    else
        b = b + 1
        n = n - 1
    End if
End while
Print a, b

/ denotes integer division (the fractional part is discarded). Printed values are separated by spaces.

  1. 1 1
  2. 4 5
  3. 1 98
  4. 4 4

Answer: D. 4 4

Explanation

Divide by 3 when possible, otherwise subtract 1; a counts divisions and b subtractions: 4 4.

Step-by-step solution

  1. n = 100 is not divisible by 3, so b adds 1 and n becomes 99.
  2. 99 divides to 33 and then to 11, so a is 2. Then 11 becomes 10 (b = 2) and 10 becomes 9 (b = 3).
  3. 9 divides to 3 and then to 1, so a is 4. Then 1 becomes 0 (b = 4). The output is 4 4: option D.

Why the other options are wrong

  • A. 1 1 is wrong; a counts the divisions and b counts the subtractions, which are 4 and 4.
  • B. 4 5 has one subtraction too many; the final b is 4.
  • C. 1 98 counts the steps wrongly; a is 4 after the divisions.

Tip: Trace a loop that has two counters by writing n, a and b on each line until n reaches 0.

Practise all 28 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