Autocoder

Pseudocode Aptitude Questions with Output Tracing

Last updated 2026-11-19

Pseudocode questions show a short program written in plain, language-neutral steps and ask for its output, the final value of a variable, or the number of times something runs. They are common in the aptitude and technical-aptitude parts of campus tests because they check whether you can read logic without needing a particular language. The method is a dry run: step through the code by hand, tracking each variable in a table.

This page explains dry-run technique and the places where candidates slip, then works through 15 programs from easy to hard. Every output below has been checked by running an equivalent program. For test-specific tips, read how to crack a pseudocode test, and for the broader aptitude plan, how to crack an aptitude test. Number-pattern practice is in number series questions.

Dry-run method

Conventions to confirm in each question

Arrays in pseudocode may start from index 0 or 1, so read the first line carefully. Division of two integers is normally integer division (the fraction is dropped), and the % operator gives the remainder. Arguments are assumed to be passed by value unless the question says otherwise, so changes inside a function do not affect the caller's variables. Where the question states a convention, follow it, even if your own language does it differently.

Typical traps

Off-by-one errors in loop bounds cause most wrong answers. The next most common cause is updating a variable in the wrong order, as in swapping without a temporary variable. A third is misreading the effect of continue and break: continue skips to the next iteration, while break leaves the loop. For recursion, remember that statements after the recursive call run as the calls return.

