← return to practice.dsc10.com
Welcome! The problems shown below should be worked on on
paper, since the quizzes and exams you take in this course will
also be on paper. You do not need to submit your solutions anywhere.
We encourage you to complete this worksheet in groups during an
extra practice session on Friday, January 12. Solutions will be posted
after all sessions have finished. This problem set is not designed to
take any particular amount of time - focus on understanding concepts,
not on getting through all the questions.
Suppose we have imported the math module using
import math. Consider the nested expression below:
int(math.sqrt(math.pow(4 * 2 ** 4, min(9 % 4, 9 / 4, 9 - 4))))
How many function calls are there in this expression? How many arguments does each function have?
Answer: 4 function calls: one argument for
int(), one for math.sqrt(), two for
math.pow(), three for min().
There are four function calls. One is a call to the type-conversion
function int(), which takes one argument. Another is a call
to math.sqrt(), which takes one argument. Another is a call
to math.pow(), which takes two arguments. Finally is a call
to the built-in function min(), which in this case takes
three arguments, but generally can take two or more arguments.
What does this expression evaluate to?
Answer: 8
For nested evaluation, it is helpful to work from the inside out.
Let’s evaluate some arithmetic expressions first. 9 % 4
evaluates to 1 because when we divide 9 by
4, there is a remainder of 1. Additionally,
9 / 4 evaluate to 2.25, and 9 - 4
evaluates to 5. Starting with the inner most function call,
we see min(9 % 4, 9 / 4, 9 - 4) is equiavlent to
min(1, 2.25, 5) which evaluates to 1.
The next-most inner function call is the call to
math.pow() which takes two arguments: a number for the
base, and a number for the exponent. We’ve already evaluated the
exponent, but we need to evaluate the base of 4 * 2 ** 4.
Using the order of operations, we know we need to evaluate the exponent
first. So 4 * 2 ** 4 is equivalent to 4 * 16
or 64.
Therefore,
math.pow(4 * 2 ** 4, min(9 % 4, 9 / 4, 9 - 4)) simplifies
to math.pow(64, 1), which Python evaluates to be
64.0, a float.
Next, math.sqrt(64.0) evaluates to 8.0.
Finally, the type conversion function int(8.0) evaluates to
8.
Suppose we have imported the math module using
import math. Consider the nested expression below:
int(math.sqrt(math.pow(4 * 2 ** 4, min(9 % 4, 9 / 4, 9 - 4))))
How many function calls are there in this expression? How many arguments does each function have?
Answer: 4 function calls: one argument for
int(), one for math.sqrt(), two for
math.pow(), three for min().
There are four function calls. One is a call to the type-conversion
function int(), which takes one argument. Another is a call
to math.sqrt(), which takes one argument. Another is a call
to math.pow(), which takes two arguments. Finally is a call
to the built-in function min(), which in this case takes
three arguments, but generally can take two or more arguments.
What does this expression evaluate to?
Answer: 8
For nested evaluation, it is helpful to work from the inside out.
Let’s evaluate some arithmetic expressions first. 9 % 4
evaluates to 1 because when we divide 9 by
4, there is a remainder of 1. Additionally,
9 / 4 evaluate to 2.25, and 9 - 4
evaluates to 5. Starting with the inner most function call,
we see min(9 % 4, 9 / 4, 9 - 4) is equiavlent to
min(1, 2.25, 5) which evaluates to 1.
The next-most inner function call is the call to
math.pow() which takes two arguments: a number for the
base, and a number for the exponent. We’ve already evaluated the
exponent, but we need to evaluate the base of 4 * 2 ** 4.
Using the order of operations, we know we need to evaluate the exponent
first. So 4 * 2 ** 4 is equivalent to 4 * 16
or 64.
Therefore,
math.pow(4 * 2 ** 4, min(9 % 4, 9 / 4, 9 - 4)) simplifies
to math.pow(64, 1), which Python evaluates to be
64.0, a float.
Next, math.sqrt(64.0) evaluates to 8.0.
Finally, the type conversion function int(8.0) evaluates to
8.
After a trip to the zoo, Anthony wrote the following block of code.
zebra = 5
lion = 4
cow = 1
zebra = zebra * 2
lion = abs(cow - lion)
zebra = zebra + lion ** 2
cow = (zebra + lion) / 2 * lionAfter running the above block of code, what is the value of
cow?
Answer: 33.0
The average score on this problem was 60%.
Consider the following four assignment statements.
bass = "5"
tuna = 2
sword = ["4.0", 5, 12.5, -10, "2023"]
gold = [4, "6", "CSE", "doc"]What is the value of the expression bass * tuna?
Answer: "55"
The average score on this problem was 48%.
Which of the following expressions results in an error?
int(sword[0])
float(sword[1])
int(sword[2])
int(sword[3])
float(sword[4])
Answer: int(sword[0])
The average score on this problem was 51%.
Which of the following expressions evaluates to
"DSC10"?
gold[3].replace("o", "s").title() + str(gold[0] + gold[1])
gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))
gold[3].replace("o", "s").upper() + str(gold[1] + int(gold[0]))
gold[3].replace("o", "s").title() + str(gold[0] + int(gold[1]))
Answer:
gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))
The average score on this problem was 92%.