• No results found

(3) You may use one 8.5” x 11” note sheet during the exam

N/A
N/A
Protected

Academic year: 2021

Share "(3) You may use one 8.5” x 11” note sheet during the exam"

Copied!
12
0
0

Loading.... (view fulltext now)

Full text

(1)

Name:

Section: Date:

INSTRUCTIONS:

(1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

(2) This exam booklet contains 30 questions, each of which will be weighted equally at 5 points each. It also contains a 50-point written question. The total points for the exam is 200 points.

(3) You may use one 8.5” x 11” note sheet during the exam. No other reference materials or electronic devices may be used during the examination, i.e. no cal- culators, cell phones, mp3 players, etc.. Paper dictionaries are allowed.

(4) Questions will not be interpreted during the examination.

(5) You should choose the single best alternative for each question, even if you be- lieve that a question is ambiguous or contains a typographic error. If a question has more than one best answer, credit will be given for any of the correct answers.

Provide only one answer.

(6) Please fill in the requested information at the top of this exam booklet.

(7) Use a #2 pencil to encode answers on the OMR form.

(8) Please encode the following on the OMR form:

– Last name and first initial – MSU PID

– Exam form (3 A)

(9) Please sign the OMR form.

(10) Only answers recorded on your OMR form for multiple choice questions will be counted for credit. Completely erase any responses on the OMR form that you wish to delete.

(11) You must turn in this exam booklet, the OMR form, your note sheet, and your scrap paper when you have completed the exam. Be sure your name is on any of these items that you would like to have returned.

(12) When leaving, please be courteous to those still taking the exam.

(2)

KEY

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

B A B D B A D C D C A D B A B

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

A A C A B A A D A B A D D C D

(3)

(1) Which of the following is not legal to use as the name of a variable in Python?

(a) a (b) 3abc (c) a b

(d) None is legal. (e) All are legal.

(2) Which of the following is a good reason to use a function.

(a) Encapsulate a complex algorithm.

(b) Inherit the features of a parent.

(c) Create a global variable.

(d) All of the above.

(e) None of the above.

(3) Which of the following statements about a Python program is true?

(a) You can determine the types of the values that each variable references by reading the program code.

(b) The type of the value that a variable references can change as the program executes.

(c) If evaluating the expression A == B does not raise an error (exception) then variables A and B must reference values of the same type.

(d) All of the above.

(e) None of the above.

(4) Following the assignment

m = ["for", "score", "and", "twenty", "years", "ago"]

what value is returned by the expression m[2:4]?

(a) ["fo", "sc", "an", "tw"]

(b) ["score", "and", "twenty", "years"]

(c) ["and", "twenty", "years"]

(d) ["and", "twenty"]

(e) None of the above.

(5) Following the assignment

m = ["for", "score", "and", "twenty", "years", "ago"]

what value is returned by the expression m[1][1]?

(a) [["f"]]

(b) "c"

(c) ["for"]

(d) an error (exception) (e) None of the above.

(4)

(6) What value is returned by the expression:

[i+j for i in range(3) for j in range(2) if i != j]

(a) [1, 1, 2, 3] (b) [0, 1, 1, 2, 2, 3]

(c) [3, 3, 4, 5] (d) [2, 3, 3, 4, 4, 5]

(e) None of (a)–(d).

(7) If adict references a dictionary, which code sequence will assign to m the maxi- mum value stored in adict?

(a) a = list(adict.items()) a.sort()

m = a[0]

(b) a = list(adict.values()) a.sort()

m = a[0]

(c) a = list(adict.items()) a.sort()

m = a[-1]

(d) a = list(adict.values()) a.sort()

m = a[-1]

(e) None of (a)–(d).

(8) Given

import math

which code segment prints a table showing each number from 1 to 100 and its square root, lined up in columns, both right justified, the number as an integer and its square root as a floating point number to two decimal digits of accuracy?

(a) for n in range(1,101):

print("{} {}".format(n, math.sqrt(n))) (b) for n in range(1,101):

print("{:>d} {:>2f}".format(n, math.sqrt(n))) (c) for n in range(1,101):

print("{:>2d}{:>10.2f}".format(n, math.sqrt(n))) (d) All of (a)–(c).

(e) None of (a)–(c).

(5)

(9) Given

fobj = open("data.txt", "w")

line lst = ["Line 1", "Line 2", "Line 3"]

which code sequence will write the strings in line lst to file data.txt, one string to a line, as follows.

Final contents of file data.txt:

Line 1 Line 2 Line 3

(a) print(’\n’.join(line_lst), file = fobj)

(b) fobj.writelines([line+’\n’ for line in line_lst]) (c) for line in line_lst:

print(line, file = fobj) (d) All of the above.

(e) None of the above.

(10) If the ‘<’ operator is overloaded, such as in the Rational class we defined, and if R1 and R2 are instances of Rational, then Python evaluates R1 < R2 as the call:

(a) lt (R1, R2) (b) R1.__<__(R2)

(c) R1.__lt__(R2) (d) R2.__rgt__(R1)

(e) None of the above.

(6)

def foo (l1, l2):

if len(l1) > 3:

l1 = l1[:3]

l2[0] = ’do’

a1 = [’do’, ’re’, ’mi’, ’fa’]

a2 = [’so’, ’la’, ’ti’, ’do’]

foo(a1, a2)

print(a1) # Line 1

print(a2) # Line 2

Figure 1

(11) What is printed by Line 1 when Figure 1 is executed?

(a) [’do’, ’re’, ’mi’, ’fa’] (b) [’do’, ’re’, ’mi’]

(c) [’so’, ’la’, ’ti’, ’do’] (d) [’do’, ’la’, ’ti’, ’do’]

(e) None of (a)–(d).

(12) What is printed by Line 2 when Figure 1 is executed?

(a) [’do’, ’re’, ’mi’, ’fa’] (b) [’do’, ’re’, ’mi’]

(c) [’so’, ’la’, ’ti’, ’do’] (d) [’do’, ’la’, ’ti’, ’do’]

(e) None of (a)–(d).

(7)

import copy L1 = [1, 2]

L2 = [’a’, L1]

L3 = copy.deepcopy(L2)

print (L3 is L2) # Line 1 print (L2 == L3) # Line 2 L1[1] = L3

print (L2 == L3) # Line 3

print (L2) # Line 4

Figure 2

(13) What is printed by Line 1 when Figure 2 is executed?

(a) True (b) False (c) None of (a)–(b).

(14) What is printed by Line 2 when Figure 2 is executed?

(a) True (b) False (c) None of (a)–(b).

(15) What is printed by Line 3 when Figure 2 is executed?

(a) True (b) False (c) None of (a)–(b).

(16) What is printed by Line 4 when Figure 2 is executed?

(a) [’a’, [1, [’a’, [1, 2]]]]

(b) [’a’, [1, 2]]

(c) [[’a’, [1, 2]], [1, 2]]

(d) [’a’, L2]

(e) None of (a)–(d).

(8)

s1 = set("forrest") s2 = set("forever")

print(s1) # Line 1

print(s1 | s2) # Line 2 print(s1 & s2) # Line 3 print(s1 - s2) # Line 4

Figure 3

(17) What is printed by Line 1 when Figure 3 is executed?

(a) {’e’, ’f’, ’o’, ’s’, ’r’, ’t’}

(b) {’forrest’}

(c) {’f’, ’o’, ’r’, ’r’, ’e’, ’s’, ’t’}

(d) set(’o’, ’e’, ’s’, ’f’, ’t’, ’r’) (e) None of the above.

(18) What is printed by Line 2 when Figure 3 is executed?

(a) {’r’, ’e’, ’o’, ’f’}

(b) {’forrest’, ’forever’}

(c) {’e’, ’f’, ’o’, ’s’, ’r’, ’t’, ’v’}

(d) set(’e’, ’f’, ’o’, ’s’, ’r’, ’t’, ’v’) (e) None of the above.

(19) What is printed by Line 3 when Figure 3 is executed?

(a) {’r’, ’e’, ’o’, ’f’}

(b) {}

(c) {’e’, ’f’, ’o’, ’s’, ’r’, ’t’, ’v’}

(d) (’e’, ’f’, ’o’, ’s’, ’r’, ’t’, ’v’) (e) None of the above.

(20) What is printed by Line 4 when Figure 3 is executed?

(a) {}

(b) {’s’, ’t’}

(c) {’rest’, ’ever’}

(d) {’e’, ’f’, ’o’, ’s’, ’r’, ’t’, ’v’}

(e) None of the above.

(9)

class NoPlayerError(Exception): # Line 1

pass # Line 2

class Target ( object ):

def __init__(self, n = 50):

self.num = n self.state = {}

def guess(self, p, n):

if p in self.state: # Line 3

self.state[p].append(self.num - n) # Line 4

else: # Line 5

self.state[p] = [self.num - n] # Line 6 return self.num - n

def turns(self, p = 0):

if p in self.state:

return(len(self.state[p])) elif p:

raise NoPlayerError else:

return(sum([len(v) for v in self.state.values()])) t = Target(10)

print (t.guess(’a’, 20)) # Line 7

t.guess(’b’,15) t.guess(’a’,30)

print (t.turns(’a’)) # Line 8

print (t.turns(0)) # Line 9

# Replace # Line 10

Figure 4

(21) Which of the following statements about Lines 1–2 in Figure 4 is true?

(a) They define a subclass of Exception—calling a method on an instance of Target can produce exceptions of this type.

(b) They define a superclass of Exception—calling a method on an instance of Target can produce exceptions of this type.

(c) They cause the program to raise an exception, which terminates the program if the shell does not find a handler matching NoPlayerError.

(d) They have no affect on the program because the suite in Line 2 does nothing.

(e) None of the above is true.

(10)

(22) What is printed by Line 7 when Figure 4 is executed?

(a) -10 (b) 20 (c) 10

(d) 0 (e) None of (a)–(d).

(23) What is printed by Line 8 when Figure 4 is executed?

(a) 3 (b) 20 (c) 1

(d) 2 (e) None of (a)–(d).

(24) What is printed by Line 9 when Figure 4 is executed?

(a) 3 (b) 20 (c) 1

(d) 2 (e) None of (a)–(d).

(25) Which of the following replacements for Line 10 will produce a NoPlayerError error (exception) when Figure 4 is executed?

(a) t.turns() (b) t.turns(’c’) (c) t.turns(’b’) (d) Target() (e) None of (a)–(d).

(26) Consider Lines 3–6 in Figure 4. Which of the following code sequences is equiva- lent?

(a) try:

self.state[p].append(self.num - n) except KeyError:

self.state[p] = [self.num - n]

(b) try:

self.state[p] = [self.num - n]

except KeyError:

self.state[p].append(self.num - n) (c) try:

self.state[p].append(self.num - n) except NoPlayerError:

self.state[p] = [self.num - n]

(d) try:

self.state[p] = [self.num - n]

except NoPlayerError:

self.state[p].append(self.num - n) (e) None of the above.

(27) Which of the following unqualified names is not in scope at Line 4 of Figure 4?

(a) self (b) p (c) NoPlayerError

(d) num (e) None of (a)–(d).

(11)

def func(a, x, y):

a[x+y] += a[x+y]

t = (5, 4, 3, 2, 1)

try: # Line 1

func(t, 2, 1) print(t) except:

print("got exception") # Line 1’

d = {’a1’:’x’, ’a2’:2, ’a3’:1.5, ’a4’:’z’}

try: # Line 2

func(d,’a’, ’3’)

print(set(d.values())) except:

print("got exception") # Line 2’

try: # Line 3

func(d, ’a’, 3)

print(set(d.values())) except:

print("got exception") # Line 3’

Figure 5

(28) What is printed by the first try-except block (Line 1 — Line 1’) when Figure 5 is executed?

(a) (5, 4, 3, 4, 1) (b) (5, 4, 6, 2, 1)

(c) 0 (d) got exception

(e) None of (a)–(d).

(29) What is printed by the second try-except block (Line 2 — Line 2’) when Fig- ure 5 is executed?

(a) {’x’, 2, 1.5, ’z’} (b) {’xz’, 3.5}

(c) {’x’, 2, 3.0, ’z’} (d) got exception

(e) None of (a)–(d).

(30) What is printed by the third try-except block (Line 3 — Line 3’) when Figure 5 is executed?

(a) {’x’, 2, 1.5, ’z’} (b) {’xz’, 3.5}

(c) {’x’, 2, 3.0, ’z’} (d) got exception

(e) None of (a)–(d).

(12)

Name AND Section:

Written: (50 points) Write correct Python code to meet the following spec- ifications. Write all of your answer neatly on the facing sheet. Indicate indentation with care.

Write a 2-dimensional vector class, called Vec. The constructor takes two op- tional numeric arguments, which indicate the x and y coordinates and which both default to 0. Your class must define operators and methods to produce the following behavior in the shell:

>>> v1 = Vec() # both coordinates default to 0

>>> print(v1) # prints as a tuple of floats, with (0.0, 0.0) # coordinates accurate to one decimal

>>> v2 = Vec(2) # y-coordinate defaults to 0

>>> print(v2) (2.0, 0.0)

>>> v3 = Vec(1.49999,1)

>>> print(v3) (1.5, 1.0)

>>> v2 + v3 # sum x-coordinates and y-coordinates (3.5, 1.0)

>>> v2 + 1 # mixed types: treat 1 as Vec(1) (3.0, 0.0)

>>> v2 # addition operations did not modify v2 (2.0, 0.0)

>>> v2 += 1 # has effect of: v2 = v2 + 1

>>> v2 (3.0, 0.0)

>>> v4 = Vec(y=1.215) # x-coordinate defaults to 0

>>> v4 (0.0, 1.2)

>>> 1.1 + v4 (1.1, 1.2)

>>> v2 == v1 # are both x-coords equal and y-coords are equal?

False

>>> v2 == 3 # mixed types: 3 is treated as Vec(3) True

References

Related documents