Indian Institute of Technology Madras
BS Degree Programme · Examination & Study Paper
Programming in Python
End Term · 31 Aug 2025 (Afternoon)
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.
- Q1MCQ3 Marks·Choose one option
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
- Q2MCQ3 Marks·Choose one option
Consider the below code snippet. def updated_val(d, key, val): if key in d: d[key] += val else: d[key] = val return d[key] my_dict = {'a': 2, 'b': 3} print( updated_val(my_dict, 'c', 4) + updated_val(my_dict, 'c', 2) + updated_val(my_dict, 'a', 2) ) What will be the output of the above code
- A6
- B14
- C10
- DRaises KeyError
- Q3MCQ3 Marks·Choose one option
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)
- Q4MCQ3 Marks·Choose one option
Consider the below output. 1 4 7 2 5 8 3 6 9 Select the code snippet that will generate the above output.
- Amatrix = [[i + (j+1)*3 for i in range(3)] for j in range(3)] for row in matrix: print(*row)
- Bmatrix = [[(i+1) + j*3 for i in range(3)] for j in range(3)] for row in matrix: print(*row)
- Cmatrix = [[(j+1) + i*3 for j in range(3)] for i in range(3)] for row in matrix: print(*row)
- Dmatrix = [[(i+1) + j*3 for j in range(3)] for i in range(3)] for row in matrix: print(*row)
- Q5MCQ3 Marks·Choose one option
Given the following code snippet, what will be the output? def update_records(records, updates): for key, value in updates.items(): if key in records and isinstance(records[key], dict): for sub_key, sub_val in value.items(): if sub_key in records[key]: records[key][sub_key] += sub_val else: records[key][sub_key] = sub_val else: records[key] = value return records data = { "A": {"x": 10, "y": 20}, "B": {"x": 5, "z": 15}, "C": 100 } updates = { "A": {"x": 5, "z": 5}, "B": {"y": 10}, "C": {"x": 10}, "D": {"w": 7} } result = update_records(data, updates) print(result)
- AA: {'x': 15, 'y': 20, 'z': 5} B: {'x': 5, 'z': 15, 'y': 10} C: {'x': 10} D: {'w': 7}
- BA: {'x': 15, 'y': 20, 'z': 5} B: {'x': 5, 'z': 15, 'y': 10} C: {'x': 110} D: {'w': 7}
- CA: {'x': 5, 'z': 5} B: {'y': 10} C: {'x': 10} D: {'w': 7}
- DA: {'x': 5, 'y': 20, 'z': 5} B: {'x': 5, 'z': 15, 'y': 10} C: 100 D: {'w': 7}
- Q6MCQ3 Marks·Choose one option
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
- Q7MSQ3 Marks·Choose all correct options
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
- Q8MSQ3 Marks·Choose all correct options
Consider the following snippet of code: filename = "test.txt" with open(filename, "w") as f: f.write("\n".join((f"Line{i}" for i in range(1,11)))) with open(filename, "r") as f: f.readline() f.readline() print(f.read(5)) f.readline() print(f.read(10)) f.seek(0) print(f.read(5)) f.readline() f.readline() print(f.read(5)) Select all that apply. Note • If a value is passed to _(f.read) , it reads that many number of characters from the current position of the file pointer. • The _(f.seek) method moves the position of the file pointer to the given number of chars after the start of the file.
- AThe first line of the output is _(Line3) .
- BThe second line of the output is _(Line5) .
- CThe second line of the output is _(Line4) .
- DThere are 5 lines in the output.
- EThe output contains empty lines.
- Q9MCQ3 Marks·Choose one option
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
- Q10NAT3 Marks·Write the number
Consider the below python code. lst = [] for i in range(5): lst.append(i) lst.append(i*10) lst[1:4] = [100] print(sum(lst)) What will be the output of the given code?
Your answer: - Q11NAT3 Marks·Write the number
Consider the below python code. def process_array(arr): total = 0 for i, row in enumerate(arr): total += row[i] return total array = [] for i in range(5): row = [] for j in range(i + 1): row.append((i + 1) * (j + 1)) array.append(row) print(process_array(array)) What will be the output of the given code?
Your answer: - Q12NAT3 Marks·Write the number
Consider the below python code. def procedure(child_dict, i): if i not in child_dict.keys(): return 1 ans = 1 for j in child_dict[i]: ans += procedure(child_dict, j) return ans child_dict = dict() child_dict[0] = [1,2] child_dict[1] = [3,4,5] child_dict[2] = [6,7,8] print(procedure(child_dict,0)) What will be the output of the given code?
Your answer: - Q13NAT3 Marks·Write the number
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: Read the passage below, then answer Q14–Q15
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 numberOn executing the given code, how many number of lines will be there in the file example_file.txt ?
Your answer:- Q15MCQ3 Marks·Choose one option
What will be printed if we call random_access_read(filename, seek_pos) immediately after running the given code?
- ALine 1 Line 2 Line 3 OVERWRITTEN_TEXT e 6
- BLine 3 OVERWRITTEN_TEXT e 6
- CLine 3 LinOVERWRITTEN_TEXT
- DLine 1 Line 2 Line 3 Line 4 Line 5 Line 6
Read the passage below, then answer Q16–Q18
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.
Q16MCQ2 Marks·Choose one optionWhat 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]
- Q17NAT2 Marks·Write the number
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: - Q18MSQ2 Marks·Choose all correct options
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}