MockExam1 (Cs303e)
MockExam1 (Cs303e)
Name: EID:
Read the questions carefully, and answer each question in the space provided. If you like,
you can use scratch paper to do your work, but copy your answers neatly and legibly
onto the test paper. Only answers recorded on the test paper will be graded. Don’t write
in the spaces marked “Page Total” at the bottom of each page. Note: points as listed on
the exam sum to 100, but your mock exam grade will be scaled to a 10-point scale.
1. (10 points: 1 point each) The following are true/false questions. Write either T
or F in the boxes at the bottom of page 1. If there’s any counterexample, it’s
false.
(a) To run a Python file in batch mode, you will need to import the corresponding
module from within the Python interpreter command loop.
(b) 3rd is a valid variable name.
(c) If x is a floating point number, then int(x) will return x rounded to the nearest
integer.
(d) The values 3 and "3" are equivalent representations of the number three, i.e.
after the assignments x = 3 and y = "3", x and y would hold exactly the same
value.
(e) Basic arithmetic operations on floats like addition, subtraction, multiplication,
and division are only approximate in python, meaning that for certain calcu-
lations the results may have some small error.
(f) There is no way to include a backslash (i.e. this character → \) in a string,
since the backslash is reserved for escape sequences.
(g) If x is a variable holding a float, then the statement print(round(x, 2)) will
display the value of x with exactly two digits after the decimal point.
(h) The expression bool(None) is equivalent to False.
(i) If you have an if-elif statement, then you must also have a corresponding else
branch.
(j) If a and b are booleans, the boolean expressions not (a and b) and
not a and b may not evaluate to the same value.
a b c d e f g h i j
2. The following 10 questions (worth 1 point each) require you to evaluate a Python
expression in the left hand column. For each question, write what the expression
evaluates to on the provided line. If evaluation results in an error, write “error”;
you don’t have to identity the specific type of error. You may assume the math
library has been imported.
You must show a value of the appropriate type. For example, 7.0 rather
than 7 for a float and "7" instead of 7 for a string. Answers that do not
indicate the data type correctly are wrong.
(b) 0 // 8 + 4
(c) math.ceil(2.718)
(e) round(3.14159, 3)
(h) int("2.5")
(i) 60 / 3 + 4 * 2
(j) float(10)
Questions 3–12 are multiple choice. Each counts 2 points. Write the letter of the
BEST answer in the box on the next page. Please write your answer in
UPPERCASE. Each problem has a single answer.
3 4 5 6 7 8 9 10 11 12
Questions 13–20 require you to trace the behavior of some Python code and identify the
output of that code. For each question, write the output for the code segment in the
provided box. If executing the code gives an error, write “ERROR” in the box; you’re
not required to identify what type of error occurs. Don’t worry about whether it goes to
the next line at the end.
13. (3 points)
14. (3 points)
val = "hello"
print(val, end=" ")
a(val, 2)
print(val)
15. (3 points)
Page total: /9
CS303E Mock Exam 1 6
16. (3 points)
x = -2
y = 3
f(x, y)
f(x, y)
print(x, y)
17. (3 points)
x = 24
while x > 1:
print(x, end=" ")
if x % 2 == 0:
x = x // 2
else:
x = 3 * x + 1
18. (3 points)
def g(t):
print(t * 3, end=" ")
return t + 1
z = 2
print(g(2 * z), g(z))
Page total: /9
CS303E Mock Exam 1 7
19. (3 points)
x = 36
for d in range(1, x + 1):
if x % d == 0:
print(d, end=" ")
20. (3 points)
print(f(5, 2))
Page total: /6
CS303E Mock Exam 1 8
21. (10 points: 1 point each) The following questions require you to write a Python
expression that returns the indicated value. You can assume that any modules
you need have been imported. Note that this asks for a single, one-line expression
for each question, not a longer program fragment. So, you should not have any
assignments, loops, or if statements.
(a)
the result of a standard die roll (i.e. a uniformly random integer between 1
and 6, inclusive)
(b)
for a given integer n, a boolean indicating whether n is divisible by 3 but not 5
(c)
for a given integer n having at least two digits, the digit in the tens place in n,
i.e. the second digit from the right
(d)
for given floats a, b, and c, the average of these three values
(e)
for given floats a, b, and c, the range of these three values, i.e. the difference
between the largest and smallest value
(f)
for given integers x and y, the remainder when x is divided by y
(g)
a user-provided input, converted to a floating-point value, after prompting the
user with "Enter a number"
(h)
for a given lowercase letter of the alphabet ch between ’a’ and ’y’, the letter
that comes after it in the alphabet (also lowercase)
(i)
for a given float num, num as a string, displayed to three decimal places
(j)
for a given float num, the smallest integer at least as big as num
22. (8 points) Complete the getQuadrant function which takes in float parameters
x and y representing the x and y coordinates of a point in the Cartesian plane.
The function should return which quadrant the specified point is in. Recall that
the plane is divided into four quadrants: quadrant 1 is the upper right quadrant,
quadrant 2 is the upper left quadrant, quadrant 3 is the lower left quadrant, and
quadrant 4 is the lower right quadrant. If the given point lies on the x-axis and/or
y-axis, the function should return 0.
Here are some example calls to the function:
Page total: /8
CS303E Mock Exam 1 10
23. (10 points) You’re playing a video game similar to a particular Nintendo property
which will not be named. In this game there are three different things you can
encounter in a level: a small coin box which gives you 20 coins, a large coin box
which gives you 50 coins, and a goomba which steals all the coins you have. You start
with 0 coins, and each time your coin count gets to 100 or more, you immediately
lose 100 coins and gain an extra life. Given a sequence of things you encounter in
a level, you’d like to know how many extra lives you’ll gain and how many coins
you’ll end up with.
For example, suppose the sequence is large box, large box, small box, goomba, small
box, large box, large box, small box, large box, goomba, goomba, end. You’ll first
gain 50 coins, then 50 more (total of 100, so you gain a life and now have 0 coins),
then 20 more (total of 20), then lose all 20 coins (total of 0), then gain 20 coins
(total of 20), then 50 more (total of 70), then 50 more (total of 120, so you gain
another life and now have 20 coins), then 20 more (total of 40), then 50 more (total
of 90), then lose all 90 coins (total of 0), then lose all 0 coins (total of 0), and then
the level is over. In total you gained 2 extra lives and ended with 0 coins.
Complete the extraLives function that reads in a sequence of things you encounter
in a level, provided one-per-line as user input. Each line will have either ”small box”,
”large box”, or ”goomba”, except for the last which will have the word ”end”. The
function should determine and print how many extra lives you gain and how many
coins you end the level with. See the example calls for how this should be formatted.
Here is an example call to the function, assuming the input sequence is large box,
large box, small box, goomba, small box, large box, large box, small box, large box,
goomba, goomba, end, as in the explained example:
>>> extraLives()
extra lives: 2
coins: 0
Here is another example call to the function, assuming the input sequence is small
box, large box, small box, goomba, small box, large box, large box, goomba, small
box, large box, end:
>>> extraLives()
extra lives: 1
coins: 70
CS303E Mock Exam 1 11
def extraLives():
24. (8 points) You’re a delivery driver, and you have the following agreement with your
employer: whenever a customer tips less than a dollar, the company pays you the
difference between the given tip and a dollar. For example, if the customer only
tips you 25 cents, then the company will pay you an additional 75 cents for that
delivery. In exchange, whenever a customer tips more than five dollars, you only get
to keep five dollars of it, and the rest must be given to the company. For example,
if a customer tips you $8.75, you must pay $3.75 of that to the company.
Complete the deliveryTips function which takes in a positive integer parameter
numDeliveries denoting the number of deliveries. The function should read in the
tip amounts (non-negative floats) for each of your deliveries, provided one-per-line
as user input, and print out the total net amount of money you made from tips.
Display the value to two decimal places and with a dollar sign.
Here are some example calls to the function:
def deliveryTips(numDeliveries):
Page total: /8