Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 1026 – Final Exam Practice Questions

Part 1) Determine what would be printed out when running each code fragment.

1.   mylist = [12, 3, 15, 24, 16, 7, 13]

for m in mylist:

if m <= len(mylist):

print("s", end="")

else:

print("b", end="")

a.   bsbbbsb

b.   sbsssbs

c.   sbsbsbs

d.   bsbsbsb

e.   sssssss

2.   file = open("jinglebells.txt", "r")       # see file below

for i in range(3):

line = file.readline()

line = file.readline()

print(line)

 

a.   Jingle bells, jingle bells

b.   Jingle all the way

c.   Oh, what fun it is to ride

d.   In a one-horse open sleigh, hey!

e.   Jingle bells, jingle bells Jingle all the way

Oh, what fun it is to ride

In a one-horse open sleigh, hey!

3.   letters = {}

file = open("jinglebells.txt", "r")     # see file from from Q2

for line in file:

line = line.strip()

x = line[0]

if x not in letters:

letters[x] = 1

else:

letters[x] += 1

print(letters)

a.   {'Jingle': 2, 'Oh': 1, 'In': 1}

b.   {'Jingle': 4, 'Oh': 2, 'In': 2}

c.   {'Jingle': 6, 'bells': 4, 'all': 2, ..., 'sleigh': 2}

d.   {'J': 2, 'O': 1, 'I': 1}

e.   {'J': 4, 'O': 2, 'I': 2}

4.   x = {5, 8, 12, 7, 2, 5, 9, 7, 2, 4, 12} print(len(x))

a.   5

b.   7

c.   8

d.   11

e.   12

5.   try:

for t in range(5,-5,-1):

r = 10 / t

print(t, end=", ")

except:

print("error")

a.   5, 4, 3, 2, 1, error

b.   5, 4, 3, 2, 1, 0, error

c.   5, 4, 3, 2, 1, 0, -1, -2, -3, -4,

d.   5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5

e.   5, 4, 3, 2, 1, 0, -1, -2, -3, -4, error

Use the following classes for the next several questions.

class Tree:

def __init__(self, type):

self._type = type

def __repr__(self):

return self._type + " tree"

def __eq__(self, other):

if isinstance(other,Tree):

return True

else:

return False

class ChristmasTree(Tree):

def __init__(self):

super().__init__("Fir")

def __repr__(self):

return "Christmas tree"

def addOrnament(self):

print("Added ornament")

6.   t1 = Tree("Oak") print(t1)

a.   Oak tree

b.   Oak

c.   Christmas tree

d.   None

7.   t2 = ChristmasTree() print(t2)

a.   Oak tree

b.   Oak

c.   Christmas tree

d.   None

8.   t3 = Tree("Birch")  t4 = ChristmasTree() print(t3 == t4)

a.   True

b.   False

c.    None

d.   This would cause an error

9.   t5 = Tree("Christmas") t5.addOrnament()

a.   True

b.   False

c.   Added ornament

d.   This would cause an error

10. def stuff(p):   total = 0   while p > 1:

p = p / 10

total += 1

print(total, end="")

stuff(1000)

a.   100010010

b.   1000100101

c.    123

d.   1234

e.   This would cause an infinite loop

11. s = "Hello world" t = ""

while len(s) > 0:

p = s[0]

s = s[1:]

t = p + t

print(t)

a.   Hello world

b.   dlrow olleH

c.    Hello

d.   world

e.   This would cause an infinite loop

12. mylist = ["a", "b", "c"] * 3 + ["d", "e"] * 2

for x in mylist:

print(x, end='')

a.   a3b3c3d2e2

b.   aaabbbcccddee

c.   abcabcabcdede

d.   abcdeabcdeabcde

e.   abcabcabcabcdeabcde

13. try:

f = open("fakefile.txt", "r") # this is not a real file except TypeError:

print("T", end="")

except IOError:

print("I", end="")

finally:

print("F")

a.   TF

b.   IF

c.   TIF

d.   I

e.   F

14. a = {14, 25, 8, 11, 20} b = {21, 27, 25, 21, 21} print(a.union(b))

a.   25

b.   8, 11, 14, 20, 21, 25, 27

c.   8, 11, 14, 20

d.   21, 27

15. a = {14, 25, 8, 11, 20} b = {21, 27, 25, 21, 21} print(a.intersection(b))

a.   25

b.   8, 11, 14, 20, 21, 25, 27

c.   8, 11, 14, 20

d.   21, 27

16. a = {14, 25, 8, 11, 20} b = {21, 27, 25, 21, 21} print(a.difference(b))

a.   25

b.   8, 11, 14, 20, 21, 25, 27

c.   8, 11, 14, 20

d.   21, 27

17. data = [3, 9, 11, 4, 8, 5, 13]

for t in range(len(data)):

if t % 2 == 0:

print(t, end=" ")

a.   0 2 4 6

b.   1 3 5

c.   4 8

d.   3 9 11 5 13

Use the following classes for the next several questions.

class Reindeer:

def __init__(self, name):

self._name = name

def __repr__(self):

if self._name == "":

return "Reindeer with no name"

elif self._name == "Rudolph":

return "Rudolph the Red-Nosed Reindeer!"

else:

return "Reindeer named " + self._name

18. comet = Reindeer("Comet") print(comet)

a.   Reindeer named Comet

b.   Reindeer with no name

c.    Rudolph the Red-Nosed Reindeer!

d.   Comet

19. vixen = Reindeer("") print(vixen)

a.   Reindeer named Vixen

b.   Reindeer with no name

c.    Rudolph the Red-Nosed Reindeer!

d.   Vixen

20. rudolph = Reindeer("rudolph") print(rudolph)

a.   Reindeer named rudolph

b.   Reindeer with no name

c.    Rudolph the Red-Nosed Reindeer!

d.   Rudolph

Part 2) Determine what lines of code would be required to complete each code fragment.

21. The following function, checkOrdered(), takes in a list of numbers and has to check if    those numbers are ordered in ascending order and return a boolean variable indicating whether or not the list is sorted. For example, checkOrdered([1, 3, 7]) should return True, and checkOrdered([4, 8, 5]) should return False. Three lines in this function are    missing. From the options listed below, determine which lines are needed to complete the function.

def checkOrdered(data):

prev = data[0]

_______________                        # blank 1

_____________                  # blank 2

return False

__________                        # blank 3

return True

Possible Lines

i.           for d in data:

ii.          for d in data[1:]:

iii.         while d in data:

iv.         if prev > d:

v.          if prev < d:

vi.         while prev > d:

vii.        d = prev

viii.       prev = d

ix.         prev += 1

a.   i, iv, vii

b.   ii, iv, viii

c.   i, v, ix

d.   iii, v, vii

e.   ii, v, viii

22. The following function, findMaximum(), takes in a dictionary in which each value is a list of positive numbers. Note that each list could be a different length. The function is supposed to find the highest number found in any of the dictionary's lists and return      that highest number. Three lines in this function are missing. From the options listed below, determine which lines are needed to complete the function.

def findMaximum(dictionary):

highest = -1

_______________                           # blank 1

_____________                     # blank 2

for d in alist:

if d > highest:

__________        # blank 3

return highest

Possible Lines

i.           for i in dictionary:

ii.          for i in dictionary.values()

iii.         for i in dictionary.items()

iv.         alist = dictionary[highest]

v.          alist = dictionary[i]

vi.         alist = len(dictionary)

vii.        d = highest

viii.       highest = alist

ix.         highest = d

a.   ii, v, vii

b.   i, iv, viii

c.   ii, vi, viii

d.   i, v, ix

e.   iii, iv, ix