Sample Final
Sample Final
XXX
Sample for Practice only
There are two sections, each will be worth 60 points. The maximum score on the test will be 120.
It is essential that you PUT YOUR NAME ON ALL TEST MATERIALS. It can be difficult identify the author of an
unsigned test and it would be better to avoid this problem.
Note: This sample test approximates the format of the final. However, the actual final may have a different
number of questions, a different proportion of easy/difficult questions, etc.
Section 1: Below you will find several Pieces of Code followed by a question and a place to fill in an answer. If there
is a bug in the code, write ERROR in the space provided. Otherwise, answer the question. Sample questions 1 and 2
(with the answers filled in) are followed by 7 real questions. Please Only Answer 5 out of 7 questions. Questions 1 to
4 are easier than questions 5 to 7. So many people will leave out two from the list: 5, 6 or 7. If you answer more than
5 questions, please cross out answers or otherwise indicate which questions you would like to be counted. If you don’t
indicate which ones you want, I will just pick the first 5.
Note for sample test: Several places include the words extra practice. On the test, these words would say extra credit
instead.
Sample 1:
total = 0
Sample 2:
print(instructions[6])
Question 1:
total = 0
def spell_out(word):
for character in word[:-1]:
print(character, end="+")
print(word[-1])
spell_out(’Establishment’)
Question 3:
import re
phonenumbers = {’Rachel\’s Rose Shop’: ’333-400-3423’, ’Donald\’s Daisies’: \
’333-121-2121’, ’Pat\’s Petunias’: ’333-434-5500’, \
’Samuelson\’s Sunflowers’: ’333-667-9689’}
import os
def enter_line_in_dictionary(line):
clean_line = line.strip(os.linesep)
line_list = clean_line.split(’|’)
for index in range(len(line_list)):
line_list[index] = line_list[index].strip(’ ’)
quotation_dictionary[line_list[0]] = line_list[1]
def read_in_quotations(file):
global quotation_dictionary
quotation_dictionary = {}
instream = open(file,’r’)
for line in instream:
enter_line_in_dictionary(line)
instream.close()
read_in_quotations(’list-of-quotations.txt’)
def add_ed(verb):
if verb in ed_exceptions:
return(ed_exceptions[verb])
elif verb[-1] == ’e’:
return(verb+’d’)
elif (len(verb) > 2) and (verb[-1] in ’bdglmnprt’) \
and (verb[-2] in ’aeiou’) and (not (verb[-3] in ’aeiou’)):
return(verb+verb[-1]+’ed’)
elif (len(verb) > 2) and (verb[-1] == ’y’) and (not (verb[-2] in ’aeiou’)):
return(verb[:-1]+’ied’)
else:
return(verb+’ed’)
sails(5,3,3)
Question: What is printed out? (It can be a little sloppy, as long as I understand it.)
Answer:
Question 7:
import turtle
my_screen = turtle.Screen()
my_turtle = turtle.Turtle()
super_regular_shape(4,100)
Question: Draw the shape that the turtle draws. (The drawing should be approximate.) Extra Practice: What shapes are
Question 8: Write a ’Madlibs’ program, given the text below. Your program should first ask the user for instances of
various parts of speech or semantic classes (person name, verb, noun, ...). The program should replace the appropriate
words in the text with the user’s words. If a word in the original text is repeated, the replaced word should repeat as well.
The text follows:
Mary had a little lamb, little lamb, little lamb
Mary had a little lamb, whose fleece was white as snow
For example, suppose you select: Mary, little, lamb, fleece, white, and snow as words that should be replaced. Then a
user’s choices may yield the following result:
Herman had a blue pencil, blue pencil, blue pencil
Herman had a blue pencil, whose lead was red as fire
Question 9: Write a program that generates 5 random playing cards (a poker hand). Make sure that the program does
not generate the same card twice. For example, it should not be possible for the hand to contain two instances of the
King of Hearts. Remember there are a total of 52 different cards: 13 different faces times 4 different suits.
The face cards include cards numbered 2 through 9, as well as Ace, Jack, Queen and King (for a total of 13).
The four suits are: Clubs, Diamonds, Hearts and Spades.
Write an additional function to evaluate the hand as follows:
• If all of the cards in the hand have the same suit, then return ’Flush’
Note that this is a minor simplification of the real problem. You do not need to identify: high card, straights,
straight-flushes, two-pairs, or a full-house. However, you can add these for extra practice.
The required definitions for extra practice are:
• For purposes of ordering cards, it is assumed that: J,Q and K are equivalent to 11, 12 and 13. Furthermore, A can
either be treated as 1 or 14.
• a straight is five consectutively numbered cards, e.g., 2, 3, 4, 5, 6 or the sets: {A,2,3,4,5}, {9,10,J,Q,K}, {10,J,Q,K,A}.
• a Straight-Flush is a hand that simultaneously meets the definition for ’Straight’ and ’Flush’, e.g., {A,2,3,4,5}
where all the cards have the suit ’Hearts’.
• two pairs refer to when there are two sets of pairs, e.g., a hand that consists of two instances of 2 and two instances
of K and one instance of something else.
• a full-house is a hand that includes one instance of 3-of-a-kind and one pair, e.g., {3,3,3,4,4}.
• a hand that does not meet any of these requirements always has one card that is higher than the others (as per the
first bullet). This is the high card.
Question 10: Write a program that tries to guess a number from 1 to 1000. The player should (secretly) select a number
between 1 and 1000 and not tell the computer program. Then the program will make a series of guesses. The player
should indicate whether each of the program’s guesses is: correct, too high or too low. The program’s guess are counted.
The game ends when the program’s guesses correctly.
The program should use the following strategy to guess the correct number:
1. It should keep selecting an integer that is approximately in the middle of the highest and lowest currently possible.
Eventually, this should result in it guessing the number correctly.
2. The initial low and high that the computer is assuming should be set to 1 and 1000.
3. If the user indicates that the guess is too high, the computer should reset the current high to 1 below its guess.
4. If the user indicates that the guess is too low, the computer should reset the current low to 1 above its guess.