Sample Final
Sample Final
Time: 75 min
Student ID:
Final Exam Sample
Introduction to Computer Programming
Questions
Q1
What will be printed?
A) 10
1 data = { " a " : 10 , " b " : 20 , " c " : 30}
2 data [ " b " ] = data [ " a " ] + data [ " c " ] B) 20
3 print ( data [ " b " ]) C) 30
D) 40
E) 50
Q2
What will be printed?
1 count = 0
A) 6
2 for i in range (1 , 6):
3 if i % 2 != 0: B) 9
4 count += i C) 10
5 print ( count ) D) 15
E) 8
Q3
1 scores = { " Alice " : 10 , " Bob " : 15 , " Carol " : 5}
2 total = 0
3
4 for name , score in scores . items ():
5 total += score
6
7 print ( total )
Q4
What will be printed?
1 x = 0 A) 6
2 for i in range (2 , 5): B) 7
3 x += i
C) 8
4 print ( x )
D) 9
E) 10
Q5
1
The following code defines a basic sensor system. The next 10 questions will be based on
your understanding of this code. Read it carefully.
Processing Functions
1 def to_fahrenheit ( celsius ):
2 # simplified conversion : 1.8 is rounded to 2 for easier math
3 return celsius * 2 + 32
4
5 def average ( values ):
6 return sum ( values ) // len ( values ) # using integer division
2
Each of the following questions is based on the sensor code from the previous page. For each
snippet, choose the correct output or behavior.
Q6.
A) 42 B) 45 C) 62 D) 64 E) Error
Q7.
A) 1 B) 2 C) 3 D) 0 E) Error
Q12.
Class D (inherits B, A)
1 class D (B , A ):
2 def __init__ ( self , x , combiner ):
3 super (). __init__ ( x )
4 self . combiner = combiner
5
6 def combine ( self ):
7 return self . combiner ( " alpha " , " beta " )
Main Execution
4
1
2 print ( C . __mro__ ) # Q20 : __mro__ shows method resolution order .
3 # It returns the order in which Python searches for methods in a class hierarchy .
4 # Useful in multiple inheritance . This order is determined by C3 linearization :
5 # a rule that combines depth - first and left - to - right traversal ,
6 # while ensuring that a class always appears before its parents .
7 # In this example , class C inherits from A and B : class C (A , B )
8 # A and B both inherit from object ( implicitly ).
9 # So the MRO is :
10 # ( < class ’C ’>, < class ’A ’>, < class ’B ’>, < class ’ object ’ >)
11 # because Python checks C first , then A ( leftmost ) , then B , then object .
Note: The questions above are provided as illustrative examples to familiarize you with the format and structure of the
final exam. The actual exam questions will differ in content and may include more advanced variations of the topics
covered.