MockExam1
pdf
keyboard_arrow_up
School
University of Texas *
*We aren’t endorsed by this school
Course
303E
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
12
Uploaded by UltraRedPanda214
CS303E Mock Exam 1
Dr. Bill Young
§
Spring 2024
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
Page total:
/10
CS303E Mock Exam 1
2
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.
(a)
(ord(’z’) - ord(’y’)) * -4 + 2
(b)
0 // 8 + 4
(c)
math.ceil(2.718)
(d)
"Hello" + "World"
(e)
round(3.14159, 3)
(f)
(3 % 100) and not (100 % 10)
(g)
"56" + "4"
(h)
int("2.5")
(i)
60 / 3 + 4 * 2
(j)
float(10)
Page total:
/10
CS303E Mock Exam 1
3
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. Which type of error, if any, is present in the following code?
import math
x = -1
print("The number", format(x, "0.1f") + " has", \
"only complex square roots")
A. syntax error
B. runtime error
C. logic error
D. no error
4. Suppose
a
and
b
are integers. Which of the following expressions won’t result in
the comparison
(a < b)
being evaluated?
A.
True and (a < b)
B.
True or (a < b)
C.
not (a < b)
D. all of these result in the comparison being evaluated
5. Suppose that a function
f
is defined with the following header:
def f(w, x, y, z = 1):
Which of the following is NOT a legal call to the function
f
?
A.
f(1, 2, y=3)
B.
f(5, z=2, y=3, x=10)
C.
f(6, 7, 8)
D.
f(z=3, 7, 8, 9)
6. Which of the following keywords can only be used inside a function?
A.
return
B.
continue
C.
in
D.
break
7. Suppose we’ve executed the following line of code:
import math
Which of the following is a valid call to the
math
module’s natural logarithm function
which could follow the previous line?
A.
math.log(10)
B.
log(10)
C. both A and B
D. neither A nor B
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
CS303E Mock Exam 1
4
8. Which of the following is NOT an immutable type in python?
A.
int
B.
float
C.
str
D. all of these are immutable
9. Suppose you want the following loop to print out
10 8 6 4 2
.
for i in range(a, b, -2):
print(i, end=" ")
What values of
a
and
b
should you choose?
A.
a = 12
and
b = 2
B.
a = 10
and
b = 2
C.
a = 12
and
b = 0
D.
a = 10
and
b = 0
10. Assume that
a
is an integer. Three of the following boolean expressions are equiv-
alent regardless of the value of
a
. Which one is not equivalent to the others?
A.
(a < 10) or (a > 50)
B.
not (10 < a < 50)
C.
(50 < a) or (10 > a)
D.
not ((a <= 50) and (10 <= a))
11. Which of the following operators has the highest precedence?
A.
+
(addition)
B.
*
(multiplication)
C.
**
(exponentiation)
D.
%
(mod)
12. Which of the following is NOT an advantage of using functions?
A. Functions allow for streamlined reuse of code.
B. It’s easier to read and maintain code that’s divided logically into functions.
C. Dividing a program into functions allows the python interpreter to execute the
code more quickly.
D. Functions provide an abstract interface for some behavior without requiring
the caller to know the implementation details.
3
4
5
6
7
8
9
10
11
12
Page total:
/20
CS303E Mock Exam 1
5
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)
print("Greetings", "I have" + "come from", "a different", \
"planet", sep="$", end="!!!")
14. (3 points)
def a(s, x):
return s + ("!" if (x > 0) else "?")
val = "hello"
print(val, end=" ")
a(val, 2)
print(val)
15. (3 points)
# a similar type of bowling handicap
max = 300
multiplier = 0.5
average = 240
handicap = int((max - average) * multiplier)
handicap = max(handicap, 0)
print(handicap)
Page total:
/9
CS303E Mock Exam 1
6
16. (3 points)
def f(a, b):
a -= 3
b *= 2
print(a, b, end=" ")
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
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
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)
def f(a=4, b=3, c=5, d=1):
return a+b*c+d
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
Page total:
/10
CS303E Mock Exam 1
9
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:
>>> getQuadrant(-2.5, -3.0)
3
>>> getQuadrant(-0.5, 0.0)
0
>>> getQuadrant(3.0, -5.0)
4
def getQuadrant(x, y):
Page total:
/8
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
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():
Page total:
/10
CS303E Mock Exam 1
12
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:
>>> deliveryTips(5) # inputs are 0.25, 8.75, 3.56, 0.99, 5.50
$15.56
>>> deliveryTips(2) # inputs are 1.23, 2.34
$3.57
>>> deliveryTips(4) # inputs are 0, 0.50, 10.90, 120
$12.00
def deliveryTips(numDeliveries):
Page total:
/8
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help