CS计算机代考程序代写 python Fall 2019
Fall 2019
Student Number: Last (Family) Name(s): First (Given) Name(s):
Final Exam CSE 20-02 Duration: 120 minutes
Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.
This exam consists of 10 questions on 17 pages (including this one), printed on both sides of the paper. When you receive the signal to start, please make sure that your copy of the test is complete, fill in the identifi- cation section above and write your name on the back of the last page. Answer each question directly on the test paper, in the space provided. Write up your solutions carefully! In particular, use notation, terminology, and syntax correctly and explain what you are trying to do — part marks will be given for showing that you know the general structure of an answer, even if your solution is incomplete.
Marking Guide
#1: /10 #2: /10 #3: /11 #4: /19 #5: /8 #6: /14 #7: /8 #8: /20 #9: /10
# 10: /10 TOTAL: /120
Page 1 of 17
Good Luck!
over. . .
Question 1. [10 marks]
For each of the following questions, circle T (true) or F (false).
T F T F
T F T F T F
T F T F T F T F T F T F
Python is generally considered to be an interpreted language.
If one function argument has a default argument all the function arguments must have a default argument.
set([ 1, 2, 1]) == set([1, 2]).
All Python functions have a return value but sometimes the return value is None.
Given: f=lambdax: x>y Evaluate: f(5, 10)
min([“a”, “aa”, “aaa”, “aaaa”]) == “aaaa” assert 0 causes a runtime error
[ 1, 2, 3, 4 ][::2] == [ 2, 4 ]
open(“my file.txt”) opens the file for writing.
A class variable has the same value across all class instances. self is a reserved keyword in Python.
Question 2. [10 marks] Part (a) [1 mark]
What is the value of x?
x = 13 % 5
Part (b) [1 mark] What is the value of x?
x = 5 * 10 > 20
Part (c) [1 mark] What is the type of x?
x = True
Page 2 of 17
cont’d. . .
Fall 2019 Final Exam Part (d) [1 mark]
What is the value of the following expression?
(not 7 * 5 > 100) and (2 > 1 or 1 > 2)
Part (e) [2 marks]
What is the value and type of x?
x= 7//3+10.0
CSE 20-02
Part (f) [4 marks]
A logical (aka semantic) error occurs when Python runs your program successfully but the resulting output
is not correct. List the two other classes of error, describing each one.
Question 3. [11 marks] Part (a) [1 mark]
What is the output of the program?
i=0
j=0
while i < 5:
if i % 2 == 0: j += i
else :
j −= i
i += 1 print ( j )
Page 3 of 17
over...
Part (b) [1 mark]
What is the output of this code?
for i in range(0, −10, −1): print(i, end=‘ ’)
Part (c) [2 marks]
What is the output of this code?
def s(x): return x*x
total = 0
for n in [1, 3, 5]:
total = total + s(n) print ( total )
Part (d) [1 mark] What is the value of x?
x=[]
for i in range(1, 3):
for j in range(1, 3): x.append((i, j))
Part (e) [1 mark]
Rewrite the second line replacing ???? with a list comprehension so that y refers to a list that doubles the
value of each element in x, i.e. such that y == [10.0, 24, 14.4, 200]. x = [ 5.0, 12, 7.2, 100 ]
y = ????
Page 4 of 17 cont’d. . .
Fall 2019 Final Exam Part (f) [1 mark]
What is the value of x?
def fac(n):
if n == 0 or n == 1:
return 1
return fac (n−1) * n
x = fac(4)
Part (g) [2 marks]
What is wrong with the following code? How could you fix it without altering the second line?
x = (1, 2, 3) x[0]=4
Part (h) [2 marks]
What is the result of the expression on line 3, why?
[1] a = (5, 7) [2] b = (5, 10) [3]aHEADER n SEQUENCE LINE 1
SEQUENCE LINE 2
…
SEQUENCE LINE N
Suppose we have the following example file ‘chr1.fa’:
>chromosome 1
GATT n
CCA n
Then this example usage:
header, seq = read fasta(‘chr1.fa;’) print(‘Header:’, header, ‘Seq:’, seq)
Should print:
Header : chromosome 1 Seq : GATTCCA
This is because ‘chromosome 1’ is the header and the function concatenates the sequence lines, minus the new-line characters, to create the sequence. Ensure any file handles are properly closed.
Page 11 of 17 over. . .
Question 8. [20 marks]
Part (a) [2 marks]
What is the output of the following code?
class Myclass :
def init (self , mylist):
self . mylist1 = mylist
self .mylist2 = mylist [:] # Copy the list mylist[0] =−1
cls = Myclass([1,2,3]) print ( cls . mylist1 ) print ( cls . mylist2 )
Part (b) [6 marks]
Define a class called Address with three attributes (instance variables): street name, street number, and city name and one method print address, which prints the values of the attributes on the screen. Give each argument to the constructor method a default value of the empty string ‘’. The following output should work:
a = Address(‘Mission Street ’, ‘420’, ‘Santa Cruz’) a. print address ()
Results in the output:
420 Mission Street , Santa Cruz
Page 12 of 17
cont’d. . .
Fall 2019 Final Exam Part (c) [5 marks]
Extend the Address class by implementing a method such the following usage:
a = Address(‘Infinite Loop’, ‘1’, ‘Cupertino’) print (a)
Results in the output:
1 Infinite Loop, Cupertino
There is no need to rewrite the whole class definition, just the appropriate method.
CSE 20-02
Part (d) [5 marks]
Use inheritance to create a child class of Address called BusinessAddress which adds a business type attribute with a default value of the empty string ‘’. You do not need to alter the printAddress method you implemented. The following usage should work:
a = BusinessAddress(‘Infinite Loop’, ‘1’, ‘Cupertino’, business type=‘Computers ’)
print(a. business type) Resulting in it printing ‘Computers’.
Page 13 of 17
over. . .
Part (e) [2 marks]
When should you use composition instead of inheritance in designing classes?
Part (f) [2 marks]
When should you use inheritance instead of composition in designing classes?
Page 14 of 17 cont’d. . .
Fall 2019 Final Exam CSE 20-02 Question 9. [10 marks]
EXTRA CREDIT: Write a function called mode to return the most frequent item in a sequence. For example:
mode([ 1, 4, 2, 4 ]) == 4
mode([ ‘the’, ‘cat’, ‘in’, ‘the’, ‘hat’ ]) == ‘the’
If two or more items are equally the most frequent item then you may arbitrarily return any one. If the input sequence is empty return None. Use comments and include a docstring. You may use built in functions, but may not use functions from external modules, e.g. you may not use the mode function from the statistics module. Hint: you may find using a dictionary helpful.
Page 15 of 17 over. . .
Question 10. [10 marks]
EXTRA CREDIT: Write a function is square(x), where x is an integer > 0 which returns True if the argument x is square number and otherwise False, i.e. such there exists an integer y for which y *y = x. You may not use any functions from an external module. Please include comments and a docstring.
Page 16 of 17 cont’d. . .
Fall 2019 Final Exam CSE 20-02 On this page, please write nothing except your name and student number.
Student Number: Last (Family) Name(s): First (Given) Name(s):
Page 17 of 17 Total Marks = 120 End of Final Exam