Testbank Chapter 2
Testbank Chapter 2
1. To store a value for later use in Python, the programmer needs to create a:
A. number
B. character
C. variable Answer
D. boolean
Title What is the term used to store a value for later use in a Python program?
Section 2.1 Variables
4. What is the value of the variable num after the following code snippet?
num = 5
num2 = 6
num = num2 + 3
A. 5
B. 9 Answer
C. 8
D. 11
Title What is the value of the variable num after the following code statement?
Section 2.1 Variables
B. num = num2 + 10
C. num2 + 10 = num
D. num + 10 = num2
Title What is the right way to assign the value of num + 10 to num2?
Section 2.1 Variables
9. What is a variable called that should remain unchanged throughout your program?
A. character: 'a'
B. string: "hello"
C. number: 1234
D. pi: 3.14159 Answer
Title Python naming conventions for variables
Section 2.1 Variables
11. Which of the following variable names follows the Python naming convention for constants?
A. maxSize
B. MAXSIZE Answer
C. MAX SIZE
D. max_size
12. Why is it important to follow Python naming standards for variables representing constants?
A. *
B. /*
C. # Answer
D. >
14. Which of the following is an appropriate variable name to represent the number of pencils in a pack?
A. NUM_PENCILS = 12 Answer
B. numPencils = 12
C. NUMpencils = 12
D. numpencils = 12
Title Which of the following is an appropriate variable name to represent the number of pencils in a
pack?
Section 2.1 Variables
15. Which line of code creates a variable named x and initializes it to the integer 5?
A. x = 5.0 Answer
B. x = 5
C. x = '5'
D. x = "5"
Title Which line of code creates a variable named x and initializes it to the integer 5?
Section 2.1.2 Number Types
17. Which of the following names is the best for a constant variable holding the price of a can of soda?
A. soda_price
B. soda-price
C. SodaPrice
D. SODA_PRICE Answer
Title Which of the following names is the best for a constant variable holding the price of a can of
soda?
Section 2.1.4 Constants
A. !
B. @
C. # Answer
D. $
19. Which of the following statements correctly multiplies num1 times num2?
Title Which of the following statements correctly multiplies num1 times num2?
Section 2.2 Arithmetic
20. Which of the following statements correctly calculates the average of three numbers: num1, num2, and
num3?
Title Which of the following statements correctly multiplies num1 times num2?
Section 2.2 Arithmetic
21. Which of the following guidelines will make code more explanatory for others?
22. What is the value of result after the following code snippet?
num1 = 10
num2 = 20
num3 = 2
result = num1 / num2 / num3
print(result)
A. 1
B. 0
C. The code has an error
D. 0.25 Answer
Title What is the value of result after the following code snippet?
Section 2.2 Arithmetic
23. What will be the values of the variables num1 and num2 after the given set of assignments?
num1 = 20
num2 = 10
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 4/19
10/24/2017 Testbank (Chapter 2)
Title What will be the values of the variables num1 and num2 after the given set of assignments?
Section 2.2 Arithmetic
24. What is the value of result after the following code snippet?
num1 = 10
num2 = 20
num3 = 2
result = num1 // num2 // num3
print(result)
A. 1
B. 0 Answer
C. The code has an error
D. 0.25
Title What is the value of result after the following code snippet?
Section 2.2 Arithmetic
25. What is the value of result after the following code snippet?
num1 = 20
num2 = 10
num3 = 2
result = num1 // num2 // num3
print(result)
A. 1 Answer
B. 0
C. The code has an error
D. 0.25
Title What is the value of result after the following code snippet?
Section 2.2 Arithmetic
26. What is the value of result after the following code snippet?
num1 = 20
num2 = 10
num3 = 2
result = num1 // num2 / num3
print(result)
A. 1.0 Answer
B. 0.0
C. The code has an error
D. 0.25
Title What is the value of result after the following code snippet?
Section 2.2 Arithmetic
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 5/19
10/24/2017 Testbank (Chapter 2)
Answer
28. Which code snippet is the correct Python equivalent to the following Algebraic expression ?
c = √(a2 + b2)
A. sqrt(a ^ 2 + b ^ 2)
B. sqrt(a ** 2 + b ** 2) Answer
C. sqrt(a * 2 + b * 2)
D. squareroot(a ** 2 + b ** 2)
Title What is the value of result after the following code snippet?
Section 2.2 Arithmetic
A. //
B. /
C. % Answer
D. #
Title What symbol is used to find remainder of a floor division?
Section 2.2 Arithmetic
30. A(n) _____________________ is a collection of programming instructions that carry out a particular task.
A. argument
B. parameter
C. function Answer
D. literal
Title What is a collection of programming instructions that carry out a particular task?
Section 2.2 Arithmetic
A. 12
B. 64 Answer
C. 1
D. Nothing, there is an error in the statement
Title What is the value of 4 ** 3?
Section 2.2 Arithmetic
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 6/19
10/24/2017 Testbank (Chapter 2)
A. 1
B. 24 Answer
C. 15
D. 2
A. 3
B. 3.14159
C. 3.2
D. 3.14 Answer
37. A(n) _____________________ is a collection of code that have been written by someone else, ready for
your to use in your program.
A. module
B. argument
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 7/19
10/24/2017 Testbank (Chapter 2)
C. function
D. library Answer
Title What is a collection of code that has been written and translated by someone else.
Section 2.2 Arithmetic
38. What must be done first before you can use a function from the standard library?
A. 8.0 Answer
B. 32.0
C. 4.0
D. 64.0
x = 5
y = 7
z = x - y * 2
42. The python code that represents the formula c = (a / b)3 is:
A. c = a / b ** 3
B. c = (a / b) ** 3 Answer
C. c = 3 ^ (a / b)
D. c = (a / b) ^ 3
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 8/19
10/24/2017 Testbank (Chapter 2)
Title Translate a formula to equivalent Python code
Section 2.2.2 Powers
x = 5
y = 3
z = 2
result = x // y + x % z
44. Which statement computes the square root of 5 and stores it in the variable, r? Assume that the math
module has already been imported.
A. r = squareRoot(5)
B. r = sqrt(5) Answer
C. r = squareRoot[5]
D. r = sqrt[5]
Title Which statement computes the square root of 5?
Section 2.2.5 Mathematical Functions
45. Which of the following statements computes the minimum of the variables a, b, c and d, and stores it in x?
A. x = minimum(a, b, c, d)
B. x = min(a, b, c, min(d))
C. x = min(min(a, b), min(c, d)) Answer
D. min(a, b, c, d) = x
Title Which statement computes the minimum value of four variables?
Section 2.2.5 Mathematical Functions
47. What is the value of length after this statement: length = len("Good Morning")?
A. 10
B. 11
C. 12 Answer
D. 13
Title What is the value of length after this statement: length = len("Good Morning")?
Section 2.4 Strings
48. What is it called when you put two strings together in Python?
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 9/19
10/24/2017 Testbank (Chapter 2)
A. concatenation Answer
B. addition
C. repetition
D. conversion
Title What is it called when you put two strings together in Python?
Section 2.4 Strings
49. Which statement correctly creates a new variable by combining the two string variables: firstName and
lastName?
Title Which statement correctly creates a new variable by combining the strings firstName and
lastName?
Section 2.4 Strings
A. ho..ho..ho
B. ho..
C. ho..ho..ho.. Answer
D. nothing is printed, this code snippet causes an error
Title What is printed from the given snippet?
Section 2.4 Strings
A. 123Main Street
B. 123 Main Street
C. 123 "Main Street"
D. nothing is printed, this code snippet causes an error Answer
Title What is printed from the given snippet?
Section 2.4 Strings
52. The following code snippet has an error, how can this be corrected so it prints: 123 Main Street?
1. street = " Main Street"
2. address = 123 + street
3. print(address)
A. change the value '123' in line 2 to a string using the str function Answer
B. reverse lines 1 and 2
C. change line 1 to read: street = 123 + "Main Street"
D. change line 2 to read: address = 123 + "Main Street"
Title What has to change to correctly print a street number and street name?
Section 2.4 Strings
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 10/19
10/24/2017 Testbank (Chapter 2)
53. If an integer value can be changed to a string, what functions convert strings to numbers?
A. str, len
B. int, float Answer
C. sqrt, abs, round
D. integer, float
55. What is the index value of the letter 'h' in the string below ?
message = "hello"
A. 1
B. 0 Answer
C. 3
D. 4
Title What is the index value of the letter 'h' in the string below ?
Section 2.4 Strings
56. Given the code snippet below, what code is needed to print the person's initials?
firstName = "Pamela"
middleName = "Rose"
lastName = "Smith"
Title Given the code snippet below, what code is needed to print the person's initials?
Section 2.4 Strings
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 11/19
10/24/2017 Testbank (Chapter 2)
Title Given the code snippet below, what code is printed?
Section 2.4 Strings
58. Which statement finds the last letter of the string variable name?
A. last = name[len(name)]
B. last = len(name) - 1
C. last = len(name)
D. last = name[len(name) - 1] Answer
Title Which statement finds the last letter of the string variable name?
Section 2.4 Strings
A. function
B. method Answer
C. class
D. object
Title Which is the name of a collection of programming instructions that carry out a particular task
to control the behavior of an object?
Section 2.4 Strings
A. Robert
B. robert
C. ROBERT Answer
D. formalName
Title Which is the result of the following code snippet?
Section 2.4 Strings
A. Robert
B. robert Answer
C. ROBERT
D. formalName
Title Which is the result of the following code snippet?
Section 2.4 Strings
B. Today is Thursday
C. Today Is Thursday
D. Today Is thursday
Title Which is the result of the following code snippet?
Section 2.4 Strings
x = len("Hello World!")
A. 10
B. 11
C. 12 Answer
D. 13
Title Determine the length of a string
Section 2.4.1 The String Type
65. Assume that s is an arbitrary string containing at least 2 characters. What is displayed by the following
code segment?
66. What is the value of words after the following code segment?
A. "HelloWorldWorldWorld" Answer
B. "Hello World World World"
C. "HelloWorldHelloWorldHelloWorld"
D. "Hello World Hello World Hello World"
C. x = 17 + int(18.4)
D. x = 17 + float("18.4")
Title Working with Numbers and Strings
Section 2.4.3 Converting Between Numbers and Strings
A. e
B. h Answer
C. o
D. t
product = "Cookies"
product = product.lower()
After this code segment executes, the value of the product variable is:
A. "cookies" Answer
B. "cOOKIES"
C. "Cookies"
D. "COOKIES"
Title Trace code that invokes the lower method on a string
Section 2.4.5 String Methods
print("\"Hello World!\"")
A. Hello World!
B. "Hello World!" Answer
C. \"Hello World!\"
D. The program reports an error
Title Trace code that includes escape sequences
Section 2.4.5 String Methods
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 14/19
10/24/2017 Testbank (Chapter 2)
73. A(n) __________________ is used to tell the user which input is expected?
A. input
B. keyword
C. comment
D. prompt Answer
Title A(n) __________________ is used to tell the user which input is expected?
Section 2.5 Input and Output
74. What is the default data type of input from the keyboard?
A. integer
B. string Answer
C. float
D. character
Title What is the default data type of input from the keyboard?
Section 2.5 Input and Output
75. Which statement correctly saves the price in the variable cost?
userInput = input("Please enter the price:")
Title Which statement correctly saves the price in the variable cost?
Section 2.5 Input and Output
76. Which statement correctly saves the number of items in the variable quantity?
userInput = input("Please enter the quantity:")
A. quantity = float(userInput)
B. quantity = userInput
C. quantity = int(userInput) Answer
D. quantity = int[userInput]
Title Which statement correctly saves the price in the variable quantity?
Section 2.5 Input and Output
A. 25.45378
B. %25.45
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 15/19
10/24/2017 Testbank (Chapter 2)
C. 25.45 Answer
D. nothing, there is an error
Title Which is printed by the following code snippet?
Section 2.5 Input and Output
78. Which output format string correctly allows for 5 positions before and two digits after the decimal point?
A. "%8.2f" Answer
B. "%5.2f"
C. "%7.2f"
D. "%5d.2f"
Title Which output format string correctly allows for 5 positions before and two digits after the
decimal point?
Section 2.5 Input and Output
79. Which output format correctly prints an item description left justified with up to 10 letters?
A. "%10"
B. "%10s"
C. "%-10s" Answer
D. "-%10s"
Title Which output format correctly prints an item description left justified with up to 10 letters?
Section 2.5 Input and Output
A. The area is 25
B. nothing, there is an error in the code snippet
C. The area is 00025 Answer
D. The area is 25
When this code segment is run the user enters 1 at the first prompt and 5 at the second prompt. The output
displayed is:
A. 1
B. 6
C. 15 Answer
D. 1 + 5
Title Trace code that reads two values from the user
Section 2.5.1 User Input
82. The line of code which reads a value from the user and stores it in a variable named x as a floating-point
value is:
A. x = float()
B. x = input("Enter the value of x: ")
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 16/19
10/24/2017 Testbank (Chapter 2)
83. The line of code that displays the floating point number stored in the variable x using 3 decimal places is:
A. print("%.3f", x)
B. print("%.3f" % x) Answer
C. print("%3.f", x)
D. print("%3.f" % x)
Title Format output to 3 decimal places
Section 2.5.3 Formatted Output
a = 10.0
b = 0.50
print("The total is %.2f and the tax is %.2f." % (a, b))
x = 12
print("%d%%" % x)
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 17/19
10/24/2017 Testbank (Chapter 2)
Section 2.6 Graphics: Simple Drawings
88. Which statement sets the fill color when drawing shapes on the canvas?
A. canvas.setOutline("black")
B. canvas.setFill("black") Answer
C. canvas.fill("black")
D. canvas.fillRect("black")
Title Which statement fills a shape?
Section 2.6 Graphics: Simple Drawings
90. Which of the given print statements generates the following output?
ABCDE"\
A. print("ABCDE\"\\") Answer
B. print("ABCDE"\")
C. print("ABCDE"\)
D. print("ABCDE\"\")
Title Which print statement generates output with quote and backslash?
Section 2.6 Graphics: Simple Drawings
The line of code that should be added to the end of the code segment above to draw a diagonal line
connecting the upper left corner to the lower right corner is:
A. canvas.drawLine(0, 0, 0, 0)
B. canvas.drawLine(0, 0, 200, 400)
C. canvas.drawLine(200, 400, 400, 200)
D. canvas.drawLine(400, 200, 0, 0) Answer
Title Draw a diagonal line on a canvas
Section 2.6.2 Lines and Polygons
92. The statement that sets the fill color to red is:
A. canvas.setFill(0, 128, 0)
B. canvas.setFill(64, 0, 128)
C. canvas.setFill(64, 255, 64)
D. canvas.setFill(128, 0, 0) Answer
Title Which statement sets the fill color to red?
Section 2.6.3 Filled Shapes and Color
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 19/19