uiz Space

September 2025 term · Programming in Python · BSCS1002

Programming in Python End Term: 21 December 2025 (September 2025 term)

The IIT Madras BS Programming in Python (Python) End Term paper sat on 21 Dec 2025, in the September 2025 term: 20 questions for 52 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
20
Marks
52
Duration
180 min
MCQ
8
MSQ
3
Written
9

Updated

Official paper: Intro to Python 18 Dec 25 · No negative marking.

Question 1

+3 marksOne correct option

Consider the following functions: def p(n): if n < 10: return 1 return 1 + p(n // 10) def q(n): if n < 10: return n return n % 10 + q(n // 10) def r(n): if n <= 1: return 1 return n * r(n - 1) Which of the following function calls returns the number of digits in 50!, where n! is the factorial of n?

  1. A

    p(q(50))

  2. B

    q(r(50))

  3. C

    p(r(50))

  4. D

    r(q(50))

  5. E

    r(p(50))

Show answer

Correct answer

  • C

    p(r(50))

Question 2

+3 marksOne correct option

Consider the following code snippet that writes student details to a CSV file named "employees.csv": def save_data(data): f = open('employees.csv', 'w') f.write('Name,Salary\n') for i in range(len(data)): emp, sal = data[i] row = f'{emp},{sal}' if i != len(data) - 1: row = row + '\n' f.write(row) f.close() data = [ ('Alice', 75000), ('Bob', 64000), ('Charlie', 82000), ('Diana', 71000), ('Eve', 69000) ] If the save_data(data) function is called with the list data as shown above, what will be the contents of the file "employees.csv"?

  1. A

    Name,Salary Alice,75000 Bob,64000 Charlie,82000 Diana,71000 Eve,69000

  2. B

    Alice,75000 Bob,64000 Charlie,82000 Diana,71000 Eve,69000

  3. C

    Salary,Name Alice,75000 Bob,64000 Charlie,82000 Diana,71000 Eve,69000

  4. D

    Name Salary Alice,75000 Bob,64000 Charlie,82000 Diana,71000 Eve,69000

  5. E

    Salary,Name 75000,Alice 64000,Bob 82000,Charlie 71000,Diana 69000,Eve

Show answer

Correct answer

  • A

    Name,Salary Alice,75000 Bob,64000 Charlie,82000 Diana,71000 Eve,69000

Question 3

+3 marksOne correct option

What will be the output of the following Python code? string1 = 'database' string2 = 'baseball' L = [] for i in range(len(string1)): for j in range(len(string2)): if string1[i] == string2[j]: L.append(string1[i]) break else: continue print(L)

  1. A

    ['a', 'a', 'b', 'a', 's', 'e']

  2. B

    ['a', 't', 'a', 'b', 'a', 's', 'e']

  3. C

    ['a', 'b', 'a', 's', 'e']

  4. D

    ['a', 'a', 'b', 's', 'e']

Show answer

Correct answer

  • A

    ['a', 'a', 'b', 'a', 's', 'e']

Question 4

+3 marksOne correct option

What is the output of the following snippet of code? sentence = 'python,is,fun,and,learning,python,is,great' D = dict() for word in sentence.split(','): for char in word: if char not in D: D[char] = 0 D[char] += 1 mchar, mval = '', 0 alpha = 'abcdefghijklmnopqrstuvwxyz' for char in alpha: if char not in D: continue if D[char] >= mval: mval = D[char] mchar = char print(mchar)

  1. A

    n

  2. B

    o

  3. C

    i

  4. D

    p

Show answer

Correct answer

  • A

    n

Question 5

+3 marksOne correct option

If n is a positive integer, what does the following snippet compute? Q = [x for x in range(1, 3 * n + 1) if x % 3 == 0] print(sum(Q))

  1. A

    1+2+3+…+n

  2. B

    3+6+9+…+3n

  3. C

    1+2+3+…+3n

  4. D

    1+3+5+…+(2n+1)

  5. E

    1+3+5+…+(3n+1)

Show answer

Correct answer

  • B

    3+6+9+…+3n

Question 6

+3 marksOne correct option

Consider the following snippets of code: Code-1 T = (4, 5, 6) T[0] = 10 Code-2 S = set() S.insert(2) Code-3 L = [] L.append(3) Select the most appropriate statement.

  1. A

    Only code-1 will throw an error in line-2

  2. B

    Only code-2 will throw an error in line-2

  3. C

    Only code-3 will throw an error in line-2

  4. D

    Code-1 and Code-2 will throw an error line-2

  5. E

    Code-2 and Code-3 will throw an error in line-2

  6. F

    All three code snippets will throw an error in line-2

Show answer

Correct answer

  • D

    Code-1 and Code-2 will throw an error line-2

Question 7

+3 marksOne or more correct options

employees is a list of tuples. Each tuple is of the form (name, salary) . Select all snippets of code that create a list high_earners that contains the names of employees whose salary is above 70,000. A sample list employees and the expected output is given below. You can assume that employees is already available to you. Sample Input employees = [ ('Alice', 75000), ('Bob', 64000), ('Charlie', 82000), ('Diana', 71000), ('Eve', 69000) ] Sample Output ['Alice', 'Charlie', 'Diana']

Select all that apply.

  1. A

    high_earners = [name for (name, salary) in employees if salary > 70000] print(high_earners)

  2. B

    high_earners = [] for (name, salary) in employees: if salary > 70000: high_earners.append(name) print(high_earners)

  3. C

    high_earners = [name if salary > 70000 for (name, salary) in employees] print(high_earners)

  4. D

    high_earners = [name for (name, salary) in employees] print(high_earners)

Show answer

Correct answers

  • A

    high_earners = [name for (name, salary) in employees if salary > 70000] print(high_earners)

  • B

    high_earners = [] for (name, salary) in employees: if salary > 70000: high_earners.append(name) print(high_earners)

Question 8

+3 marksOne or more correct options

Consider the following snippet: f = open('employees.csv', 'r') D = dict() for line in f: name, department, salary = line.strip().split(',') salary = int(salary) if name not in D: D[name] = dict() D[name][department] = salary print(D) This code produces the given output: {'Alice': {'HR': 70000, 'IT': 80000}, 'Bob': {'Finance': 65000, 'IT': 72000}} Which of the following could be the contents of the file employees.csv ? Select all possible answers. Note that dictionaries store keys from left to right in the order in which they are inserted. Once a key has been inserted into a dictionary, its order with respect to other keys doesn't change, unless it is deleted and reinserted.

Select all that apply.

  1. A

    Alice,HR,70000 Bob,Finance,65000 Alice,IT,80000 Bob,IT,72000

  2. B

    Alice,HR,70000 Bob,Finance,66000 Alice,IT,80000 Bob,IT,72000 Bob,Finance,65000

  3. C

    Bob,Finance,65000 Bob,IT,72000 Alice,HR,70000 Alice,IT,80000

  4. D

    Alice,IT,80000 Alice,HR,70000 Bob,Finance,65000 Bob,IT,72000

Show answer

Correct answers

  • A

    Alice,HR,70000 Bob,Finance,65000 Alice,IT,80000 Bob,IT,72000

  • B

    Alice,HR,70000 Bob,Finance,66000 Alice,IT,80000 Bob,IT,72000 Bob,Finance,65000

Question 9

+3 marksOne or more correct options

Consider the following snippet of code: def analyze_string(t, k): freq = {} for ch in t: freq[ch] = freq.get(ch, 0) + 1 is_unique = all(c < k for c in freq.values()) if is_unique: print(True) else: print(False) if input_str.isdigit(): print(True) else: print(False) analyze_string(input_str, 2) input_str is a string variable that has already been defined. The above code runs without any errors. The output when the code given above is executed is as follows: True True Which of the following statements are True? Note that your answer should hold for any value of the string input_str that results in the above output.

Select all that apply.

  1. A

    input_str contains only numeric characters

  2. B

    input_str could contain alphabets

  3. C

    The first character of input_str occurs exactly two times

  4. D

    All elements of input_str occur exactly one time

  5. E

    All elements in input_str occur at least two times.

Show answer

Correct answers

  • A

    input_str contains only numeric characters

  • D

    All elements of input_str occur exactly one time

Question 10

+3 marksWritten answer

What is the output of the following snippet of code? def g(x, y): if x == 1: return 0 return 1 + g(x // y, y) print(g(1024, 2))

Show answer

A written answer, not marked automatically.

Question 11

+3 marksWritten answer

What is the output of the following snippet of code? total = 0 errors = 0 for s in ['5', '2.5', '7', '8']: try: n = int(s) total += n except: errors += 1 total += 5 print(total + errors)

Show answer

A written answer, not marked automatically.

Question 12

+3 marksWritten answer

What is the output of the following snippet of code? total = 0 num = 10 while num <= 20: count = 0 for i in range(1, num + 1): if num % i == 0: count += 1 if count == 2: total += num num += 1 print(total)

Show answer

A written answer, not marked automatically.

Question 13

+3 marksWritten answer

Consider the following snippet of code: L = [2, 3, 4] S = [] T = 0 i = 0 while i < len(L): S += L[:i] + L[i:] for j in S: T += j i += 1 What will be the value of T at the end of execution of the above code?

Show answer

A written answer, not marked automatically.

Question 14

+3 marksWritten answer

What is the output of the following snippet of code? def doSomething(x, y): if x < y: return 0 return 1 + doSomething(x // y, y) log1 = doSomething(81, 3) log2 = doSomething(64, 2) log3 = doSomething(125, 5) print(log1 + log2 + log3)

Show answer

A written answer, not marked automatically.

Question 15

+2 marksOne correct option

You are inside the lift of a building. There are 8 levels in the building: [−3,−2,−1,0,1,2,3,4] The number 0 is the ground floor. Positive numbers correspond to floors above the ground floor, negative numbers correspond to basement levels below the ground floor. The lift has only two buttons. The button U will take you one level up and the button D will take you one level down. You make a sequence of presses. # presses contains the sequence of button presses made by you presses = 'UDDUUUDDUDU' floor = 0 index = 0 while index < len(presses): char = presses[index] if char == 'U': floor += 1 elif char == 'D': floor -= 1 if floor == 2: print(index + 1) break index += 1 Based on the above data answer the subsequent questions.

What is the given code snippet printing?

  1. A

    It prints the number of button presses after which you reach the floor 2 for the first time.

  2. B

    It prints the number of times you cross the floor 2.

  3. C

    It prints the final floor level after all the button presses.

  4. D

    None of these.

Show answer

Correct answer

  • A

    It prints the number of button presses after which you reach the floor 2 for the first time.

Question 16

+2 marksWritten answer

You are inside the lift of a building. There are 8 levels in the building: [−3,−2,−1,0,1,2,3,4] The number 0 is the ground floor. Positive numbers correspond to floors above the ground floor, negative numbers correspond to basement levels below the ground floor. The lift has only two buttons. The button U will take you one level up and the button D will take you one level down. You make a sequence of presses. # presses contains the sequence of button presses made by you presses = 'UDDUUUDDUDU' floor = 0 index = 0 while index < len(presses): char = presses[index] if char == 'U': floor += 1 elif char == 'D': floor -= 1 if floor == 2: print(index + 1) break index += 1 Based on the above data answer the subsequent questions.

What is the output of this snippet of code?

Show answer

A written answer, not marked automatically.

Question 17

+2 marksOne correct option

Consider the following snippet: def ProcedureOne(M): n = len(M) for i in range(n): temp = M[i][i] M[i][i] = M[i][-i - 1] M[i][-i - 1] = temp return M def ProcedureTwo(M): p = 1 n = len(M) for i in range(n): p *= M[i][i] return p M = [[2, 4, 6], [8, 10, 12], [14, 16, 18]] P = ProcedureOne(M) print(P) print(ProcedureTwo(P)) Based on the above code answer the subsequent questions.

What is the first line of output?

  1. A

    [[6, 4, 2], [8, 10, 12], [18, 16, 14]]

  2. B

    [[2, 4, 6], [8, 10, 12], [14, 16, 18]]

  3. C

    [[14, 16, 18], [8, 10, 12], [2, 4, 6]]

  4. D

    [[2, 6, 4], [8, 12, 10], [14, 18, 16]]

Show answer

Correct answer

  • A

    [[6, 4, 2], [8, 10, 12], [18, 16, 14]]

Question 18

+2 marksWritten answer

Consider the following snippet: def ProcedureOne(M): n = len(M) for i in range(n): temp = M[i][i] M[i][i] = M[i][-i - 1] M[i][-i - 1] = temp return M def ProcedureTwo(M): p = 1 n = len(M) for i in range(n): p *= M[i][i] return p M = [[2, 4, 6], [8, 10, 12], [14, 16, 18]] P = ProcedureOne(M) print(P) print(ProcedureTwo(P)) Based on the above code answer the subsequent questions.

What is the second line of output?

Show answer

A written answer, not marked automatically.

Question 19

+1 markWritten answer

You are inside the lift of a building. There are 8 levels in the building: [−3,−2,−1,0,1,2,3,4] The number 0 is the ground floor. Positive numbers correspond to floors above the ground floor, negative numbers correspond to basement levels below the ground floor. The lift has only two buttons. The button U will take you one level up and the button D will take you one level down. You make a sequence of presses. # presses contains the sequence of button presses made by you presses = 'UDDUUUDDUDU' floor = 0 index = 0 while index < len(presses): char = presses[index] if char == 'U': floor += 1 elif char == 'D': floor -= 1 if floor == 2: print(index + 1) break index += 1 Based on the above data answer the subsequent questions.

Show answer

A written answer, not marked automatically.

Question 20

+1 markWritten answer

Consider the following snippet: def ProcedureOne(M): n = len(M) for i in range(n): temp = M[i][i] M[i][i] = M[i][-i - 1] M[i][-i - 1] = temp return M def ProcedureTwo(M): p = 1 n = len(M) for i in range(n): p *= M[i][i] return p M = [[2, 4, 6], [8, 10, 12], [14, 16, 18]] P = ProcedureOne(M) print(P) print(ProcedureTwo(P)) Based on the above code answer the subsequent questions.

Show answer

A written answer, not marked automatically.