Pseudocode Questions for Placements — How to Trace Any Snippet (10 Solved, Answers Verified)
Pankaj Sharma ·

Pseudocode questions show you a few lines of language-free code and ask what it prints. They are reported in the tests of Infosys, Accenture, Capgemini (25–30 questions), Cognizant, HCLTech, LTIMindtree and Deloitte.
They are not hard because the code is hard. They are hard because students trace in their head and lose one value halfway. This guide gives you a method that removes that mistake, then 10 typical questions.
How we checked the answers: below every question is the same logic written in Python, and its real output. Our build runs each one automatically, so every answer on this page comes from running the code, not from working it out by hand.
The tracing-table method
- Write one column per variable, plus a column for the loop counter.
- Write one row per loop iteration (or per recursive call).
- Update values in the order the lines run — never update two variables "at the same time" in your head.
- Check the stop condition before each new row. Most wrong answers come from running one iteration too many or too few.
- Read the output line last: is it printing the variable, an array element, or something computed from them?
Rules that pseudocode questions rely on
| Rule | Meaning | Example |
|---|---|---|
for (i = 1 to n) | includes both 1 and n | runs n times |
/ on integers | integer division — the fraction is dropped | 17 / 5 = 3 |
mod | remainder | 17 mod 5 = 2 |
*, /, mod have equal priority | evaluate left to right | 17 / 5 * 2 = 6 |
&, ` | , ^` | bitwise AND, OR, XOR on binary digits |
<< 1 | shift left = multiply by 2 | 8 << 1 = 16 |
10 solved pseudocode questions
1. Sum of multiples in a loop
Integer s = 0
for (i = 1 to 20)
if (i mod 3 == 0)
s = s + i
end if
end for
Print s
The condition picks the multiples of 3 from 1 to 20: 3 + 6 + 9 + 12 + 15 + 18 = 63. Trap: forgetting that 20 is included (it doesn't matter here, but check it every time).
s = 0
for i in range(1, 21):
if i % 3 == 0:
s = s + i
print(s)
63
2. Reversing digits
Integer n = 4520, r = 0
while (n > 0)
r = r * 10 + (n mod 10)
n = n / 10
end while
Print r
| n | n mod 10 | r |
|---|---|---|
| 4520 | 0 | 0 |
| 452 | 2 | 2 |
| 45 | 5 | 25 |
| 4 | 4 | 254 |
Answer: 254, not 0254 — the leading zero disappears because r is an integer.
n, r = 4520, 0
while n > 0:
r = r * 10 + n % 10
n = n // 10
print(r)
254
3. Nested loops: count the iterations
Integer count = 0
for (i = 1 to 5)
for (j = i to 5)
count = count + 1
end for
end for
Print count
For i = 1 the inner loop runs 5 times, then 4, 3, 2, 1. Total = 5 + 4 + 3 + 2 + 1 = 15. Shortcut: n(n + 1)/2 = 5 × 6 / 2.
count = 0
for i in range(1, 6):
for j in range(i, 6):
count = count + 1
print(count)
15
4. Recursion (Fibonacci-style)
Integer fun(Integer x)
if (x <= 1) return 1
return fun(x - 1) + fun(x - 2)
End
Print fun(6)
Build up from the bottom instead of expanding the whole tree: fun(0) = 1, fun(1) = 1, fun(2) = 2, fun(3) = 3, fun(4) = 5, fun(5) = 8, fun(6) = 13.
def fun(x):
if x <= 1:
return 1
return fun(x - 1) + fun(x - 2)
print(fun(6))
13
5. Bitwise operators
Integer a = 12, b = 10
Print (a & b) << 1, a | b, a ^ b
12 = 1100 and 10 = 1010 in binary.
- a & b = 1000 = 8, shifted left once = 16
- a | b = 1110 = 14
- a ^ b = 0110 = 6
a, b = 12, 10
print((a & b) << 1, a | b, a ^ b)
16 14 6
6. Running total in an array
Integer arr[5] = {3, 1, 4, 1, 5}
for (i = 1 to 4)
arr[i] = arr[i] + arr[i - 1]
end for
Print arr[4]
Each element becomes the sum of everything up to it: {3, 4, 8, 9, 14}. arr[4] = 14. Trap: using the original arr[i − 1] instead of the already-updated one.
arr = [3, 1, 4, 1, 5]
for i in range(1, 5):
arr[i] = arr[i] + arr[i - 1]
print(arr[4])
14
7. Integer division and operator order
Integer x = 17
Integer y = x / 5 * 2 + x mod 5
Print y
Left to right for / and *: 17 / 5 = 3 (integer division), 3 × 2 = 6. Then 17 mod 5 = 2. y = 6 + 2 = 8. Trap: computing 5 × 2 first and getting 17 / 10.
x = 17
y = x // 5 * 2 + x % 5
print(y)
8
8. Euclid's algorithm (HCF)
Integer a = 84, b = 36
while (b != 0)
Integer t = b
b = a mod b
a = t
end while
Print a
(84, 36) → (36, 12) → (12, 0). It prints 12, the HCF of 84 and 36. Recognising this pattern saves you the full trace.
a, b = 84, 36
while b != 0:
a, b = b, a % b
print(a)
12
9. Recursion on digits
Integer f(Integer n)
if (n == 0) return 0
return (n mod 10) + f(n / 10)
End
Print f(9045)
Each call takes the last digit and recurses on the rest: 5 + 4 + 0 + 9 = 18 — the sum of the digits.
def f(n):
if n == 0:
return 0
return n % 10 + f(n // 10)
print(f(9045))
18
10. A loop that multiplies its counter
Integer i = 1, total = 0
while (i < 50)
total = total + i
i = i * 3
end while
Print total, i
| i (before) | total (after) |
|---|---|
| 1 | 1 |
| 3 | 4 |
| 9 | 13 |
| 27 | 40 |
Next i = 81, which fails i < 50, so it prints 40 81. Trap: adding 81 to the total — the condition is checked before the body runs.
i, total = 1, 0
while i < 50:
total = total + i
i = i * 3
print(total, i)
40 81
The traps behind most wrong answers
- Off-by-one in loops —
1 to nincludes n;while (i < n)stops before n. - Integer division — 7 / 2 is 3 in these questions, not 3.5.
- Using an old value after it was updated in the same loop (question 6).
- Expanding recursion as a tree when building up from the base case is faster (question 4).
- Printing the wrong thing — read the last line again before choosing an option.
Where to practise
- Company question sets with pseudocode sections: Infosys, Accenture, Capgemini, Deloitte, HCLTech, LTIMindtree.
- If loops and recursion feel shaky, the free C programming tutorials cover them step by step.
- See how pseudocode fits into each test in placement test patterns compared.
Frequently asked questions
What is pseudocode in placement tests?
Short, language-free code using words like Integer, for (i = 1 to n), mod and Print. You read it and choose what it prints. No compiler is involved, and you don't need to know any particular programming language's syntax.
Which companies ask pseudocode questions?
Infosys, Accenture, Capgemini, Cognizant, HCLTech, LTIMindtree and Deloitte all report pseudocode or code-output MCQs. Capgemini's pseudocode section is one of the largest, at about 25–30 questions.
How do I get faster at pseudocode questions?
Use a tracing table on paper, learn to recognise common patterns (sum of digits, reverse, HCF, Fibonacci, prefix sums), and practise 10–15 questions a day under a one-minute-per-question timer.
Does / mean normal division in pseudocode?
When both values are integers, placement pseudocode almost always means integer division — the fractional part is dropped (17 / 5 = 3). Read the question's notes in case it says otherwise.