uiz Space

May 2025 term · Programming in Python · BSCS1002

Programming in Python Quiz 1: 13 July 2025 (May 2025 term)

The IIT Madras BS Programming in Python (Python) Quiz 1 paper sat on 13 Jul 2025, in the May 2025 term: 14 questions for 50 marks in 120 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.

Questions
14
Marks
50
Duration
120 min
MCQ
7
MSQ
3
Numerical
4

Updated

Official paper: IIT M DIPLOMA AN EXAM QDD2 13 July 2025 · No negative marking.

Question 1

+3 marksOne correct option

Consider the following code:
text = " >>> Hello, Python! <<< "
to_strip = " <>"
A = text.strip(to_strip)
B = text.lstrip(to_strip)
C = text.rstrip(to_strip)
D = text.strip()
print(A)
print(B)
print(C)
print(D)
Which of the following is the correct output?

  1. A

    Hello, Python!
    " Hello, Python! <<< "
    " >>> Hello, Python! "
    >>> Hello, Python! <<<

  2. B

    Hello, Python!
    Hello, Python! <<<
    >>> Hello, Python!
    >>> Hello, Python! <<<

  3. C

    Hello, Python!.
    >>> Hello, Python!
    Hello, Python! <<<
    >>> Hello, Python! <<<

  4. D

    >>> Hello, Python! <<<
    Hello, Python! <<<
    >>> Hello, Python!
    Hello, Python!

Show answer

Correct answer

  • B

    Hello, Python!
    Hello, Python! <<<
    >>> Hello, Python!
    >>> Hello, Python! <<<

Question 2

+3 marksOne correct option

Consider a Python program that takes a sentence (which may contain both uppercase and lowercase letters, spaces, and punctuation) and prints the number of vowels present.
Snippet-1
sentence = input()
count = 0
vowels = set("aeiouAEIOU")
for char in sentence:
if char in vowels:
count += 1
print(count)
Snippet-2
sentence = input()
count = 0
for char in sentence.lower():
if char in ['a', 'e', 'i', 'o', 'u']:
count += 1
print(count)
Which of these two snippets is correct?

  1. A

    Only snippet-1 is correct

  2. B

    Only snippet-2 is correct

  3. C

    Both snippets are correct

  4. D

    Both snippets are wrong

Show answer

Correct answer

  • C

    Both snippets are correct

Question 3

+3 marksOne correct option

What is the output of the following Python code?
data = ["alpha", "beta", "gamma"]
data[1] = data[1].replace("e", data[0][1])
data[2] = data[2][::-1].replace("a", data[0][3])
print(data)

  1. A

    ['alpha', 'blta', 'hmmhg']

  2. B

    ['alpha', 'blta', 'ghmmh']

  3. C

    ['alpha', 'blta', 'htlb']

  4. D

    ['alpha', 'blta', 'hmmh']

Show answer

Correct answer

  • A

    ['alpha', 'blta', 'hmmhg']

Question 4

+4 marksOne correct option

We want to print the following pattern.
1111
1001
1011
1111
Which of these two snippets is correct?
Snippet-1
n = 4
for i in range(n):
for j in range(n):
if i == 0 or i == n-1 or j == 0 or j == n-1:
print('1', end='')
elif i == j:
print('1', end='')
else:
print('0', end='')
print()
Snippet-2
n = 4
for i in range(n):
row = ''
for j in range(n):
if i in [0, n-1] or j in [0, n-1]:
row += '1'
elif i == j:
row += '0'
else:
row += '1'
print(row)

  1. A

    Only Snippet-1 prints the pattern correctly

  2. B

    Only Snippet-2 prints the pattern correctly

  3. C

    Both snippets print the pattern correctly

  4. D

    Both snippets print incorrect patterns

Show answer

Correct answer

  • D

    Both snippets print incorrect patterns

Question 5

+4 marksOne or more correct options

P is a non-empty list of distinct integers that has already been defined. Which of the following statements are true at the end of execution of the code given below? Q, R = [ ], [ ]
for x in P:
Q.append(-x)
Q.sort()
for x in Q:
R.append(-x)

Select all that apply.

  1. A

    P is always equal to R , i.e., P == R returns the value True

  2. B

    Every element in P is present in R , but not necessarily in the same sequence.

  3. C

    R is a list of integers sorted in descending order.

  4. D

    Q is a list of integers sorted in ascending order.

Show answer

Correct answers

  • B

    Every element in P is present in R , but not necessarily in the same sequence.

  • C

    R is a list of integers sorted in descending order.

  • D

    Q is a list of integers sorted in ascending order.

Question 6

+4 marksOne or more correct options

Consider the following Python code:
def process(x, y):
if x > y:
return x - y
elif x < y:
return y - x
else:
return x + y
def compute(a, b):
result = process(a, b)
if result % 2 == 0:
return "Even"
else:
return "Odd"
Which of the following statements is/are true?

