Pseudocode questions in placement tests: how to predict output quickly
Pseudocode questions show you a short piece of code written in plain, language-neutral steps and ask what it prints. You do not write any code. To answer quickly, trace the variables in a small table, one line at a time, and watch for a handful of traps: loop limits, integer division, the order of operations and recursion.
Many service-company hiring processes include a coding or technical assessment after the aptitude test, and pseudocode questions often appear there. Our guides to the Capgemini, Accenture and Infosys tests cover where the section sits in each. The exact number of questions and the time change from drive to drive, so treat the method below as the constant part.
Quick facts
| Item | What to expect |
|---|---|
| What you see | A short program in plain steps: variables, if, loops, functions |
| What you answer | The output, or the value of a variable at the end |
| Format | Multiple choice, usually four options |
| Topics | Loops, conditions, integer division and mod, arrays, recursion, bitwise operators |
| Skill tested | Careful reading and tracing, not memorising syntax |
| Where it appears | Technical or pseudocode sections of service-company tests (varies by drive) |
Company patterns change between drives. Always read the instructions on the official careers page or in the test invitation for your drive.
The tracing method
- List every variable in a small table before you start: one column per variable, one row per step.
- Write the starting values exactly as given. A missing starting value is often the trick.
- Run the code line by line, updating only the variable that changes. Do not skip lines "because it is obvious".
- Count loop runs carefully. Write the loop counter in the first column so you can see when it stops.
- Check the last line: is it printing a variable, an expression (
a + b) or something inside the loop? - Look at the options before you finish. If two options differ by one, the question is testing a loop limit.
Worked example 1: a loop that swaps values
Integer a = 5, b = 3for (i = 1 to 3)a = a + bb = a - bend forprint a + b
| i | a | b |
|---|---|---|
| start | 5 | 3 |
| 1 | 8 | 5 |
| 2 | 13 | 8 |
| 3 | 21 | 13 |
The loop runs three times (1, 2 and 3), so the output is 21 + 13 = 34. Notice that b uses the new value of a in the same step. That is the most common place to slip.
Worked example 2: integer division and mod
Integer x = 47, s = 0while (x > 0)s = s + x mod 10x = x / 10end whileprint s
x mod 10 gives the last digit and x / 10 with integers drops the decimal part. First pass: s = 7, x = 4. Second pass: s = 11, x = 0. The loop stops and the output is 11, the sum of the digits. If you wrote x = 4.7, you would get a wrong answer. With Integer variables, division always throws away the fraction.
Worked example 3: recursion
Integer fun(Integer n)if (n <= 1) return 1return n + fun(n - 2)end functionprint fun(7)
Write the calls going down, then add on the way back up: fun(7) = 7 + fun(5) = 7 + 5 + fun(3) = 7 + 5 + 3 + fun(1) = 7 + 5 + 3 + 1 = 16. Check the stopping condition first: here it is n <= 1, so fun(1) returns 1 and does not call itself again.
Worked example 4: bitwise operators
Write numbers in binary before you apply the operator. With 6 = 110 and 3 = 011:
6 & 3(AND) = 010 = 26 | 3(OR) = 111 = 76 ^ 3(XOR) = 101 = 55 << 1(left shift by one) = 1010 = 10, the same as multiplying by 2
Traps that cost marks
- Loop limits:
for i = 1 to 5usually runs five times, buti < 5runs four. Check whether the limit is included. - Update order: a line that uses a variable changed just above it uses the new value.
- Integer division and mod with negative numbers: these are rare, but if they appear, read the question's convention carefully.
- Operator precedence:
,/andmodcome before+and-.a + b 2is not(a + b) * 2. - Short-circuit conditions: in
if (a > 0 && b / a > 1), the second part is not checked when the first is false. - Passing values to functions: if a function changes its own copy of a number, the original outside does not change, unless the question says it is passed by reference.
- Overflow and data types: questions that mention a small type (such as a 1-byte integer) may wrap around. Read the declarations.
A two-week preparation plan
- Days 1 to 3: revise loops, conditions, integer division,
modand operator precedence in one language you know well, such as C. Our free C programming practice is a good place to start. - Days 4 to 7: trace 15 short snippets a day by hand, with a table, before checking the answer.
- Days 8 to 10: add recursion, arrays and bitwise operators. Write the call chain for every recursive question.
- Days 11 to 14: take timed sets of 10 questions in about 10 to 15 minutes and note every mistake by type (loop limit, update order, precedence).
Pseudocode sits alongside aptitude in most tests, so keep some time for quant and reasoning practice too. All of it is free on Target Campus placement practice.
Frequently asked questions
Do I need to know a programming language for pseudocode questions?
You do not need to write code, but knowing the basics of one language (C, Java or Python) helps you read loops, conditions and functions quickly. The pseudocode itself is language-neutral.
Are pseudocode questions only for computer science students?
No. Service-company tests are often common to all engineering branches, so students from mechanical, civil, electrical and other branches may see the same section. The concepts needed are basic.
How much time should one question take?
With practice, most simple questions take about a minute. Recursion and nested loops take longer. If a question is still unclear after two minutes, mark your best guess (if there is no negative marking in your test) and move on.
Should I trace in my head or on paper?
On paper, always, unless the code is two lines long. A small table of values is faster than restarting after a mistake.
