Output-prediction and pseudocode questions in campus tests, solved
In this article (6 sections)
Related programme: the FDE for Freshers course (6–7 months; EMI from ₹8,000/month · full fee shared on your counselling call). See also open Forward Deployed Engineer jobs in India, updated daily.
Output-prediction and pseudocode questions show you a short program and ask what it prints. They test whether you can trace state — variable values, loop counters, what a function returns — without running the code. The reliable method is a trace table: one column per variable, one row per step, filled in with a pencil. The nine questions below cover the traps these sections return to most often: shared references, default arguments, integer division, loop bounds, slicing, swaps, bitwise operators, recursion and SQL NULLs.
Build a trace table before you answer
For any loop, write the variables across the top and update one row per iteration. It feels slow for the first week, then becomes faster than guessing — and it catches the one-off errors that cost most marks.
| Step | i | a | b |
|---|---|---|---|
| start | – | 5 | 3 |
| after i = 1 | 1 | 8 | 5 |
That fragment is the start of question 6 below.
Python questions
Q1. What does this print?
x = [1, 2, 3]
y = x
y.append(4)
print(x)Answer: `[1, 2, 3, 4]`. y = x does not copy the list; both names point to the same object. To copy, use y = x.copy() or y = list(x).
Q2. What do the two calls print?
def f(a, b=[]):
b.append(a)
return b
print(f(1))
print(f(2))Answer: `[1]` then `[1, 2]`. The default list is created once, when the function is defined, and reused on every call. The usual fix is b=None and if b is None: b = [] inside the function.
Q3. What is printed?
print(7 // 2, -7 // 2, 7 % 3, -7 % 3)Answer: `3 -4 1 2`. Python's // rounds down (towards negative infinity), so −3.5 becomes −4. The remainder takes the sign of the divisor, so −7 % 3 is 2 because −7 = 3 × (−3) + 2. C and Java give −3 and −1 for the negative cases, which is why tests like this question.
Q4. What is `count` after the loop?
count = 0
for i in range(1, 10, 3):
count += iAnswer: 12. range(1, 10, 3) gives 1, 4, 7 — it stops before 10. 1 + 4 + 7 = 12.
Q5. What is printed?
s = "placement"
print(s[::-1][:3])Answer: `tne`. s[::-1] reverses the string to "tnemecalp", and [:3] takes the first three characters.
Pseudocode questions
Pseudocode sections use a neutral syntax so that the question is about logic, not a language. Read = as assignment and assume integers unless told otherwise.
Q6. What is printed?
Integer a = 5, b = 3
for i = 1 to 3
a = a + b
b = a - b
end for
print a, bTrace: after i = 1, a = 8 and b = 8 − 3 = 5. After i = 2, a = 13 and b = 13 − 5 = 8. After i = 3, a = 21 and b = 21 − 8 = 13. Answer: `21 13` — consecutive Fibonacci numbers, which is a hint worth noticing when a loop adds and subtracts like this.
Q7. What is printed?
Integer p = 12, q = 10
print (p AND q), (p OR q), (p XOR q) // bitwise operators12 is 1100 and 10 is 1010 in binary. AND keeps bits set in both: 1000 = 8. OR keeps bits set in either: 1110 = 14. XOR keeps bits set in exactly one: 0110 = 6. Answer: `8 14 6`.
Q8. What does `fun(7)` return?
Integer fun(Integer n)
if n <= 1
return 1
return n + fun(n - 2)fun(7) = 7 + fun(5) = 7 + 5 + fun(3) = 7 + 5 + 3 + fun(1) = 7 + 5 + 3 + 1. Answer: 16. Write each call on its own line and add from the bottom up.
A SQL output question
Tests for data roles sometimes include a query and ask for its result.
Q9. Table `marks` has four rows for column `score`: 70, NULL, 90, NULL. What does this return?
SELECT COUNT(*), COUNT(score), AVG(score) FROM marks;Answer: `4, 2, 80.0`. COUNT(*) counts rows; COUNT(score) skips NULLs; AVG also skips NULLs, so it is (70 + 90) / 2, not 160 / 4.
Check all nine answers
After practising on paper, confirm your answers by running them. This script uses only the Python standard library, including the built-in sqlite3 module.
import sqlite3
x = [1, 2, 3]; y = x; y.append(4)
q1 = x
def f(a, b=[]):
b.append(a)
return b
q2 = (list(f(1)), list(f(2)))
q3 = (7 // 2, -7 // 2, 7 % 3, -7 % 3)
count = 0
for i in range(1, 10, 3):
count += i
q4 = count
q5 = "placement"[::-1][:3]
a, b = 5, 3
for i in range(1, 4):
a = a + b
b = a - b
q6 = (a, b)
q7 = (12 & 10, 12 | 10, 12 ^ 10)
def fun(n):
return 1 if n <= 1 else n + fun(n - 2)
q8 = fun(7)
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE marks (score INTEGER)")
con.executemany("INSERT INTO marks VALUES (?)", [(70,), (None,), (90,), (None,)])
q9 = con.execute("SELECT COUNT(*), COUNT(score), AVG(score) FROM marks").fetchone()
for n, ans in enumerate([q1, q2, q3, q4, q5, q6, q7, q8, q9], start=1):
print(f"Q{n}:", ans)Expected output: [1, 2, 3, 4], ([1], [1, 2]), (3, -4, 1, 2), 12, tne, (21, 13), (8, 14, 6), 16 and (4, 2, 80.0).
How to practise this section
- Write ten of your own questions a week from code you already know, predict the output, then run it. Writing questions teaches you where the traps are.
- When you get one wrong, name the rule (reference vs copy, floor division, off-by-one), not just the answer.
- Time yourself: most of these should take under a minute once you trace confidently.
These questions usually sit next to aptitude in the first round; see the 16-week placement plan for non-CS branches for where to fit them. If you enjoy tracing and debugging, that is the core daily skill of the engineering track in FDE for Freshers.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our FDE for Freshers programme — 6–7 months. Build your engineering foundations, then take AI from discovery to delivery.
Explore FDE for Freshers