0% found this document useful (0 votes)
3 views5 pages

Sample Final

The document is a sample final exam for an Introduction to Computer Programming course, consisting of multiple-choice questions that test knowledge of programming concepts, including data structures, control flow, and object-oriented programming. It includes questions related to Python code snippets and class definitions, along with their expected outputs. The exam aims to assess the students' understanding of the material covered in the course.

Uploaded by

eymenalhallak22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Sample Final

The document is a sample final exam for an Introduction to Computer Programming course, consisting of multiple-choice questions that test knowledge of programming concepts, including data structures, control flow, and object-oriented programming. It includes questions related to Python code snippets and class definitions, along with their expected outputs. The exam aims to assess the students' understanding of the material covered in the course.

Uploaded by

eymenalhallak22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Student Name:

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 )

What will be printed?


A) 25 B) 30 C) 35 D) 20 E) Error

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 def check ( n ): What will be printed?


2 if n % 3 == 0:
A) Yes
3 return " Yes "
4 else : B) No
5 return " No " C) 3
6 D) True
7 print ( check (9))
E) Error

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.

Sensor (Base Class)


1 class Sensor :
2 count = 0 # class variable to track number of sensors
3
4 def __init__ ( self , name , processor ):
5 # ’ callable ’ checks if the object can be called like a function
6 # ( e . g . , functions , lambdas , methods ).
7 if not callable ( processor ):
8 raise TypeError ( " Processor must be a callable function " )
9 self . name = name
10 self . processor = processor
11 Sensor . count += 1
12
13 def read ( self ):
14 pass # to be implemented by subclasses
15
16 def __str__ ( self ):
17 return f " Sensor : { self . name } "

TemperatureSensor (Child Class)


1 class Tem peratureSensor ( Sensor ):
2 def __init__ ( self , name , processor , value ):
3 Sensor . __init__ ( self , name , processor )
4 self . value = value
5
6 def read ( self ):
7 return self . processor ( self . value )

PressureSensor (Child Class)


1 class PressureSensor ( Sensor ):
2 def __init__ ( self , name , processor , readings ):
3 Sensor . __init__ ( self , name , processor )
4 self . readings = readings
5
6 def read ( self ):
7 return self . processor ( self . readings )
8
9 def __add__ ( self , other ):
10 if not isinstance ( other , PressureSensor ):
11 raise TypeError ( " Can only add another PressureSensor " )
12 combined = self . readings + other . readings
13 return PressureSensor ( f " { self . name }+{ other . name } " , self . processor , combined )

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.

1 t = Tempe ratureSensor ( " T " , to_fahrenheit , 15)


2 print ( t . read ())

A) 42 B) 45 C) 62 D) 64 E) Error
Q7.

1 x = PressureSensor ( " P " , 42 , [100])

A) ValueError B) Nothing C) 100 D) TypeError E) 42


Q8.

1 t = Tempe ratureSensor ( " Lab " , to_fahrenheit , 10)


2 print ( t )

A) TemperatureSensor: Lab B) Sensor: Lab C) Lab D) 10 E) Error


Q9.

1 p1 = PressureSensor ( " P1 " , average , [100])


2 t1 = Tem peratureSensor ( " T1 " , to_fahrenheit , 25)
3 p = p1 + t1

A) 100 B) 50 C) 25 D) Error E) None


Q10.

1 p1 = PressureSensor ( " P1 " , average , [100 , 110])


2 p2 = PressureSensor ( " P2 " , average , [90 , 100])
3 p3 = p1 + p2
4 print ( p3 . read ())

A) 100 B) 101 C) 102 D) 99 E) 105


Q11.

1 Sensor ( " Test " , to_fahrenheit )


2 t = Tempe ratureSensor ( " T " , to_fahrenheit , 10)
3 p = PressureSensor ( " P " , average , [100 , 101])
4 print ( Sensor . count )

A) 1 B) 2 C) 3 D) 0 E) Error
Q12.

1 def dummy ( x ): return x


2 t = Tempe ratureSensor ( " T " , dummy , 5)
3 print ( t . processor (7))

A) 5 B) 7 C) None D) Error E) dummy


Q13.

1 p = PressureSensor ( " Check " , average , [105])


2 print ( p . read ())

A) 0 B) 105 C) 100 D) 52 E) Error


Q14.

1 t = Tempe ratureSensor ( " Bad " , to_fahrenheit , " cold " )


2 print ( t . read ())

A) 32 B) Error C) "cold32" D) coldcold E) None


Q15.

1 p1 = PressureSensor ( " X " , average , [95 , 100])


2 p2 = PressureSensor ( " Y " , average , [105])
3 p3 = p1 + p2
4 print ( p3 . read ())

A) 95 B) 100 C) 98 D) 105 E) 101


3
The following code involves multiple inheritance, function passing, and method resolution. The next five questions will be
based on this code.

External Functions Class B

1 def process_value ( x ): 1 class B :


2 return x * 2 + 4 2 def __init__ ( self , x ):
3 3 self . value = x * 2
4 def combine_labels (a , b ): 4
5 return f " { a } -{ b } - Combined " 5 def source ( self ):
6 return " from B "
Class A
Class C (inherits A, B)
1 class A :
2 def __init__ ( self , x ): 1 class C (A , B ):
3 self . value = x + 10 2 def __init__ ( self , x , func ):
4 3 super (). __init__ ( x )
5 def source ( self ): 4 self . func = func
6 return " from A " 5
6 def apply ( self ):
7 return self . func (5)

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

1 if __name__ == " __main__ " :


2 obj1 = C (11 , process_value )
3 obj2 = C (0 , process_value )
4 obj3 = C (3 , process_value )
5 obj4 = D (1 , combine_labels )
6 obj5 = C (0 , process_value )
7
8 print ( obj1 . value ) # Q16
9 print ( obj2 . source ()) # Q17
10 print ( obj3 . apply ()) # Q18
11 print ( obj4 . combine ()) # Q19
12 print ( C . __mro__ ) # Q20

Q16. What will be printed by print(obj1.value)?


A) 22 B) 32 C) 21 D) 20 E) Error
Q17. What will be printed by print(obj2.source())?
A) from A B) from B C) from C D) None E) Error
Q18. What will be printed by print(obj3.apply())?
A) 10 B) 14 C) 13 D) 20 E) Error
Q19. What will be printed by print(obj4.combine())?
A) alphabeta B) alpha-beta C) alpha+beta D) alpha-beta-Combined E) Error
Q20. What is the method resolution order of class C?
A) (C, A, B, object) B) (C, B, A, object) C) (C, object, A, B) D) (C, A, object, B) E) (C, A, B)

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.

You might also like