Indian Institute of Technology Madras
BS Degree Programme · Examination & Study Paper
Programming in Python
End Term · 31 Aug 2025 (Forenoon)
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
What will be the output of the following Python code? words = ["jacket", "cap", "scarf", "hat", "sock"] result = [] for idx, word in enumerate(words): if idx % 2 == 0: result.append(word[:2].upper() + str(idx)) elif len(word) > 4: result.append(word[::-1][:3]) else: result.append("0" + word[-1]) print(result)
- A['JA0', 'pa0', 'SC2', 'tah', 'SO4']
- B['JA0', '0p', 'SC2', 'tah', 'SO4']
- C['JA0', '0p', 'SC2', '0t', 'SO4']
- D['JA0', '0p', 'SC2', '0t', '0k']
- Q2MCQ3 Marks·Choose one option
Consider the following Python code: def evaluate_score(score): if score >= 90: if score == 100: print("Perfect Score") else: print("Excellent") elif score >= 60: if score >= 80: print("Very Good") else: print("Good") else: if score >= 40: if score % 2 == 0: print("Needs Improvement") else: print("Barely Passed") else: print("Fail") Which of the following inputs will produce the output _(Needs Improvement) ?
- A42
- B47
- C39
- D60
- Q3MCQ3 Marks·Choose one option
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
- Q4MCQ3 Marks·Choose one option
What will be the total number of lines printed when the following code is executed? scores = { "Aarav": [10, 20, 30], "Bhavna": [15, 25], "Chirag": [12, 18, 24, 30] } round = 0 done = False while not done: done = True for student in scores: if round < len(scores[student]): score = scores[student][round] print( f"Round {round+1}: {student} scored {score}" ) done = False round += 1
- A9
- B8
- C7
- D10
- Q5MCQ3 Marks·Choose one option
What will be the output of the following code snippet? data = [(4, 7), (5, 8), (6, 6), (9, 3)] total = 0 for a, b in data: if a % 2 == 0: if a < b: total += (b - a) elif a == b: total += a + b else: total += (a * b) % 4 else: if b % 2 == 0: total += b else: total -= a print(total)
- A30
- B14
- C16
- D6
- Q6MCQ3 Marks·Choose one option
What will be the output of the following code? items = ["apple", "banana", "cherry", "date", "fig", "grape", "kiwi"] sliced = items[::2] # Step of 2 filtered = [fruit for fruit in sliced if len(fruit) > 4] result = "_".join(filtered[::-1]) print(result)
- Aapple_cherry
- Bfig_kiwi
- Cgrape_fig
- Dcherry_apple
- Q7MSQ3 Marks·Choose all correct options
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))
- Q8MSQ3 Marks·Choose all correct options
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
- Q9MSQ3 Marks·Choose all correct options
Consider the following code snippet: def update_scores(scores, bonus): updated = [] for score in scores: updated.append(score + bonus) return updated original_scores = [10, 20, 30] new_scores = update_scores(original_scores, 5) Which of the following statements are TRUE?
- AThe list _(original_scores) remains unchanged after calling _(update_scores)
- BThe function _(update_scores) returns a new list
- CThe variable _(score) inside the function is local to the function
- DThe function modifies the _(scores) list in place
- Q10NAT2 Marks·Write the number
Consider the following Python code: names = ["Aarav", "Bhuvan", "Charan", "Deepa"] marks = [88, 76, 92, 81] bonus = [5, 3, 4, 2] total = 0 for name, mark, extra in zip(names, marks, bonus): if "a" in name.lower(): total += mark + extra else: total += mark print(total) What will be the output of the above code?
Your answer: - Q11NAT2 Marks·Write the number
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: - Q12NAT3 Marks·Write the number
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: - Q13NAT3 Marks·Write the number
Consider the following Python code: def transform(words): if not words: return [] first = words[0][::-1] if first == first[::-1]: first = "<|>" return [first] + transform(words[1:]) sentence = "refer logic noon stats apple radar" words = sentence.split() new_sentence = " ".join(transform(words)) print(len(new_sentence)) What is the output of the given code?
Your answer: - Q14NAT3 Marks·Write the number
Consider the following Python code snippet: colors = ['red', 'blue', 'green', 'red', 'blue', 'red', 'yellow'] freq = [colors.count(color) for color in colors] filtered = [f for f in freq if f >= 2] filtered.sort(reverse=True) if filtered: filtered.pop(0) if filtered: filtered.pop(-1) print(len(filtered)) What is the output of the given code?
Your answer: Read the passage below, then answer Q15–Q17
Consider the following Python code and answer the sub-questions:
students = [
{
"name": "Arya",
"subjects": {
"Math": 78, "Science": 88, "English": 92
}
},
{
"name": "Bilal",
"subjects": {
"Math": 65, "Science": 55, "English": 60
}
},
{
"name": "Chitra",
"subjects": {
"Math": 45, "Science": 40, "English": 42
}
},
{
"name": "Deep",
"subjects": {
"Math": 90, "Science": 91, "English": 85
}
},
{
"name": "Esha",
"subjects": {
"Math": 35, "Science": 39, "English": 55
}
},
]
def filter_and_rank(data):
eligible = []
for student in data:
marks = list(student["subjects"].values())
passed = 0
high = 0
for mark in marks:
if mark >= 40:
passed += 1
if mark >= 90:
high += 1
if passed == len(marks) and high > 0:
total = sum(marks)
eligible.append((student["name"], total))
eligible.sort(key=lambda x: x[1], reverse=True)
return [name for name, _ in eligible]
Q15MCQ2 Marks·Choose one optionWhat is the output of filter_and_rank(students) ?
- A['Deep', 'Arya']
- B['Arya', 'Deep']
- C['Deep']
- D['Arya']
- Q16MSQ2 Marks·Choose all correct options
Which of the following students failed to be included due to failing at least one subject?
- ABilal
- BChitra
- CEsha
- DArya
- Q17NAT1 Mark·Write the number
If a new student {"name": "Fatima", "subjects": {"Math": 90, "Science": 90, "English": 90}} is added to the list, what will be the new position (1-based index) of _(Fatima) in the ranked output list returned by _(filter_and_rank) ?
Your answer: Read the passage below, then answer Q18–Q20
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)
Q18NAT2 Marks·Write the numberWhat will the output of the given code?
Your answer:- Q19MSQ2 Marks·Choose all correct options
Which of the following are correct?
- AElectricVehicle.ev_count is 3
- BPetrolVehicle.pv_count is 2
- C"Mahindra e2o" is in low_battery_models
- DVehicle.get_total() will raise an error
- Q20MCQ1 Mark·Choose one option
What does fleet[1].get_info() return?
- AModel: Mahindra e2o, Battery: 35%, Status: Low
- BModel: Mahindra e2o, Fuel: 35L
- CModel: Mahindra e2o
- DModel: Mahindra e2o, Battery: 35%