100% found this document useful (2 votes)
1K views19 pages

Testbank Chapter 2

This document contains a testbank with 22 multiple choice questions about programming with numbers and strings in Python. The questions cover topics like variables, data types, arithmetic operations, and commenting code. Correct answers are provided to check understanding of concepts like defining variables, storing values, following naming conventions, and performing basic math in Python programs. Maintaining explanatory code through comments and variable names is important for others reading the source code.

Uploaded by

Jia Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
1K views19 pages

Testbank Chapter 2

This document contains a testbank with 22 multiple choice questions about programming with numbers and strings in Python. The questions cover topics like variables, data types, arithmetic operations, and commenting code. Correct answers are provided to check understanding of concepts like defining variables, storing values, following naming conventions, and performing basic math in Python programs. Maintaining explanatory code through comments and variable names is important for others reading the source code.

Uploaded by

Jia Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

10/24/2017 Testbank (Chapter 2)

Title 2 Programming with Numbers and Strings


Book Python for Everyone
Edition 1

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

2. The definition of a variable is:


A. a storage location in a computer program Answer
B. an assignment statement
C. an expression
D. a statement
Title What is the definition of a variable?
Section 2.1 Variables

3. How is a value stored in a variable?


A. an assignment statement Answer
B. an expression
C. a print statement
D. an equality statement
Title How is a value stored in a variable?
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

5. What is wrong with this assignment statement?


num + 10 = num2

A. left hand side of assignment statement cannot have an equation Answer


B. nothing, this statement compiles and executes
C. num2 must be defined before this statement can be executed
D. num must be defined before this statement can be executed
Title What is wrong with this assignment statement?
Section 2.1 Variables

6. What is the right way to assign the value of num + 10 to num2?

A. num2 = num + 10 Answer


file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 1/19
10/24/2017 Testbank (Chapter 2)

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

7. What is wrong with the following code snippet?


num = 78A

A. The num variable is never assigned a value


B. The num variable is assigned a non-numeric value Answer
C. The num variable is not a valid variable name
D. The num variable is never initialized
Title What is wrong with the following code snippet?
Section 2.1 Variables

8. What is wrong with the following code snippet?


2ndNum = 78

A. The 2ndNum variable is never assigned a value


B. The 2ndNum variable is assigned a non-numeric value
C. The 2ndNum variable is not a valid variable name Answer
D. The 2ndNum variable is never initialized
Title What is wrong with the following code snippet?
Section 2.1 Variables

9. What is a variable called that should remain unchanged throughout your program?

A. a constant variable Answer


B. a data variable
C. a string variable
D. a boolean variable
Title What is a variable called that should remain unchanged throughout your program?
Section 2.1 Variables

10. Which of the following variables should be coded as a constant in Python?

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

Title Python naming conventions for variables


Section 2.1 Variables
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 2/19
10/24/2017 Testbank (Chapter 2)

12. Why is it important to follow Python naming standards for variables representing constants?

A. it is good programming style Answer


B. it is required by the Python programming language
C. it is required by graphic programs
D. it is required for all non-zero numbers
Title Python naming conventions for variables
Section 2.1 Variables

13. What symbol is used to indicate a comment in Python?

