Advanced Python Examination
November 12, 2025
Total Points: 100
Name:
SECTION 1: MULTIPLE CHOICE [48 pts] (4 pts each) - For each question, select the
BEST answer
Question 1: Control Flow & Short-Circuit Evaluation
[ ]: x = 5
y = 10
result = x > 3 and y / 0 or x < 10
print(result)
What will be printed?
� A. True
� B. False
� C. ZeroDivisionError occurs
� D. None
Question 2: List Mutability & Function Side Effects
[ ]: def mystery(lst, val=[]):
[Link](lst)
return val
a = mystery(1)
b = mystery(2)
c = mystery(3, [])
print(len(a), len(b), len(c))
What will be printed?
� A. 1 1 1
� B. 1 2 1
� C. 2 2 1
� D. 3 3 1
1
Question 3: Dictionary Iteration & Modification
[ ]: d = {'a': 1, 'b': 2, 'c': 3}
for key in d:
if d[key] == 2:
d['d'] = 4
break
print(len(d))
What happens?
� A. Prints 3
� B. Prints 4
� C. RuntimeError occurs
� D. Prints 5
Question 4: Tuple vs List Reference
[ ]: t1 = ([1, 2], [3, 4])
t2 = t1
t1[0].append(5)
t1 = ([6, 7], [8, 9])
print(t2[0])
What will be printed?
� A. [1, 2]
� B. [1, 2, 5]
� C. [6, 7]
� D. Error occurs
Question 5: Set Operations & Mutability Which of the following will cause an error?
� A. s = {1, 2, (3, 4)}
� B. s = {[1, 2], 3}
� C. s = {(1, [2, 3]), 4}
� D. Both B and C
2
Question 6: Deep vs Shallow Copy
[ ]: import copy
original = [[1, 2], [3, 4]]
shallow = [Link](original)
deep = [Link](original)
original[0][0] = 99
original[1] = [5, 6]
print(shallow[0][0], shallow[1][0], deep[0][0])
What will be printed?
� A. 99 5 1
� B. 99 3 1
� C. 1 3 1
� D. 99 5 99
Question 7: Pass by Reference Confusion
[ ]: def modify(a, b, c):
a = a + 1
[Link](4)
c = c + [5]
return a, b, c
x, y, z = 1, [2, 3], [4]
result = modify(x, y, z)
print(x, len(y), len(z), result[0])
What will be printed?
� A. 1 3 1 2
� B. 2 3 2 2
� C. 1 3 2 2
� D. 1 2 1 2
3
Question 8: OOP - Inheritance & Method Resolution
[ ]: class A:
def method(self):
return "A"
class B(A):
def method(self):
return "B" + super().method()
class C(A):
def method(self):
return "C" + super().method()
class D(B, C):
pass
obj = D()
print([Link]())
What will be printed?
� A. BA
� B. BCA
� C. BCAA
� D. Error occurs
Question 9: Polymorphism & Duck Typing
[ ]: class Cat:
def speak(self):
return "Meow"
class Dog:
def speak(self):
return "Woof"
def make_sound(animal):
return [Link]()
animals = [Cat(), Dog(), "Bird"]
result = [make_sound(a) for a in animals]
What happens?
� A. Returns [‘Meow’, ‘Woof’, error]
� B. Error on third iteration
� C. Returns [‘Meow’, ‘Woof’, ‘Bird’]
� D. Error before loop starts
4
Question 10: Aggregation vs Composition Which statement is TRUE about aggregation vs
composition?
� A. In aggregation, the contained object cannot exist independently
� B. In composition, the contained object can exist independently
� C. Aggregation is a “has-a” relationship, composition is “part-of”
� D. Composition objects are destroyed when container is destroyed
Question 11: Pandas - DataFrame Manipulation
[ ]: import pandas as pd
df = [Link]({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = df
df2['C'] = [7, 8, 9]
[Link][0, 'A'] = 99
print([Link][0, 'A'], 'A' in [Link], 'C' in [Link])
What will be printed?
� A. 1 True False
� B. 99 True True
� C. 99 True False
� D. 1 True True
Question 12: Try-Catch-Finally with Return
[ ]: def mystery():
try:
return 1
except:
return 2
finally:
return 3
print(mystery())
What will be printed?
� A. 1
� B. 2
� C. 3
� D. None
5
SECTION 2: OUTPUT PREDICTION [30 pts] (6 pts each) - Write the EXACT output
that will be printed
Question 13: Complex List Comprehension with Conditions
[ ]: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = [matrix[i][j] for i in range(len(matrix))
for j in range(len(matrix[i]))
if i == j or i + j == len(matrix) - 1]
print(result)
Output:
Question 14: Nested Dictionary & Set Operations
[ ]: data = {
'group1': {1, 2, 3},
'group2': {2, 3, 4},
'group3': {3, 4, 5}
}
result = {}
for key, value in [Link]():
common = value & data['group1']
if len(common) >= 2:
result[key] = list(common)
print(sorted([Link]()), sum(len(v) for v in [Link]()))
Output:
Question 15: OOP with Class & Instance Variables
[ ]: class Counter:
count = 0
def __init__(self):
[Link] += 1
[Link] = [Link]
def increment(self):
[Link] += 1
[Link] += 1
c1 = Counter()
c2 = Counter()
[Link]()
c3 = Counter()
print([Link], [Link], [Link], [Link])
Output:
6
Question 16: Pandas GroupBy & Aggregation
[ ]: import pandas as pd
df = [Link]({
'Category': ['A', 'B', 'A', 'B', 'A'],
'Value': [10, 20, 30, 40, 50],
'Count': [1, 2, 3, 4, 5]
})
result = [Link]('Category').agg({
'Value': 'sum',
'Count': 'mean'
})
print([Link]['A', 'Value'], int([Link]['B', 'Count']))
Output:
Question 17: Multiple Exception Handling
[ ]: def process(lst):
try:
result = []
for i in range(len(lst) + 1):
try:
[Link](10 / lst[i])
except ZeroDivisionError:
[Link](0)
except IndexError:
[Link](-1)
raise
except:
return result
finally:
[Link](999)
return result
print(process([2, 0, 5]))
Output:
7
SECTION 3: CODING PROBLEM [22 pts]
Question 18: Advanced OOP with Pandas Integration You are building a grade manage-
ment system for a university. Create a class hierarchy with the following requirements:
Base Class: Student
Attributes:
name (str): student name
student_id (str): unique identifier
grades (dict): maps course names to grades (default empty dict)
Methods:
__init__(self, name, student_id, grades={}) : Initialize student
add_grade(self, course, grade) : Add a grade for a course (adds to grades dict)
get_gpa(self) : Calculate and return GPA (average of all grades, rounded to 2 decimals)
__lt__(self, other) : Compare students by GPA (return True if self’s GPA < other’s GPA)
__str__(self) : Return formatted string: “Name (ID): GPA” (e.g., “Alice (001): 3.75”)
Derived Class: GraduateStudent(Student)
Additional Attributes:
thesis_grade (float or None): thesis grade (default None)
advisor (str): advisor name
Constructor:
__init__(self, name, student_id, grades, thesis_grade, advisor)
Override Methods:
get_gpa(self) : Calculate GPA where thesis counts as 2 courses if thesis_grade is not None. If
thesis_grade is None,
calculate normally.
Formula: (sum of all course grades + 2 * thesis_grade) / (number of courses + 2)
__str__(self) : Return: “Name (ID): GPA [Advisor: advisor_name]”
Function: analyze_class(students_list)
Parameter:
students_list (list): list of Student/GraduateStudent objects
Returns:
pandas DataFrame with columns: ‘Name’ , ‘ID’ , ‘Type’ , ‘GPA’ , ‘Status’
Requirements:
8
‘Type’ column: “Undergrad” for Student, “Grad” for GraduateStudent
‘Status’ column rules:
GPA >= 3.5: “Honors”
3.0 <= GPA < 3.5: “Good Standing”
2.0 <= GPA < 3.0: “Probation”
GPA < 2.0: “Academic Warning”
DataFrame should be sorted by GPA in descending order
Reset index to start from 0
Example Usage:
[ ]: s1 = Student("Alice", "001", {"Math": 4.0, "CS": 3.5})
s2 = Student("Bob", "002", {"Math": 3.0, "CS": 2.5})
g1 = GraduateStudent("Carol", "G001", {"AI": 4.0, "ML": 3.8}, 3.9, "Dr. Smith")
g2 = GraduateStudent("Dave", "G002", {"DB": 2.5, "Networks": 2.8}, 2.0, "Dr.␣
↪Jones")
df = analyze_class([s1, s2, g1, g2])
print(df)
Expected Output:
Name ID Type GPA Status
0 Carol G001 Grad 3.90 Honors
1 Alice 001 Undergrad 3.75 Honors
2 Bob 002 Undergrad 2.75 Probation
3 Dave G002 Grad 2.43 Probation