On this page
Many CBSE computer science students lose easy marks on questions that begin "Predict the output of the following Python code". The code is short, so it feels safe to read it once and write an answer. That is where slips happen: a loop that runs one time fewer than you thought, a list changed halfway through, an integer that turns into a float. This post gives a tracing method you can use on every such question, works through one from the official sample paper, and lists the mistakes to watch for.
How often do output questions appear?
The CBSE sample question paper for 2026-27 for computer science (083) has 37 questions for 70 marks. Several ask you to predict output or find the result of an expression: question 1 (a list after insert), question 10 (an arithmetic expression), question 16 (a function that changes a variable), question 18 (string slicing) and question 26, a two-mark loop over a string. The 2026-27 curriculum gives the programming unit 40 of the 70 theory marks, so this skill matters across the paper.
Why does tracing matter beyond the exam?
Tracing means carrying out code by hand, as the computer would. In a study across seven countries, Lister and colleagues (2004) tested first-year programming students on predicting what short pieces of code would do, and found many were weak at it. They argued that students with a fragile grasp of tracing lack a skill that problem-solving depends on. A later exam study by Lopez and colleagues (2008) found that performance on tracing tasks correlated with performance on code-writing tasks. So time spent tracing is not time taken away from learning to program.
The method: a trace table
Draw a table before you read the code twice. Give it one column per variable, one column for the loop counter or condition, and one column for anything printed. Then:
Write the starting value of every variable.
Go through the code one line at a time. Each time a value changes, write a new row. Never change a value in your head.
For each loop, write the condition you are checking and whether it is true or false.
Copy printed output exactly, including brackets, quotes, commas and the effect of end= or sep=.
Worked example from the sample paper
Question 26 of the 2026-27 sample paper:
code = "gLaD5"
result = []
i = 0
while i < len(code):
if code[i].isupper():
result.append(code[i].lower())
elif code[i].isdigit():
result.append(int(code[i]) * 2)
else:
result.append("#")
i += 1
print(result)The trace, one row per pass of the loop:
i = 0, character g: not upper case, not a digit, so append "#".
i = 1, character L: upper case, so append "l".
i = 2, character a: append "#".
i = 3, character D: append "d".
i = 4, character 5: a digit, so append int("5") * 2, which is the number 10, not the string "10".
i = 5: 5 < 5 is false, so the loop stops.
Output: ['#', 'l', '#', 'd', 10]. This matches the official marking scheme. Notice that 10 has no quotes, because it is an integer in the list.
A second example: when the list changes as you go
Here is a practice snippet of our own. Run it after you have traced it.
L = [3, 6, 2, 8]
total = 0
for i in range(1, len(L)):
if L[i] > L[i-1]:
total += L[i] - L[i-1]
else:
L[i] = L[i-1]
print(total, L)range(1, 4) gives i = 1, 2, 3. At i = 1, 6 > 3, so total becomes 3. At i = 2, 2 > 6 is false, so L[2] is replaced by 6. At i = 3, the comparison is 8 > 6, not 8 > 2, because the list has changed. total becomes 3 + 2 = 5. Output: 5 [3, 6, 6, 8].
A student who works from the original list writes 9, because 8 − 2 = 6 and 3 + 6 = 9. The trace table prevents this, since the row for i = 2 shows the new value.
Which mistakes cost marks most often?
range end. range(1, 4) stops at 3. Count the values before you start.
Division. / always gives a float, so 100 / 5 is 20.0. Question 10 in the sample paper, print(25+10013/5), prints 45.0, not 45. Our guide to operator precedence in Python works through it.
Negative slicing. For text = "Drone Expo", text[-2::-4] starts at "p" and steps back four places each time, giving peD.
Local variables. In question 16, a function assigns y = y + 2 without declaring y global, so Python raises an error instead of printing 10.
Output format. print(a, b) puts a space between values; a list prints with square brackets and quotes around strings.
For questions built on random numbers, see our post on possible outputs with the random module.
A practice routine
Three days a week, trace two snippets from past CBSE sample papers on paper, with a full trace table, then run them to check.
When your answer is wrong, find the exact row where your table and Python disagree, and write one line on why.
Once a week, write a five-line snippet that tests one trap from the list above and swap it with a friend.
When a trace goes wrong and you cannot see why, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer, which suits checking each row of your table.
Tonight, trace question 26 above without looking, then try the second snippet and run both to check.



