Indian Institute of Technology Madras
BS Degree Programme · Examination & Study Paper
Programming in Python
Most repeated questions
Examination Instructions & Guidelines
- Solve on notebook/paper first before checking solutions.
- MCQ: Exactly 1 option is correct. Negative marking applies.
- MSQ: One or more options correct. Select all to score.
- NAT: Numerical response. No negative marking.
- Scientific calculator allowed for all calculations.
- Official answer key is sealed at the end of this sheet.
Type 1 · asked 9 times · Recursion
Trace a function on a target and a list: first output, True or False
Read the passage below, then answer Q1–Q3
Q1MCQ1.5 Marks·Choose one optionEnd Term · 22 Dec 2024 (AN)Key ↓
- A

- B

- C

- D

- A
- Q2MSQ1.5 Marks·Choose all correct optionsEnd Term · 22 Dec 2024 (AN)Key ↓

- A

- B

- C

- D

- A
- Q3MSQ1 Mark·Choose all correct optionsEnd Term · 22 Dec 2024 (AN)Key ↓

- A

- B

- C

- D

- A
Read the passage below, then answer Q4–Q6
Q4MCQ1.5 Marks·Choose one optionEnd Term · 22 Dec 2024 (FN)Key ↓
- A

- B

- C

- D

- A
- Q5MSQ1.5 Marks·Choose all correct optionsEnd Term · 22 Dec 2024 (FN)Key ↓

- A

- B

- C

- D

- A
- Q6MCQ1 Mark·Choose one optionEnd Term · 22 Dec 2024 (FN)Key ↓

- A

- B

- C

- D

- A
Read the passage below, then answer Q7–Q9
Q7MCQ1.5 Marks·Choose one optionEnd Term · 13 Apr 2025 (FN)Key ↓
- A

- B

- C

- D

- A
- Q8MSQ1.5 Marks·Choose all correct optionsEnd Term · 13 Apr 2025 (FN)Key ↓

- A

- B

- C

- D

- A
- Q9MSQ1 Mark·Choose all correct optionsEnd Term · 13 Apr 2025 (FN)Key ↓

- A

- B

- C

- D

- A
Type 2 · asked 6 times · File handling
Count the lines in a file after a series of writes
- Q10MCQ3 Marks·Choose one optionEnd Term · 13 Apr 2025 (AN)Key ↓

- A2
- B3
- C4
- D6
- Q11NAT3 Marks·Write the numberEnd Term · 13 Apr 2025 (AN)Key ↓
Your answer: - Q12MCQ3 Marks·Choose one optionEnd Term · 13 Apr 2025 (FN)Key ↓