Select all that apply.

  1. A

    If x = 5 and y = 2, then output will be Odd

  2. B

    If x = 4 and y = 8, then output will be Even

  3. C

    If x = 3 and y = 3, then output will be Odd

  4. D

    If x = 10 and y = 5, then output will be Even

Show answer

Correct answers

  • A

    If x = 5 and y = 2, then output will be Odd

  • B

    If x = 4 and y = 8, then output will be Even

Question 7

+3 marksNumerical answer

Consider the following snippet of code. What will the output be?
result = 0
for i in range(1, 4):
for j in range(1, 4):
if i == j:
continue
if i + j > 4:
break
result += i + j
print(result)

Show answer

Correct answer: 14

Question 8

+4 marksNumerical answer

What is the output of the following snippet of code? Enter an integer as your answer. lst = ['IIT', ['Madras', '2025'], 'exam']
print(len(lst[0]) + len(lst[1][1]) + len(lst[2]))

Show answer

Correct answer: 11

Question 9

+4 marksNumerical answer

What is the output of the following snippet of code? Enter an integer as your answer. lst = [x for x in [1,0,-2]]
result = 0
for val in lst:
if val == 0:
result += 10
elif val < 0:
result += 5
elif val % 2 == 1:
result += 3
else:
result += 1
print(result)

Show answer

Correct answer: 18

Question 10

+4 marksNumerical answer

Consider the following snippet of code. What will the output be?
names = ["alice", "bob", "charlie", "david", "eve"]
result = 0
short = []
for name in names:
if len(name) < 4:
short.append(name)
elif len(name) == 5:
result += 1
elif 'a' in name:
result += 2
print(len(short) + result)

Show answer

Correct answer: 6

Question 11

+4 marksOne correct option

Consider the following snippet of code and answer the given sub-questions.
def transform(data):
result = []
for item in data:
if isinstance(item, str) and len(item) % 2 == 0:
result.append(item.upper())
elif isinstance(item, int) and item % 2 == 1:
result.append(item * item)
else:
continue
return result
sample = ['ai', 5, 'sql', 4, 'ml', 8, 9]
output = transform(sample)
Note: The built-in function isinstance(object, type) is used to check if an object is of a specified type.

What is the value of output after executing the given code?

  1. A

    ['AI', 25, 'ML']

  2. B

    ['AI', 25, 'ML', 64]

  3. C

    ['AI', 25, 'ML', 81]

  4. D

    ['AI', 16, 'ML', 64]

Show answer

Correct answer

  • C

    ['AI', 25, 'ML', 81]

Question 12

+3 marksOne or more correct options

Consider the following snippet of code and answer the given sub-questions.
def transform(data):
result = []
for item in data:
if isinstance(item, str) and len(item) % 2 == 0:
result.append(item.upper())
elif isinstance(item, int) and item % 2 == 1:
result.append(item * item)
else:
continue
return result
sample = ['ai', 5, 'sql', 4, 'ml', 8, 9]
output = transform(sample)
Note: The built-in function isinstance(object, type) is used to check if an object is of a specified type.

Which of the following changes would also include SQL in the final result?

Select all that apply.

  1. A

    Make the length condition len(item) % 2 == 1 instead of == 0.

  2. B

    Remove the condition len(item) % 2 == 0 .

  3. C

    Ensure 'sql' is written as 'SQL' in the original list.

  4. D

    Remove the condition isinstance(item, str)

Show answer

Correct answers

  • A

    Make the length condition len(item) % 2 == 1 instead of == 0.

  • B

    Remove the condition len(item) % 2 == 0 .

Question 13

+4 marksOne correct option

Consider the following snippet of code and answer the given sub-questions.
def matrix_transform(matrix):
result = []
i = 0
while i < len(matrix):
row = matrix[i]
for element in row:
if element % 2 == 1:
result.append(element * 2)
i += 1
return result
matrix = [[1, 2, 3], [4, 5], [6]]
output = matrix_transform(matrix)
print(output)

What is the value of output after executing the given code snippet?

  1. A

    [2, 6, 10, 14]

  2. B

    [2, 6, 10]

  3. C

    [2, 8, 10]

  4. D

    [2, 6, 14]

Show answer

Correct answer

  • B

    [2, 6, 10]

Question 14

+3 marksOne correct option

Consider the following snippet of code and answer the given sub-questions.
def matrix_transform(matrix):
result = []
i = 0
while i < len(matrix):
row = matrix[i]
for element in row:
if element % 2 == 1:
result.append(element * 2)
i += 1
return result
matrix = [[1, 2, 3], [4, 5], [6]]
output = matrix_transform(matrix)
print(output)

In the given code, if we change the condition to if element % 2 == 0 at line 7, then what is the value of output after executing it?

  1. A

    [4, 8, 12]

  2. B

    [4, 8, 10]

  3. C

    [9, 10]

  4. D

    [6, 8, 12]

Show answer

Correct answer

  • A

    [4, 8, 12]