uiz Space

May 2025 term · Programming in Python · BSCS1002

Programming in Python End Term: 31 August 2025, Set QDF3 (May 2025 term)

The IIT Madras BS Programming in Python (Python) End Term paper sat on 31 Aug 2025, in the May 2025 term, set QDF3: 18 questions for 50 marks in 180 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.

Questions
18
Marks
50
Duration
180 min
MCQ
9
MSQ
3
Numerical
6

Updated

Official paper: IIT M FOUNDATION AN EXAM QDF3 31 Aug 2025 · No negative marking.

Question 1

+3 marksOne correct 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")

  1. A

    None is True

  2. B

    0 is True

  3. C

    Empty string is True

  4. D

    All False

Show answer

Correct answer

  • D

    All False

Question 2

+3 marksOne correct 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

  1. A

    6

  2. B

    14

  3. C

    10

  4. D

    Raises KeyError

Show answer

Correct answer

  • B

    14

Question 3

+3 marksOne correct 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)

  1. A

    (3, 4, (5, 6))

  2. B

    (3, 4, 5, 6)

  3. C

    (1, 2, (5, 6))

  4. D

    (1, 2, 5, 6)

Show answer

Correct answer

  • C

    (1, 2, (5, 6))

Question 4

+3 marksOne correct option

Consider the below output.
1 4 7
2 5 8
3 6 9
Select the code snippet that will generate the above output.

  1. A

    matrix = [[i + (j+1)*3 for i in range(3)] for j in range(3)]
    for row in matrix:
    print(*row)

  2. B

    matrix = [[(i+1) + j*3 for i in range(3)] for j in range(3)]
    for row in matrix:
    print(*row)

  3. C

    matrix = [[(j+1) + i*3 for j in range(3)] for i in range(3)]
    for row in matrix:
    print(*row)

  4. D

    matrix = [[(i+1) + j*3 for j in range(3)] for i in range(3)]
    for row in matrix:
    print(*row)

Show answer

Correct answer

  • D

    matrix = [[(i+1) + j*3 for j in range(3)] for i in range(3)]
    for row in matrix:
    print(*row)

Question 5

+3 marksOne correct 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)

  1. A

    A: {'x': 15, 'y': 20, 'z': 5}
    B: {'x': 5, 'z': 15, 'y': 10}
    C: {'x': 10}
    D: {'w': 7}

  2. B

    A: {'x': 15, 'y': 20, 'z': 5}
    B: {'x': 5, 'z': 15, 'y': 10}
    C: {'x': 110}
    D: {'w': 7}

  3. C

    A: {'x': 5, 'z': 5}
    B: {'y': 10}
    C: {'x': 10}
    D: {'w': 7}

  4. D

    A: {'x': 5, 'y': 20, 'z': 5}
    B: {'x': 5, 'z': 15, 'y': 10}
    C: 100
    D: {'w': 7}

Show answer

Correct answer

  • A

    A: {'x': 15, 'y': 20, 'z': 5}
    B: {'x': 5, 'z': 15, 'y': 10}
    C: {'x': 10}
    D: {'w': 7}

Question 6

+3 marksOne correct 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))

  1. A

    25
    0
    -1
    -1

  2. B

    4
    4
    -1
    -1

  3. C

    4
    9
    -1
    -1

  4. D

    4
    9
    0
    -1

Show answer

Correct answer

  • D

    4
    9
    0
    -1

Question 7

+3 marksOne or more 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.

Select all that apply.

  1. A

    The First Line of output is (3, 4, 5, 6)

  2. B

    The First Line of output is (3, 4, 5)

  3. C

    The Second Line of output is 2

  4. D

    The Third Line of output is True

  5. E

    The Fourth Line of output is 3

Show answer

Correct answers

  • B

    The First Line of output is (3, 4, 5)

  • C

    The Second Line of output is 2

  • D

    The Third Line of output is True

Question 8

+3 marksOne or more 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.

Select all that apply.

  1. A

    The first line of the output is Line3 .

  2. B

    The second line of the output is Line5 .

  3. C

    The second line of the output is Line4 .

  4. D

    There are 5 lines in the output.

  5. E

    The output contains empty lines.

Show answer

Correct answers

  • A

    The first line of the output is Line3 .

  • C

    The second line of the output is Line4 .

  • D

    There are 5 lines in the output.

Question 9

+3 marksOne correct 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?

  1. A

    I am a shape. Area: 20
    I am a shape. Area: Area not defined

  2. B

    I am a shape. Area: Area not defined
    I am a shape. Area: Area not defined

  3. C

    AttributeError: 'Rectangle' object has no attribute 'describe'

  4. D

    NameError: describe is not defined

  5. E

    I am a shape. Area: 20
    I am a shape. Area: 28.26

Show answer

Correct answer

  • A

    I am a shape. Area: 20
    I am a shape. Area: Area not defined

Question 10

+3 marksNumerical answer

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?

Show answer

Correct answer: 199

Question 11

+3 marksNumerical answer

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?

Show answer

Correct answer: 55

Question 12

+3 marksNumerical answer

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?

Show answer

Correct answer: 9

Question 13

+3 marksNumerical answer

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?

Show answer

Correct answer: 16

Question 14

+2 marksNumerical answer

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.

On executing the given code, how many number of lines will be there in the file example_file.txt ?

Show answer

Correct answer: 5

Question 15

+3 marksOne correct option

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.

What will be printed if we call random_access_read(filename, seek_pos) immediately after running the given code?

  1. A

    Line 1
    Line 2
    Line 3
    OVERWRITTEN_TEXT
    e 6

  2. B

    Line 3
    OVERWRITTEN_TEXT
    e 6

  3. C

    Line 3
    LinOVERWRITTEN_TEXT

  4. D

    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    Line 6

Show answer

Correct answer

  • B

    Line 3
    OVERWRITTEN_TEXT
    e 6

Question 16

+2 marksOne correct option

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.

What will be the value of the following expression after running the given code snippet?
sorted(result["cat_1"] & result["cat_3"])

  1. A

    [10]

  2. B

    [5]

  3. C

    [2, 4, 5, 6, 8, 10]

  4. D

    [5, 10]

Show answer

Correct answer

  • A

    [10]

Question 17

+2 marksNumerical answer

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.

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"])

Show answer

Correct answer: 9

Question 18

+2 marksOne or more correct options

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.

If result = categorize_numbers([1, 2, 10, 15, 20]) , which of the following expression(s) would be evaluated as True ?

Select all that apply.

  1. A

    result == {
    'cat_1': {2, 10, 20},
    'cat_2': {1, 15},
    'cat_3': {10, 20, 15}
    }

  2. B

    len(result["cat_1"] & result["cat_3"]) == 2

  3. C

    len(result["cat_1"] & result["cat_2"]) == 5

  4. D

    (result["cat_1"] & result["cat_2"] | result["cat_3"]) == {10,20,15}

Show answer

Correct answers

  • A

    result == {
    'cat_1': {2, 10, 20},
    'cat_2': {1, 15},
    'cat_3': {10, 20, 15}
    }

  • B

    len(result["cat_1"] & result["cat_3"]) == 2

  • D

    (result["cat_1"] & result["cat_2"] | result["cat_3"]) == {10,20,15}