12th Computer Science Python Output Questions: Traced Line by Line
Output questions are where many students lose easy marks in Class 12 Computer Science. The code is always short, but one missed detail, such as a negative index or where a break sits, changes the answer. The fix is a habit: trace the code on paper, writing down the value of each variable after every line, before you look at the options.
Below are the patterns the book back questions use, each traced step by step.
1. Reversing a string with a slice
str1 = "TamilNadu"
print(str1[::-1])
A slice is written [start:stop:stride]. Leaving start and stop empty covers the whole string, and a stride of -1 walks backwards. Reading T-a-m-i-l-N-a-d-u from the end gives udaNlimaT. The capital N stays capital, and that detail is exactly what the wrong options change.
Two related facts: str1[0:5] gives Tamil, because the stop index is not included, and str1[-1] gives the last character, u.
2. Trying to change a string
str1 = "Chennai Schools"
str1[7] = "-"
Strings are immutable. You cannot assign to a single character, so this raises a TypeError. If you wanted a changed string, you would have to build a new one.
3. A while loop with break
i = 1
while True:
if i % 3 == 0:
break
print(i, end=' ')
i += 1
| Pass | i | i % 3 == 0? | Action |
|---|---|---|---|
| 1 | 1 | No | prints 1, i becomes 2 |
| 2 | 2 | No | prints 2, i becomes 3 |
| 3 | 3 | Yes | break, nothing printed |
Output: 1 2. The check happens before the print, so 3 is never printed.
4. A while loop that runs once
T = 1
while T:
print(True)
break
Any non-zero number counts as true, so the loop starts. It prints True, then break ends the loop immediately. The output is a single line.
5. Negative indexing in a list
list1 = [2, 4, 6, 8, 10]
print(list1[-2])
Negative indexes count from the end: -1 is 10 and -2 is 8. For the same list, len(list1) is 5.
6. Changing and appending list elements
List = [10, 20, 30, 40, 50]
List[2] = 35 # [10, 20, 35, 40, 50]
List.append(32) # [10, 20, 35, 40, 50, 32]
Lists are mutable. Index 2 is the third element, so 30 is replaced by 35. append() adds one element at the end; extend() would add several.
7. A list comprehension
S = [x**2 for x in range(5)]
print(S)
range(5) gives 0, 1, 2, 3 and 4. It stops before 5. Squaring each gives [0, 1, 4, 9, 16]. The common mistake is to include 25.
8. Set operations
setA = {3, 6, 9}
setB = {1, 3, 9}
print(setA | setB)
| Expression | Operation | Result |
|---|---|---|
setA | setB | Union | {1, 3, 6, 9} |
setA & setB | Intersection | {3, 9} |
setA - setB | Difference | {6} |
setA ^ setB | Symmetric difference | {1, 6} |
A union keeps each value once, so duplicates disappear. Sets are unordered, so don't rely on the order in which elements are displayed.
9. A constructor that prints
class Student:
def __init__(self, name):
self.name = name
print(self.name)
S = Student("Tamil")
Creating the object calls __init__() automatically, which stores the name and prints it. Output: Tamil. No separate method call is needed.
10. Skipping a row in a CSV file
import csv
d = csv.reader(open('city.csv'))
next(d)
for row in d:
print(row)
If city.csv contains the lines chennai,mylapore and mumbai,andheri, next(d) consumes the first row, so the loop prints only ['mumbai', 'andheri']. Each row comes back as a list of strings.
A method for any new output question
- Write every variable as a column heading.
- Go line by line and update the values. For loops, add a row for every pass.
- Mark exactly when output happens, and whether
end=' 'keeps it on one line. - Check boundaries: where slices and ranges stop, negative indexes, and whether
breakcomes before or after the print.
Practise with the solved sets for Control Structures, Strings and String Manipulation, Lists, Tuples, Sets and Dictionary and Python Classes and Objects, then take the timed tests on the Class 12 Computer Science section.