A. *
B. /*
C. # Answer
D. >

Title Python naming conventions for variables


Section 2.1 Variables

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

16. Which of the following names is not a legal variable name?


A. bottle-volume Answer
B. cans_per_pack
C. four
D. x2
Title Which of the following names is not a legal variable name?
Section 2.1.3 Variable Names

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

18. What symbol is used to begin a comment in a python program?


file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 3/19
10/24/2017 Testbank (Chapter 2)

A. !
B. @
C. # Answer
D. $

Section 2.1.5 Comments


Title What symbol is used to begin a comment in a python program?

19. Which of the following statements correctly multiplies num1 times num2?

A. num1 * num2 Answer


B. num1 x num2
C. num1 · num2
D. num1 ** 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?

A. num1 + num2 + num3 / 3 Answer


B. num1 + num2 + num3 % 3
C. ( num1 + num2 + num3 ) / 3
D. ( num1 + num2 + num3 / 3 )

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?

A. Use more statements in source code.


B. Add comments to source code. Answer
C. Avoid usage of complex calculations in source code
D. Keep variable names short, i.e.: x, y, z
Title What is wrong with the following code snippet?
Section 2.2 Arithmetic

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)

num1 = num1 + num2 / 2


num2 = num1

A. num1 = 20.0, num2 = 10.0


B. num1 = 15.0, num2 = 10.0
C. num1 = 25.0, num2 = 25.0 Answer
D. num1 = 15.0, num2 = 15.0

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

27. Which statement determines the dollar and cents of a variable?

file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 5/19
10/24/2017 Testbank (Chapter 2)

A. dollars = pennies // 100


cents = pennies % 100

Answer

B. dollars = pennies / 100


cents = pennies % 100

C. dollars = pennies // 100


cents = pennies / 100

D. The code has an error


Title What is the value of result after the following code snippet?
Section 2.2 Arithmetic

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

29. What symbol is used to find remainder of a floor division?

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

31. What is the value of 4 ** 3?

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

32. What is returned by the function: round(x)if x = 5.64?

file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 6/19
10/24/2017 Testbank (Chapter 2)

A. Nothing, there is an error in the statement


B. 5
C. 5.6
D. 6 Answer

Title What is returned by the function: round(x)if x = 5.64?


Section 2.2 Arithmetic

33. What is returned by the function: abs(x)if x = 5.64?

A. Nothing, there is an error in the statement


B. 5
C. 5.64 Answer
D. 6

Title What is returned by the function: abs(x)if x = 5.64?


Section 2.2 Arithmetic

34. What is returned by the function: max(1, 4, 15, 2, 3, 24)?

A. 1
B. 24 Answer
C. 15
D. 2

Title What is returned by the max function?


Section 2.2 Arithmetic

35. What is returned by the function: round(3.14159, 2)?

A. 3
B. 3.14159
C. 3.2
D. 3.14 Answer

Title What is returned by the function: round(3.14159, 2)?


Section 2.2 Arithmetic

36. What is wrong with the following code snippet?


result = num1 // num2 / num3
num1 = 20
num2 = 10
num3 = 2
print(result)

A. the code snippet is using undefined variables Answer


B. nothing, the code compiles and runs
C. there is no symbol // in Python
D. the variable names are invalid
Title What is wrong with the following code snippets which include variables?
Section 2.2 Arithmetic

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. the function must be defined


B. the function must be imported Answer
C. the function must be included in a module
D. the function must be enclosed in parenthesis
Title What must be done first before you can use a function from the standard library?
Section 2.2 Arithmetic

39. What is returned by the function: sqrt(64)?

A. 8.0 Answer
B. 32.0
C. 4.0
D. 64.0

Title What is returned by the sqrt function?


Section 2.2 Arithmetic

40. What is wrong with the following code snippet?


((num1 + num2) * num3 / 2 * (1 - num4)

A. nothing, the code compiles and runs


B. there is an extra parenthesis Answer
C. parenthesis are not required
D. illegal expression
Title What is wrong with the following code snippet?
Section 2.2 Arithmetic

41. Consider the following code segment:

x = 5
y = 7
z = x - y * 2

After this code segment executes, the value of z is:


A. -9 Answer
B. -4
C. 5
D. 7
Title Evaluate expressions involving basic arithmetic operations
Section 2.2.1 Basic Arithmetic Operations

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

43. Consider the following code segment:

x = 5
y = 3
z = 2
result = x // y + x % z

After this code segment, the value of result is:


A. 2 Answer
B. 3
C. 4
D. 5
Title Evaluate an expression involving division and remainder
Section 2.2.3 Floor Division and Remainder

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

46. What is a sequence of characters?


A. string Answer
B. boolean
C. variable
D. expression
Title What is a sequence of characters?
Section 2.4 Strings

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?

A. name = "firstName" + "lastName"


B. name = firstName + lastName Answer
C. name = first name + last name
D. name = firstName & lastName

Title Which statement correctly creates a new variable by combining the strings firstName and
lastName?
Section 2.4 Strings

50. What is printed from the following code snippet:


message = "ho.."
print(message * 3)

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

51. What is printed by the following code snippet:


street = " Main Street"
address = 123 + street
print(address)

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

Title What functions are used to convert strings to numbers?


Section 2.4 Strings

54. What is printed by the following code snippet?


num = int("45") * float("1.5")
print(num)

A. nothing, this causes an error


B. 46.5
C. 45 * 1.5
D. 67.5 Answer

Title What is printed by the following code snippet?


Section 2.4 Strings

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"

A. print(firstName[1], middleName[1], lastName[1])


B. print(firstName[0], middleName[0], lastName[0]) Answer
C. print(firstName + middleName + lastName)
D. print(firstName, middleName, lastName)

Title Given the code snippet below, what code is needed to print the person's initials?
Section 2.4 Strings

57. Given the code snippet below, what code is printed?


firstName = "Pamela"
middleName = "Rose"
lastName = "Smith"
print(firstName[0], middleName[0], lastName[5])

A. nothing, this causes an index of bounds error Answer


B. PRh
C. P R h
D. PRS

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

59. A ___________________________ is a collection of programming instructions that can be appllied to an


object?

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

60. What is the result of the following code snippet?


name = "Robert"
formalName = name.upper()
print(formalName)

A. Robert
B. robert
C. ROBERT Answer
D. formalName
Title Which is the result of the following code snippet?
Section 2.4 Strings

61. What is the result of the following code snippet?


name = "Robert"
formalName = name.lower()
print(formalName)

A. Robert
B. robert Answer
C. ROBERT
D. formalName
Title Which is the result of the following code snippet?
Section 2.4 Strings

62. What is the result of the following code snippet?


name = "today is thursday"
name.replace("t", "T")
name.replace("i", "I")
print(name)

A. today is thursday Answer


file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 12/19
10/24/2017 Testbank (Chapter 2)

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

63. What is the result of the following code snippet?

name = "today is thursday" newName = name.replace("t", "T") print(newName)


A. today is thursday
B. Today is Thursday Answer
C. Today Is Thursday
D. Today Is thursday
Title Which is the result of the following code snippet?
Section 2.4 Strings

64. What is the value of x after the following code segment?

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?

print(s[0], s[len(s) - 1])

A. The first character of s, followed immediately by the second last character of s.


B. The first character of s, followed immediately by the last character of s.
C. The first character of s, followed by a space, followed by the second last character of s.
D. The first character of s, followed by a space, followed by the last character of s. Answer
Title Display specific characters from a string
Section 2.4.1 The String Type

66. What is the value of words after the following code segment?

words = "Hello" + "World" * 3

A. "HelloWorldWorldWorld" Answer
B. "Hello World World World"
C. "HelloWorldHelloWorldHelloWorld"
D. "Hello World Hello World Hello World"

Title String Concatenation and Repetition


Section 2.4.2 Concatenation and Repetition

67. Which of the following statements causes Python to report an error?


A. x = 17 + 18.4
B. x = 17 + "18.4" Answer
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 13/19
10/24/2017 Testbank (Chapter 2)

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

68. What letter is displayed by the following code segment?

title = "Python for Everyone"


print(title[3])

A. e
B. h Answer
C. o
D. t

Title Identify a character within a string


Section 2.4.4 Strings and Characters

69. Consider the following code segment:

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

70. Consider the following code segment:

title = "Python for Everyone"


newTitle = title.replace("e", "*")

After this code runs, the value stored in newTitle is:


A. "Python for *veryone"
B. "Python for Ev*ryone"
C. "Python for Ev*ryon*" Answer
D. "Python for *v*ryon*"
Title Trace code that invokes the replace method on a string
Section 2.4.5 String Methods

71. What is displayed by the following code segment?

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)

72. What function is used to read input from the keyboard?


A. input Answer
B. print
C. keyboard
D. next
Title What function is used to read input from the keyboard?
Section 2.5 Input and Output

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:")

A. cost = float(userInput) Answer


B. cost = userInput
C. cost = int(userInput)
D. cost = float[userInput]

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

77. What is printed by the following code snippet?


cost = 25.45378
print("%.2f" % cost)

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

80. What is the output for the following code snippet:


area = 25
print("The area is %05d" % area)

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

Title What is the output for the following code snippet?


Section 2.5 Input and Output

81. Consider the following code segment:

a = input("Enter the value of a: ")


b = input("Enter the value of b: ")
print(a + b)

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)

C. x = float(input("Enter the value of x: ")) Answer


D. x = input(float())
Title Read numerical input from the user
Section 2.5.2 Numerical Input

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

84. What output is generated by the following code segment?

a = 10.0
b = 0.50
print("The total is %.2f and the tax is %.2f." % (a, b))

A. The total is .00 and the tax is .50


B. The total is 10.0 and the tax is 0.5
C. The total is 10.0 and the tax is 0.50
D. The total is 10.00 and the tax is 0.50 Answer

Title Format multiple values in a single output statement


Section 2.5.3 Formatted Output

85. Consider the following code segment:

x = 12
print("%d%%" % x)

The output generated by this code segment is:


A. 12
B. %12
C. 12% Answer
D. 12%%
Title Format integer output
Section 2.5.3 Formatted Output

86. A graphics application shows information inside a ____________________


A. panel
B. window Answer
C. form
D. page
Title A graphics application shows information inside a ____________________
Section 2.6 Graphics: Simple Drawings

87. Which statement draws a square on the canvas?


A. canvas.drawRect(0, 50, 0, 50)
B. canvas.drawRect(50, 50, 0, 0)
C. canvas.drawRect(0, 0, 50, 100)
D. canvas.drawRect(0, 0, 50, 50) Answer
Title Which statement draws a square on the canvas?

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

89. Which statement writes the word Hello on the canvas?


A. canvas.setString(10, 10, "Hello")
B. canvas.setText(10, 10, "Hello")
C. canvas.drawText(10, 10, "Hello") Answer
D. canvas.drawString(10, 10, "Hello")
Title Which statement writes test to the canvas?
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

91. Consider the following code segment:

from graphics import GraphicsWindow

win = GraphicsWindow(400, 200)


canvas = win.canvas()

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

93. Which of the following statements draws a circle?


A. canvas.drawOval(100, 200, 100, 200)
file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 18/19
10/24/2017 Testbank (Chapter 2)

B. canvas.drawOval(200, 100, 200, 200) Answer


C. canvas.drawOval(200, 200, 100, 200)
D. canvas.drawOval(200, 200, 200, 100)
Title Which statement draws a circle?
Section 2.6.4 Ovals, Circles and Text

file:///C:/Horstmann1e_TB/ch02.testbank.xhtml 19/19

You might also like