- A2
- B3
- C5
- DIt will give an error
- Q13NAT3 Marks·Write the numberEnd Term · 13 Apr 2025 (FN)Key ↓
Your answer: Read the passage below, then answer Q14
Consider the below python code.
def create_and_write_file(filename, num_lines):
with open(filename, "w") as f:
for i in range(1, num_lines + 1):
f.write(f"Line {i}\n")
def random_access_read(filename, seek_pos):
with open(filename, "r") as f:
first_chars = f.read(5)
f.seek(seek_pos)
line = f.readline()
rest = f.readlines()
for line in rest:
print(line.strip())
def overwrite_some_text(filename, overwrite_pos, text):
with open(filename, "r+") as f:
f.seek(overwrite_pos)
original = f.readline()
f.seek(overwrite_pos)
f.write(text + "\n")
filename = "example_file.txt"
num_lines = 6
seek_pos = 10
overwrite_pos = 21
overwrite_text = "OVERWRITTEN_TEXT"
create_and_write_file(filename, num_lines)
overwrite_some_text(filename, overwrite_pos, overwrite_text)
Based on the given code snippet answer the given subquestions.
Q14NAT2 Marks·Write the numberEnd Term · 31 Aug 2025 (AN)Key ↓On executing the given code, how many number of lines will be there in the file example_file.txt ?
Your answer:- Q15NAT2 Marks·Write the numberEnd Term · 31 Aug 2025 (FN)Key ↓
Consider the following Python code: with open("data.txt", "w") as f: for i in range(3): for j in range(i + 1): f.write(f"Line {i}-{j}" + ("\n" * (j + 1))) with open("data.txt", "r") as f: count = len(f.readlines()) print(count) What is the output of the given code?
Your answer:
Type 3 · asked 5 times · Tuples & sets
Compute the result of set operations (union, difference, intersection)
- Q16NAT3 Marks·Write the numberEnd Term · 13 Apr 2025 (AN)Key ↓
Your answer: Read the passage below, then answer Q17–Q19
Consider the below python code.
def categorize_numbers(numbers):
categories = {
"cat_1": set(),
"cat_2": set(),
"cat_3": set()
}
def check(n):
if n % 5 == 0:
return True
return False
for num in numbers:
if num % 2 == 0:
categories["cat_1"].add(num)
else:
categories["cat_2"].add(num)
if check(num):
categories["cat_3"].add(num)
return categories
result = categorize_numbers([
2, 3, 4, 2, 5, 6, 3, 2, 7, 8,
9, 10, 5, 4, 7, 8, 3, 10, 2
])
Based on the given code snippet answer the given subquestions.
Q17MCQ2 Marks·Choose one optionEnd Term · 31 Aug 2025 (AN)Key ↓What will be the value of the following expression after running the given code snippet? sorted(result["cat_1"] & result["cat_3"])
- A[10]
- B[5]
- C[2, 4, 5, 6, 8, 10]
- D[5, 10]
- Q18NAT2 Marks·Write the numberEnd Term · 31 Aug 2025 (AN)Key ↓
What will be the value of the following expression after running the given code snippet? len(result["cat_1"] | result["cat_2"] | result["cat_3"])
Your answer: - Q19MSQ2 Marks·Choose all correct optionsEnd Term · 31 Aug 2025 (AN)Key ↓
If result = categorize_numbers([1, 2, 10, 15, 20]) , which of the following expression(s) would be evaluated as _(True) ?
- Aresult == { 'cat_1': {2, 10, 20}, 'cat_2': {1, 15}, 'cat_3': {10, 20, 15} }
- Blen(result["cat_1"] & result["cat_3"]) == 2
- Clen(result["cat_1"] & result["cat_2"]) == 5
- D(result["cat_1"] & result["cat_2"] | result["cat_3"]) == {10,20,15}
Useful Reference Data for this Paper
Useful Information
NAT → integer
For all NAT questions in this exam, the answer will always be an integer and not a float value. If the answer to a question is 18, then just enter that value. Do not enter 18.0
Q20NAT3 Marks·Write the numberQuiz 1 · 26 Oct 2025 (AN)Key ↓Consider the following Python code: P = {1, 2, 3, 4} Q = {3, 4, 5, 6} R = {4, 5, 6, 7} S = {2, 4, 6, 8} result = ((P | Q) - R) & S print(result) How many elements will be present in result?
Your answer:
Type 4 · asked 4 times · Conditionals (if-elif-else)
Trace a nested if-elif-else with a flag variable for a given input
Type 5 · asked 4 times · Exception handling
Trace try/except/finally to find the final result value
- Q25NAT3 Marks·Write the numberEnd Term · 22 Dec 2024 (AN)Key ↓
Your answer: - Q26NAT3 Marks·Write the numberEnd Term · 22 Dec 2024 (FN)Key ↓
Your answer: - Q27MCQ3 Marks·Choose one optionEnd Term · 31 Aug 2025 (AN)Key ↓
Given the following code snippet, what will be the output? def get_square(items, i): return items[i]**2 def safe_get_square(items,i): try: return get_square(items,i) except IndexError: return 0 except: return -1 nums = [5, -2, 3] print(safe_get_square(nums , 1)) print(safe_get_square(nums , -1)) print(safe_get_square([] , -1)) print(safe_get_square(None , 1))
- A25 0 -1 -1
- B4 4 -1 -1
- C4 9 -1 -1
- D4 9 0 -1
- Q28MSQ3 Marks·Choose all correct optionsEnd Term · 31 Aug 2025 (FN)Key ↓
Which of the following statements about the exception handling behavior are TRUE? Select ALL that apply. def safe_divide(x, y): result = [] try: result.append("Trying division") result.append(x // y) except ZeroDivisionError: result.append("Division by zero") else: result.append("No exception occurred") finally: result.append("Cleanup done") return result output = safe_divide(10, 0) print(output)
- AThe output will contain the string _("Trying division")
- BThe string _("Division by zero") will appear in the output
- CThe division result will be appended to the output
- DThe string _("Cleanup done") will always appear regardless of exceptions
Type 6 · asked 4 times · Comprehensions, map & lambda
Pick snippets that build a list of squares/cubes of even or odd numbers
- Q29MSQ3 Marks·Choose all correct optionsEnd Term · 22 Dec 2024 (AN)Key ↓

- A

- B

- C

- D

- E

- A
- Q30MSQ3 Marks·Choose all correct optionsEnd Term · 22 Dec 2024 (FN)Key ↓

- A

- B

- C

- D

- E

- A
- Q31MSQ3 Marks·Choose all correct optionsEnd Term · 13 Apr 2025 (AN)Key ↓

- A

- B

- C

- D

- A
- Q32MSQ3 Marks·Choose all correct optionsEnd Term · 31 Aug 2025 (FN)Key ↓
Consider the use of the _(map()) function in the following code snippets. Select all options that has the value _([4, 9, 16, 25]) in the variable _(squares) after execution.
- Anums = [2, 3, 4, 5] squares = list(map(lambda x: x**2, nums))
- Bnums = [4, 9, 16, 25] squares = map(int, nums)
- Cnums = [2, 3, 4, 5] def square(n): return n * n squares = list(map(square, nums))
- Dnums = [2, 3, 4, 5] squares = [] for x in nums: squares.append(x ** 2)
- Enums = [2, 3, 4, 5] squares = list(map(pow, nums, [2]*4))
Type 7 · asked 4 times · Functions & scope
Trace an algorithm on a list and a target value (return value or None)
Read the passage below, then answer Q33–Q34
Q33MCQ1.5 Marks·Choose one optionEnd Term · 22 Dec 2024 (AN)Key ↓
- A

- B

- C

- D

- A
- Q34MCQ1.5 Marks·Choose one optionEnd Term · 22 Dec 2024 (AN)Key ↓

- A

- B

- C

- D

- A
Read the passage below, then answer Q35–Q36
Q35MCQ2 Marks·Choose one optionEnd Term · 22 Dec 2024 (FN)Key ↓
- A

- B

- C

- D

- A
- Q36MCQ2 Marks·Choose one optionEnd Term · 22 Dec 2024 (FN)Key ↓

- A

- B

- C

- D

- A
Type 8 · asked 4 times · Classes, objects & inheritance
Predict the output when a subclass overrides a parent method
- Q37MCQ3 Marks·Choose one optionEnd Term · 22 Dec 2024 (FN)Key ↓

- A

- B

- C

- D

- A
- Q38MCQ3 Marks·Choose one optionEnd Term · 31 Aug 2025 (AN)Key ↓
Consider the following snippet of code: class Shape: def __init__(self): pass def area(self): return "Area not defined" def describe(self): return f"I am a shape. Area: {self.area()}" class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height class Circle(Shape): def __init__(self, radius): self.radius = radius r = Rectangle(4, 5) c = Circle(3) print(r.describe()) print(c.describe()) What will be the output?
- AI am a shape. Area: 20 I am a shape. Area: Area not defined
- BI am a shape. Area: Area not defined I am a shape. Area: Area not defined
- CAttributeError: 'Rectangle' object has no attribute 'describe'
- DNameError: describe is not defined
- EI am a shape. Area: 20 I am a shape. Area: 28.26
- Q39NAT3 Marks·Write the numberEnd Term · 31 Aug 2025 (AN)Key ↓
Consider the below python code. class A: def __init__(self): self.value = 5 def add(self, num): self.value += num return self.value class B(A): def __init__(self): super().__init__() self.value *= 2 def add(self, num): return super().add(num * 2) obj = B() result = obj.add(3) print(result) What will be the output of the given code?
Your answer: - Q40MCQ3 Marks·Choose one optionEnd Term · 31 Aug 2025 (FN)Key ↓
Consider the following Python code: class Employee: def __init__(self, name): self.name = name self.role = "Employee" def show_details(self): print(f"{self.name} works as {self.role}") class Developer(Employee): def __init__(self, name): super().__init__(name) self.role = "Developer" def show_details(self): print(f"{self.name} writes code as a {self.role}") super().show_details() class Manager(Employee): def __init__(self, name): super().__init__(name) self.role = "Manager" def show_details(self): print(f"{self.name} manages tasks as a {self.role}") super().show_details() employees = [ Developer("Ananya"), Manager("Rajeev"), Developer("Kiran") ] for emp in employees: emp.show_details() What will be the output of the code above?
- AAnanya writes code as a Developer Ananya works as Developer Rajeev manages tasks as a Manager Rajeev works as Manager Kiran writes code as a Developer Kiran works as Developer
- BAnanya writes code as a Developer Ananya works as Employee Rajeev manages tasks as a Manager Rajeev works as Employee Kiran writes code as a Developer Kiran works as Employee
- CAnanya works as Developer Rajeev works as Manager Kiran works as Developer
- DDeveloper Employee Manager Employee Developer Employee
Type 9 · asked 4 times · Tuples & sets
Trace tuple concatenation, slicing and membership
- Q41MSQ3 Marks·Choose all correct optionsEnd Term · 22 Dec 2024 (AN)Key ↓

- A

- B

- C

- D

- A
- Q42MCQ3 Marks·Choose one optionEnd Term · 31 Aug 2025 (AN)Key ↓
Given the following code snippet, what will be the output? a = (1, 2) b = (3, 4) c = ((5, 6),) d = a + b + c e = d[2:] f = d[:2] + (e[2],) print(f)
- A(3, 4, (5, 6))
- B(3, 4, 5, 6)
- C(1, 2, (5, 6))
- D(1, 2, 5, 6)
- Q43MSQ3 Marks·Choose all correct optionsEnd Term · 31 Aug 2025 (AN)Key ↓
Consider the following snippet of code: t1 = (1, 2, 3) t2 = (4, 5, 6) t3 = t1 + t2 t4 = (t1, t2) print(t3[2:5]) print(len(t4)) print(5 in t2) try: t1[0] = 10 except: t1 = t3[0] print(t1) Select all that apply.
- AThe First Line of output is _((3, 4, 5, 6))
- BThe First Line of output is _((3, 4, 5))
- CThe Second Line of output is _2
- DThe Third Line of output is _(True)
- EThe Fourth Line of output is _3
- Q44NAT3 Marks·Write the numberEnd Term · 31 Aug 2025 (FN)Key ↓
Consider the following Python code: a1 = (3, 6, 9, 12, 15, 18, 21, 24) a2 = a1[2:7] a3 = a2[::-1] a4 = tuple(x for x in a3 if x % 6 != 0) a5 = a4[1::2] ans = sum(a5) + len(a4) print(ans) What is the output of the given code?
Your answer:
Type 10 · asked 3 times · Classes, objects & inheritance
Output after updating and appending to a list attribute in a class
Type 11 · asked 3 times · Lists, slicing & matrices
Build a list by adding slices in a while loop
Type 12 · asked 3 times · Classes, objects & inheritance
Count instances using a class-level counter variable
Read the passage below, then answer Q51
Based on the below data answer the given subquestions.
Q51MCQ1.5 Marks·Choose one optionEnd Term · 13 Apr 2025 (AN)Key ↓
- A1
- B2
- C3
- DCannot be determined
Read the passage below, then answer Q52

Based on the above data, answer the given subquestions.
Q52NAT1.5 Marks·Write the numberEnd Term · 13 Apr 2025 (FN)Key ↓If the below main code is executed after the class definition, what will be the output?
Your answer:Read the passage below, then answer Q53
Consider the following Python code and answer the sub-questions:
class Vehicle:
total_vehicles = 0
all_models = []
def __init__(self, model):
self.model = model
Vehicle.total_vehicles += 1
Vehicle.all_models.append(model)
def get_info(self):
return "Model: " + self.model
def get_total():
return Vehicle.total_vehicles
def count_models_starting_with(letter):
count = 0
for m in Vehicle.all_models:
if m.startswith(letter):
count += 1
return count
class ElectricVehicle(Vehicle):
ev_count = 0
def __init__(self, model, battery):
Vehicle.__init__(self, model)
self.battery = battery
ElectricVehicle.ev_count += 1
if battery < 40:
self.status = "Low"
else:
self.status = "OK"
def get_info(self):
return (
f"{Vehicle.get_info(self)}, "
f"Battery: {self.battery})%, "
f"Status: {self.status}"
)
class PetrolVehicle(Vehicle):
pv_count = 0
def __init__(self, model, fuel):
Vehicle.__init__(self, model)
self.fuel = fuel
PetrolVehicle.pv_count += 1
def get_info(self):
return f"{Vehicle.get_info(self)}, Fuel: {self.fuel} L"
fleet = [
ElectricVehicle("Tesla Model 3", 75),
ElectricVehicle("Mahindra e2o", 35),
PetrolVehicle("Hyundai i10", 20),
PetrolVehicle("Swift", 12),
ElectricVehicle("Tata Tigor EV", 80),
Vehicle("Generic Cycle")
]
low_battery_models = []
for v in fleet:
if isinstance(v, ElectricVehicle):
if v.status == "Low":
low_battery_models.append(v.model)
count = 0
for v in fleet:
if isinstance(v, Vehicle):
count += 1
print(count)
Q53NAT2 Marks·Write the numberEnd Term · 31 Aug 2025 (FN)Key ↓What will the output of the given code?
Your answer:
Type 13 · asked 3 times · Loops: while, for & nested loops
Count lines printed by nested loops
- Q54NAT3 Marks·Write the numberEnd Term · 13 Apr 2025 (FN)Key ↓
Your answer: Useful Reference Data for this Paper
Useful Information
NAT → integer
For all NAT questions in this exam, the answer will always be an integer and not a float value. If the answer to a question is 18, then just enter that value. Do not enter 18.0
Q55NAT4 Marks·Write the numberQuiz 1 · 26 Oct 2025 (AN)Key ↓Consider the following Python code: for i in range(1, 6): for j in range(1, 6): for k in range(1, 2): if i == j: print("-") else: print("*", end=" ") print("+") How many lines will be printed when this code is executed?
Your answer:- Q56NAT3 Marks·Write the numberQuiz 1 · 26 Oct 2025 (AN)Key ↓
Consider the following Python code: for i in range(1, 5): for j in range(1, 5): if j == 3: break if i == 2: continue print(i, j) How many lines will be printed when this code is executed?
Your answer:
Type 14 · asked 3 times · Lists, slicing & matrices
Trace a function that transforms a nested list (matrix)
- Q57NAT3 Marks·Write the numberEnd Term · 13 Apr 2025 (FN)Key ↓
Your answer: Useful Reference Data for this Paper
Useful Information
NAT → integer
For all NAT questions in this exam, the answer will always be an integer and not a float value. If the answer to a question is 18, then just enter that value. Do not enter 18.0
Read the passage below, then answer Q58–Q59
Consider the following code:
def flipper(matrix):
transformed = []
for row in matrix:
new_row = []
for element in row:
if element < 0:
new_row.append(0)
elif element % 2 == 0:
new_row.append(element * 2)
else:
new_row.append(element * 3)
transformed.append(new_row)
result = [
val
for sub in transformed
for val in sub
if val > 5
]
return transformed, result
matrix = [[1, -2, 3], [4, 5, -6], [7, 8]]
out1, out2 = flipper(matrix)
print(out1)
print(out2)
Answer the subquestions based on the given data.
Q58MCQ4 Marks·Choose one optionQuiz 1 · 26 Oct 2025 (AN)Key ↓What is the value of _(out1) after executing the code?
- A[[1, -4, 9], [8, 15, -12], [21, 16]]
- B[[3, 0, 9], [8, 15, 0], [21, 16]]
- C[[3, 0, 9], [4, 15, 0], [21, 16]]
- D[[1, 0, 3], [8, 15, 0], [21, 16]]
- Q59MCQ4 Marks·Choose one optionQuiz 1 · 26 Oct 2025 (AN)Key ↓
What is the value of _(out2) after executing the code?
- A[3, 9, 8, 15, 21, 16]
- B[9, 8, 15, 21, 16]
- C[8, 15, 21, 16]
- D[3, 0, 9, 8, 15, 0, 21, 16]
Type 15 · asked 2 times · Conditionals (if-elif-else)
Pick the snippet(s) that map a number range to the right category
Type 16 · asked 2 times · Loops: while, for & nested loops
Find the best pair from a list using two nested loops
Type 17 · asked 2 times · File handling
What a file holds after writing in 'w' mode (overwrite)
Type 18 · asked 2 times · File handling
Read a CSV file and compute an average for filtered rows
Type 19 · asked 2 times · Expressions, types & strings
Find the length of a string built from slices
Type 20 · asked 2 times · Loops: while, for & nested loops
Count while-loop steps when a value is divided or multiplied
Type 21 · asked 2 times · Expressions, types & strings
Describe what a character-by-character string function does
- Q72MSQ3 Marks·Choose all correct optionsEnd Term · 22 Dec 2024 (AN)Key ↓

- A

- B

- C

- D

- A
- Q73MCQ3 Marks·Choose one optionEnd Term · 13 Apr 2025 (AN)Key ↓

- AIt converts all vowels to uppercase and consonants to lowercase while leaving digits unchanged.
- BIt reverses the order of characters in the string while converting all letters to lowercase.
- CIt removes all non-alphabetic characters and alternates uppercase and lowercase letters in the remaining string.
- DIt alternates the case of alphabetical characters based on their position, starting with uppercase at index 0, leaving non-alphabetic characters unchanged.
Type 22 · asked 2 times · Tuples & sets
Trace a while loop that reassigns tuples or sets; which one has X
Type 23 · asked 2 times · Recursion
Trace recursion that takes the first and last items (lst[1:-1])
Type 24 · asked 2 times · Dictionaries
Use a character-frequency dict to find which strings return True
Type 25 · asked 2 times · Expressions, types & strings
Evaluate truthiness of None, 0 and empty string in conditions
- Q80MCQ3 Marks·Choose one optionEnd Term · 31 Aug 2025 (AN)Key ↓
Given the following code snippet, what will be the output? if None: print("None is True") elif 0: print("0 is True") elif "": print("Empty string is True") else: print("All False")
- ANone is True
- B0 is True
- CEmpty string is True
- DAll False
Useful Reference Data for this Paper
Useful Information
NAT → integer
For all NAT questions in this exam, the answer will always be an integer and not a float value. If the answer to a question is 18, then just enter that value. Do not enter 18.0
Q81MCQ3 Marks·Choose one optionQuiz 1 · 26 Oct 2025 (AN)Key ↓Consider the following Python code. What will be the output? x, y, z = "", "python", 0 print((x or y) and (z or len(y)))
- A6
- B""
- C"python"
- D0
- ETrue
Official KeyMaster Answer Key & Explanations
Official answer keys from IIT Madras BS past papers. Click any question to review.
Reveal KeyCollapse Key
Master Answer Key & Explanations
Official answer keys from IIT Madras BS past papers. Click any question to review.










































