Test Bank For Python Programming in Context 3rd Edition
Test Bank For Python Programming in Context 3rd Edition
Multiple Choice
1. In order to use the sqrt() function, you must use which of the following Python statements?
A) use math;
B) import math;
C) math::sqrt();
D) return sqrt();
Ans: B
Ahead: 2.2
Subject: Ch 2
Complexity: Easy
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
Reference: Case Study 1
4. Refer to the session in the accompanying case study. Which line uses the math module?
A) innerAngleB = 360.0 / numSides
B) oneHalfSideS = [Link]([Link](halfAngleA))
C) pi = polygonCircumference / 2
D) sideS = oneHalfsideS * 2
Ans: B
Ahead: 2.4.1
Subject: Ch 2
Complexity: Easy
5. Refer to the session in the accompanying case study. How many sides does the polygon in this
approximation have?
A) 2
B) 8
C) 64
D) pi
Ans: B
Ahead: 2.4.1
Subject: Ch 2
Complexity: Easy
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
D) quit
Ans: A
Ahead: 2.4.2
Subject: Ch 2
Complexity: Easy
>>> acc = 0
>>> for x in range(1, 6):
acc = acc + x
>>> acc
15
8. Refer to the session in the accompanying case study. Which of the following is the
initialization statement?
A) acc
B) for x in range(1, 6):
C) acc = acc + x
D) acc = 0
Ans: D
Ahead: 2.5
Subject: Ch 2
Complexity: Moderate
9. Refer to the session in the accompanying case study. What type of variable is acc?
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
A) Accumulator variable
B) Range variable
C) Initialization variable
D) Timer variable
Ans: A
Ahead: 2.5
Subject: Ch 2
Complexity: Easy
10. What is the name of the formula that calculates pi using the following equation?
/4 = 1/1 – 1/3 + 1/5 – 1/7 + 1/9 …
A) Archimedes approach
B) Leibniz formula
C) Wallis formula
D) Monte Carlo simulation
Ans: B
Ahead: 2.5,2
Subject: Ch 2
Complexity: Easy
11. When calculating pi using the Wallis formula, the accumulator variable:
A) must be initialized to zero.
B) is increased by two each iteration.
C) must be initialized to one.
D) is multiplied by two each iteration.
Ans: C
Ahead: 2.5.3
Subject: Ch 2
Complexity: Moderate
12. The ____ uses probability and random behavior to calculate pi.
A) Archimedes approach
B) Leibniz formula
C) Wallis formula
D) Monte Carlo simulation
Ans: D
Ahead: 2.6
Subject: Ch 2
Complexity: Easy
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
13. To create a random number in Python, use the ____ function.
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
Ans: B
Ahead: 2.6
Subject: Ch 2
Complexity: Moderate
14. The relational operator that means “not equal” is represented with which operator in Python?
A) ==
B) !=
C) <=
D) >=
Ans: B
Ahead: 2.6.1
Subject: Ch 2
Complexity: Easy
1. if <condition>:
2. if <condition>:
3. <statements>
4. else:
5. <statements>
6. else:
7. if <condition>:
8. <statements>
9. else:
10. <statements>
15. Refer to the code in the accompanying case study. Under what circumstances are the
statements on line 10 executed?
A) The condition in line 1 is false, and the condition in line 7 is false.
B) The condition in line 1 is false, and the condition in line 7 is true.
C) The condition in line 1 is true, and the condition in line 7 is false.
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
D) The condition in line 1 is true, and the condition in line 7 is true.
Ans: A
Ahead: 2.6.3
Subject: Ch 2
Complexity: Moderate
True or False
1. The parameters passed to the print function are, by default, separated by a space when printed.
Ans: True
Ahead: 2.4.2
Subject: Ch 2
Complexity: Easy
2. The code in the accompanying case study 2 (see below) is used to calculate a running product.
>>> acc = 0
>>> for x in range(1, 6):
acc = acc + x
>>> acc
15
3. When using the Wallis function, the larger the value of the parameter passed into the function,
the less accurate the result. Therefore, wallis(100) will be more accurate than
wallis(10000).
Ans: False. The larger the value, the more accurate the result.
Ahead: 2.5.3
Subject: Ch 2
Complexity: Easy
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
Ans: True
Ahead: 2.6.3
Subject: Ch 2
Complexity: Easy
5. To create a drawing window, use the Screen constructor contained in the turtle module.
Ans: True
Ahead: 2.6.5
Subject: Ch 2
Complexity: Easy
Matching
Short Answer
1. Refer to the session in the accompanying Case Study 1. Explain, in general terms, how to
modify this series of statements in such a way that you would be able to change the number of
sides and try the calculation again without needing to retype all of the statements.
Ans: One way to accomplish this is to use abstraction. Abstraction allows us to think about a
collection of steps as a logical group. In Python, we can define a function that not only serves as
a name for a sequence of actions but also returns a value when it is called. We have already seen
this type of behavior with the sqrt function. When we call sqrt(16), it returns 4.
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
Ahead: 2.4.2
Subject: Ch 2
Complexity: Moderate
2. Refer to the session in the accompanying Case Study 2. Explain what pattern this code
implements and why it is useful.
Ans: The code implements a common problem-solving pattern known as the accumulator
pattern. This common pattern comes up often. Your ability to recognize the pattern and then
implement it will be especially useful as you encounter new problems that need to be solved. As
an example, consider the simple problem of computing the sum of the first five integer numbers.
Of course, this is quite easy because we can just evaluate the expression 1+2+3+4+5. But what if
we wanted to sum the first ten integers? Or perhaps the first hundred? In this case, we would find
that the size of the expression would become quite long. To remedy this, we can develop a more
general solution that uses iteration.
Ahead: 2.5.1
Subject: Ch 2
Complexity: Moderate
3. What are some patterns to keep in mind when implementing the Leibniz formula with Python?
Ans:
All the numerators are 4.
The denominators are all odd numbers.
The sequence alternates between addition and subtraction.
Ahead: 2.5.2
Subject: Ch 2
Complexity: Easy
4. Explain how a Monte Carlo simulation can be used to calculate the value of pi.
Ans: Pretend that we are looking at a dartboard in the shape of a square that is 2 units wide and 2
units high. A circle has been inscribed within the square so that the radius of the circle is 1 unit.
Now assume that we cut out the upper right-hand quadrant of the square. The result will be a
square that is 1 unit high and 1 unit wide with a quarter-circle transcribed inside. This piece is
what we will use to “play” our simulation. The area of the original square was 4 units. The area
of the original circle was πr2 = π units since the circle had a radius of 1 unit. After we cut the
upper-right quarter, the area of the quarter-circle is π/4 and the area of the entire quarter square is
1.
The simulation will work by randomly “throwing darts” at the dartboard. We will assume that
every dart will hit the board but that the location of that strike will be random. It is easy to see
that each dart will hit the square, but some will also land inside the quarter-circle. The number of
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
darts that hit inside or outside the quarter-circle will be proportional to the areas of each. More
specifically, the fraction of darts that land inside the quarter-circle will be equal to π/4.
Ahead: 2.6
Subject: Ch 2
Complexity: Moderate
5. Explain the difference between the “=” in line 1 and the “==” in line 2 in the code sample
below:
1. >>> apple = 25
2. >>> apple == 25
3. True
Ans: On line 1, the variable apple is assigned the value 25 and is then used in an equality
comparison in line 2. It is important to distinguish between the use of the assignment operator, =,
and the equality operator, ==.
Ahead: 2.6.1
Subject: Ch 2
Complexity: Moderate
6. Explain the term short-circuit evaluation of Boolean expressions. Use an example to illustrate
what this means.
Ans: This means that Python evaluates only as much of the expression, from left to right, as it
needs to in order to determine if the expression is True or False. For example, consider the
Boolean expression 3 < 7 or 10 < 20. Because 3 < 7 is True, we know at that time
that the full expression is True. Python does not need to evaluate the expression 10 < 20
since it would not matter whether 10 < 20 is True or False. Similarly, for the Boolean
expression 10 > 20 and 3 < 7, Python would need to evaluate only the 10 > 20 part of
the expression. Because 10 > 20 is False, the full expression will be False regardless of
whether 3 < 7 is True or False.
Ahead: 2.6.2
Subject: Ch 2
Complexity: Difficult
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company
Complexity: Easy
8. Refer to the code in the accompanying Case study 3. Why is this section of code referred to as
“nested selection”?
Ans: The result of this structure is to decide between four groups of statements. If the outer
condition is True, then the first nested ifelse is performed. If the nested condition is also
True, then the first group of statements is performed. However, if the nested condition is
False, the second group of statements is executed. Likewise, if the outer condition is False,
the second nested ifelse will be performed. In this way, we are able to decide between four
groups of statements by using the results of two conditions.
Ahead: 2.6.3
Subject: Ch 2
Complexity: Moderate
9. Explain how to modify the coordinate system of the drawing window when using the turtle
module.
Ans: To modify the coordinate system of the drawing window, we can use a Screen method
called setworldcoordinates. This method will take four pieces of information: (1) the x
-coordinate of the lower-left corner, (2) the y-coordinate of the lower-left corner, (3) the x
-coordinate of the upper-right corner, and (4) the y-coordinate of the upper-right corner. These
two points will denote the coordinates of the lower-left and upper-right corners of the window
used by the drawing turtle: [Link](-2, -2, 2, 2).
Ahead: 2.6.5
Subject: Ch 2
Complexity: Moderate
Copyright © 2021 by Jones & Bartlett Learning, LLC, an Ascend Learning Company