Professional Documents
Culture Documents
CS115 Final 2021 Fall
CS115 Final 2021 Fall
Section: Grade:
Exam Room:
Exam: Final Q1 : 30
1
Question 1 (30 pts.)
A. (24 points) For each part below, write a single Python statement in the box provided to answer the question.
Important:
● Write a single statement for each part.
● You should NOT use any loops in your solutions. Use ONLY numpy functions and numpy operators.
● You should NOT use any tuple / range / list / dictionary objects or list comprehensions in your solutions.
● Assume that the numpy module is imported as follows: import numpy as np
1. Create a one-dimensional array of integers which are multiples of 3 from 0 to 72 (inclusive), named x.
x = np.arange(0, 73, 3)
3. Print the indexes of the elements in the array y which are equal to 50.
print(np.where(y == 50))
print(np.argmax(y)) or print(y.argmax())
m = y.reshape(5, 5)
6. Update the array m by multiplying all the elements in the third column by 5.
m[:,2] *= 5
7. Print all integers in the array m in rows (indexes) 1 to 3, inclusive, and columns (indexes) 1 to 3, inclusive.
print(m[1:4,1:4])
2
B. (6 points) Trace the following and show the exact output in the space provided.
class A: Answer:
def __init__(self , a):
self.__a = a
def myfun(self): 36
return 2 * self.__a
def __str__(self): 1 15
return str(self.__a) + ' ' + str(self.myfun())
def get_a(self):
return self.__a
class B(A):
def __init__(self , a, b):
A.__init__(self, a)
self.__b = b
def myfun(self):
self.increase_a(4)
self.__b += 5
return self.get_a() + self.__b
x = A(3)
y = B(1, 5)
print(x)
print(y)
z = y
print(z)
3
Question 2 (70 pts.)
This question has 2 parts. In part (a) you will complete the Measurement class using the information given in the
comments. In part (b), you will complete the function and application using the Measurement class defined in part (a)
and by following the instructions given. Do not duplicate functionality.
Instructions:
● Complete the program by filling in the spaces with the thick, black border in the right margin.
● Read the comments carefully and do only what is asked.
● You may not add any additional methods or update the visibility (private/public) of the attributes.
class Measurement:
4
def get_value( self ):
"""
Returns the value of the Measurement object
"""
return self.__value
5
b) (40 pts) Using the Measurement class, implement the following function and python script. The script
uses data from a file, measures.txt (sample file shown below). You should read the question carefully
and do only what is asked using Measurement functionality. You may not use numpy in your solution.
Sample Run:
Original Measurements: [1.20m, 50.00cm, 70.00cm, 1.40m, 50.00cm]
Increased Measurements: [1.74m, 72.50cm, 1.01m, 2.03m, 72.50cm]
Sorted Increased Measurements: [72.50cm, 72.50cm, 1.01m, 1.74m, 2.03m]
6
#open file measures.txt
f = open('measures.txt')
#read the Measurements from the file into the list, measures
measures = []
for line in f:
print(line)
data = line.split(',')
m = Measurement(float(data[0]),data[1].strip())
measures.append( m )
plt.subplot(1,2,2)
plt.plot(measurements,'*')
plt.title('Increased Meter Measurements - star marker')