Pseudocode Aptitude Questions with Output Tracing: questions with worked answers

  1. 1

    What is printed by the following pseudocode?

    easy

    Dry run: x = 5, y = 3. After x = x + y, x is 8. After y = x - y, y is 8 - 3 = 5. After x = x - y, x is 8 - 5 = 3. This swaps the two values without a temporary variable. Output: 3 5.

    text
    x = 5
    y = 3
    x = x + y
    y = x - y
    x = x - y
    print x, y
  2. 2

    What is the output?

    easy

    The loop adds 1, 2, 3, 4 and 5 to sum: 1 + 2 + 3 + 4 + 5 = 15. Output: 15.

    text
    sum = 0
    for i = 1 to 5
        sum = sum + i
    print sum
  3. 3

    What does this program print?

    easy

    The value 7 is odd, because 7 % 2 is 1, not 0, so the else branch runs. Output: odd.

    text
    a = 7
    if a % 2 == 0
        print "even"
    else
        print "odd"
  4. 4

    What is the output of this program?

    easy

    Trace n and count. Start n = 10, count = 0. Pass 1: n = 7, count = 1. Pass 2: n = 4, count = 2. Pass 3: n = 1, count = 3. Pass 4: n = -2, count = 4. Now n > 0 is false, so the loop ends. Output: 4.

    text
    n = 10
    count = 0
    while n > 0
        n = n - 3
        count = count + 1
    print count
  5. 5

    What is the output? (Integer division is used.)

    easy

    Each pass adds the last digit and removes it. 1234: s = 4, n = 123. s = 7, n = 12. s = 9, n = 1. s = 10, n = 0. The loop ends. Output: 10, the digit sum of 1234.

    text
    n = 1234
    s = 0
    while n > 0
        s = s + n % 10
        n = n / 10
    print s
  6. 6

    How many times is the statement inside the inner loop executed, and what is printed?

    medium

    For i = 1 the inner loop runs once, for i = 2 twice and for i = 3 three times, so count = 1 + 2 + 3 = 6. Output: 6.

    text
    count = 0
    for i = 1 to 3
        for j = 1 to i
            count = count + 1
    print count
  7. 7

    What is the output? (The array index starts at 0.)

    medium

    m starts as 4. Comparing with 7 sets m to 7, 1 does not change it, 9 sets m to 9, and 3 does not change it. Output: 9, the largest element.

    text
    a = [4, 7, 1, 9, 3]
    m = a[0]
    for i = 1 to 4
        if a[i] > m
            m = a[i]
    print m
  8. 8

    What is printed? (Integer division is used.)

    medium

    n = 358, r = 0. Pass 1: r = 0 x 10 + 8 = 8, n = 35. Pass 2: r = 8 x 10 + 5 = 85, n = 3. Pass 3: r = 85 x 10 + 3 = 853, n = 0. Output: 853, the reverse of 358.

    text
    n = 358
    r = 0
    while n > 0
        r = r * 10 + n % 10
        n = n / 10
    print r
  9. 9

    What is the output?

    medium

    The loop variable takes 2, 6, 10, 14 and 18, since the step is 4 and the bound is 20. The sum is 2 + 6 + 10 + 14 + 18 = 50. Output: 50.

    text
    x = 0
    for i = 2 to 20 step 4
        x = x + i
    print x
  10. 10

    What is printed? (Indexing starts at 0 and length(s) is 5.)

    medium

    The loop reads the string from the last index to the first, appending each character to r. That builds the reverse. Output: edcba.

    text
    s = "abcde"
    r = ""
    for i = length(s) - 1 down to 0
        r = r + s[i]
    print r
  11. 11

    What is the output of print f(5)?

    medium

    f(5) = 5 x f(4) = 5 x 4 x f(3) = 5 x 4 x 3 x f(2) = 5 x 4 x 3 x 2 x f(1), and f(1) returns 1. The product is 120. Output: 120, the factorial of 5.

    text
    function f(n)
        if n <= 1
            return 1
        return n * f(n - 1)
    
    print f(5)
  12. 12

    What is the output? (Arguments are passed by value.)

    hard

    The function swaps its own copies a and b. Because the arguments are passed by value, x and y in the caller are unchanged. Output: 2 9.

    text
    function swap(a, b)
        t = a
        a = b
        b = t
    
    x = 2
    y = 9
    swap(x, y)
    print x, y
  13. 13

    What is the final value printed?

    hard

    Trace i from 1 to 10. i = 1: total = 1. i = 2: total = 3. i = 3: skipped by continue. i = 4: total = 7. i = 5: total = 12. i = 6: skipped. i = 7: total = 19. i = 8: the first test is false, and the second test i > 7 is true, so the loop breaks. Output: 19.

    text
    total = 0
    for i = 1 to 10
        if i % 3 == 0
            continue
        if i > 7
            break
        total = total + i
    print total
  14. 14

    What is the output of p(3), where each print goes on the same line?

    hard

    p(3) prints 3, then calls p(2), which prints 2 and calls p(1), which prints 1 and calls p(0), which returns. Then p(1) prints 1, p(2) prints 2 and p(3) prints 3 as the calls unwind. Output: 3 2 1 1 2 3.

    text
    function p(n)
        if n == 0
            return
        print n
        p(n - 1)
        print n
    
    p(3)
  15. 15

    What is printed by print gcd(48, 18)?

    hard

    Trace (a, b): start (48, 18). t = 18, b = 48 % 18 = 12, a = 18, giving (18, 12). Then t = 12, b = 18 % 12 = 6, a = 12, giving (12, 6). Then t = 6, b = 12 % 6 = 0, a = 6, giving (6, 0). The loop ends and the function returns 6. Output: 6, the greatest common divisor of 48 and 18.

    text
    function gcd(a, b)
        while b != 0
            t = b
            b = a % b
            a = t
        return a
    
    print gcd(48, 18)

Frequently asked questions

Do pseudocode questions need a particular language?

No. They use language-neutral steps, but check the conventions in each question, such as index base and integer division.

How should I trace a loop?

Write a table with one column per variable and one row per iteration, and update values in the order that the code runs.

What is the difference between break and continue?

Break exits the loop entirely, while continue skips the rest of the current pass and begins the next one.

How do I trace recursion?

Write each call on its own line, note what is printed before the recursive call, and then note what happens as each call returns.

How many pseudocode questions should I practise?

A few each day, with a mix of loops, arrays, strings and recursion, is enough to make dry runs quick.

Related guides

More in Aptitude Questions for Placements

See all in this section