Testbank Chapter 3
Testbank Chapter 3
Testbank Chapter 3
Title 3 Decisions
Book Python for Everyone
Edition 1
A.
if (x < 10) size = "small";
B.
if (x < 10)
size = "small"
else (x < 20)
size = "medium"
C.
if x < 10 :
size = "small"
else :
size = "medium"
Answer
D.
if x < 10 :
size = "small"
else
size = "medium"
5. Which of the following correctly identifies what is wrong with the code snippet below:
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 1/30
10/24/2017 Testbank (Chapter 3)
if y > 300 :
x = y
else :
x = 0
print("x:", x)
6. Assuming that the user provides 303 as input, what is the output of the following code snippet?
A. x: 0
B. x: 303 Answer
C. x: 300
D. There is no output due to a syntax error.
Title What is the output of an if/else statement given sample input value
Section 3.1 The if statement
8. Assuming that the user provides 95 as input, what is the output of the following code snippet?
A. x: 0 Answer
B. x: 303
C. x: 300
D. There is no output due to a syntax error
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 2/30
10/24/2017 Testbank (Chapter 3)
Title What is the output of an if/else statement given sample input value
Section 3.1 The if statement
9. What is printed by the following code snippet if itemCount contains a value of 10 and cost contains 80:
if itemCount > 5 :
discount = 0.8
totalcost = cost * discount
print("Total discounted price is:", totalCost)
A. Nothing, the program will run but not print any results
B. Total discounted price is: 64 Answer
C. Total discounted price is: 0
D. Total discounted price is: 16
10. What is the output of the following code snippet if the cost contains 100:
if count > 0 :
x = x + 1
print(x)
If count is initialized to -1 and x is initialized to 4 then the value displayed by this code segment is:
A. -1
B. 0
C. 4 Answer
D. 5
Title Trace an if statement
Section 3.1 The if Statement
numPizzas = 1
numPeople = 4
if numPeople == 5 :
numPizzas = 2
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 3/30
10/24/2017 Testbank (Chapter 3)
After this code segment executes, what value is in the variable numPizzas?
A. 1 Answer
B. 2
C. 4
D. 5
Title Trace an if statement
Section 3.1 The if Statement
c = 2
b = 1
if b == 0 :
c = c + 1
else :
c = c - 1
print(c)
A. 1 Answer
B. 2
C. 3
D. 4
Title Trace an if/else statement
Section 3.1 The if Statement
if count = max :
print("You win")
A. Equality is evaluated using two equal signs (==), not one. Answer
B. The print function should not be indented
C. There must be an else statement
D. Nothing, if count equals max, it would print "You win"
Title What is the error in this statement?
Section 3.2 Relational Operators
16. What is the output of the following code snippet if count contains 56?
if count % 2 == 0 :
print("Count is an even number")
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 4/30
10/24/2017 Testbank (Chapter 3)
else :
print("Count is an odd number")
17. What is the output of the following code snippet if count contains 56?
if count % 2 == 0 :
print("Count is an even number")
else :
print("Count is an odd number")
20. Which statement correctly tests if the user entered the letter Y?
A. if userInput = "y" :
B. if userInput = "Y" :
C. if userInput == "Y" : Answer
D. if userInput == "y" :
Title Which statement correctly tests for a user input value?
Section 3.2 Relational Operators
21. Assuming the user enters 15 as input, what is the output of the following code snippet?
22. What is the output of the following code snippet if the input is 34?
A. 34
B. 33
C. 35 Answer
D. 36
Title What is the output of a given code snippet and given input value?
Section 3.2 Relational Operators
23. Assuming that the user enters a value of 45, what is the output of the following code snippet?
A. 105
B. 45
C. 43
D. 49 Answer
Title What is the output of a code statement containing multiple if statements and a given input
value?
Section 3.2 Relational Operators
24. A store provides a 10% discount on all items with a price of at least $100. Otherwise, no discount is
applicable. Which of the following DOES NOT correctly compute the discount?
A.
discount = 0
if price >= 100 :
discount = 0.10 * price
B.
discount = 0.10 * price
if price <= 100 :
discount = 0
Answer
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 6/30
10/24/2017 Testbank (Chapter 3)
C.
discount = 0
if price >= 100 :
discount = 0.10 * price
D.
discount = 10
if price >= 100 :
discount = 0.1 * price
else :
discount = 0
25. Which of the following conditions is true, given that num1 contains 3 and num2 contains 4?
A. num1 + 1 < b
B. num1 + 1 > b
C. num1 + num2 != 7
D. num1 - num2 <= 0 Answer
Title Which of the following conditions evaluates to True given two variables?
Section 3.2 Relational Operators
27. Which condition will cause the body of the if statement to execute when count is 0?
A. if count = 0 :
B. if count < 0 :
C. if count =< 0 :
D. if count == 0 : Answer
Title Which condition will cause the body of the if statement to execute?
Section 3.2 Relational Operators
28. Which of the following if statements is problematic because of the limited precision of floating-point
numbers?
A. if 4 // 3 == 1 :
B. if sqrt(2) * sqrt(2) == 2.0 : Answer
C. if "10" == 5 :
D. if 4 <= 4 :
Title If statements and the limited precision of floating-point numbers
Section 3.2 Relational Operators
s1 = "CAT"
s2 = "cat"
A. if s1 == s2 :
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 7/30
10/24/2017 Testbank (Chapter 3)
B. if s1 = s2 :
C. if s1 < s2 : Answer
D. if s1 >= s2 :
Title Comparing strings with relational operators
Section 3.2 Relational Operators
30. Which statement evaluates to True when comparing the two strings:
name1 = "Heather"
name2 = "hanna"
A. name1 == name2
B. name1 > name2 Answer
C. name1 < name2
D. Relational operators cannot be used to compare strings
Title Which statement evaluates to true when comparing two strings?
Section 3.2
31. Given the following list of strings, what is the correct order using lexicographic ordering: "Ann", "amy",
"Heather", "hanna", "joe", "john", "Leo", "Jim" ?
A. amy, Ann, hanna, Heather, Jim, joe, john, Leo
B. Ann, Heather, Jim, Leo, amy, hanna, joe, john Answer
C. amy, hanna, joe, john, Ann, Heather, Jim, Leo
D. Leo, john, joe, Jim, Heather, hanna, Ann, amy
Title Given a list of strings, what is the correct order using lexicographic ordering?
Section 3.2 Special Topic: Lexicographic Ordering of Strings
33. Assuming a user enters 30, 20, and 10 as the input values, what is the output of the following code
snippet?
A. 0
B. 10
C. 20
D. 30 Answer
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 8/30
10/24/2017 Testbank (Chapter 3)
Title What is the output of the nested if code snippet given three input values?
Section 3.3 Nested Branches
34. Which of the following values make the expression not x == yand z > x true?
A. x = 10, b = 10, c = 15
B. x = 10, b = 20, c = 1t Answer
C. x = 10, b = 2, c = 5
D. x = 10, b = 20, c = 10
Title Which of the following values make the expression not x == y and z > x true?
Section 3.3 Nested Branches
num1 = 100
if num1 < 100 :
if num1 < 50 :
num1 = num1 - 5
else :
num1 = num1 - 10
else :
if num1 > 150 :
num1 = num1 + 5
else :
num1 = num1 + 10
print(num1)
A. 95
B. 100
C. 105
D. 110 Answer
Title What is the output of an if/else code snippet?
Section 3.3 Nested Branches
36. Which of the following options refers to the technique of simulating program execution on a sheet of
paper?
A. Compiling
B. Prototyping
C. Debugging
D. Tracing Answer
Title Which refers to the technique of simulating program execution on a sheet of paper?
Section 3.3 Nested Branches
37. Assuming that a user enters 25 for the price of an item, which of the following hand-trace tables is valid
for the given code snippet?
price = 0
status = ""
price = eval(input("Enter the price for your item: "))
if price >= 50 :
status = "reasonable"
if price >= 75 :
status = "costly"
else :
status = "inexpensive"
if price <= 25 :
status = "cheap"
A.
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 9/30
10/24/2017 Testbank (Chapter 3)
price status
0 "inexpensive"
25 "cheap"
Answer
B.
price status
0 "inexpensive"
25 "reasonable"
C.
price status
0 "inexpensive"
25 "reasonable"
"costly"
D.
price status
0 "inexpensive"
25 "costly"
Title Which hand-trace table is valid for this snippet?
Section 3.3 Nested Branches
B.
if a == b :
print(a)
else :
print(b)
C.
if a == b :
print(a)
if c == d :
print(c)
Answer
D. a = a - 1 if a > 0 else a = a + 1
Title Which of the following is an example of a nested if statement?
Section 3.3 Nested Branches
if a > b :
print("X")
if a == b :
print("Y")
if a > b :
print("X")
if a == b :
print("Y")
if a > b :
print("X")
if a == b :
print("Y")
if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")
If a, b and c are all 0 then the output generated by this code segment is:
A. W Answer
B. W followed by Y on the next line
C. X followed by Y on the next line
D. W followed by X on the next line, followed by Y on the next line
Title Trace nested if/else statements
Section 3.3 Nested Branches
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 11/30
10/24/2017 Testbank (Chapter 3)
if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")
A. W
B. X
C. X followed by Y on the next line
D. X followed by Z on the next line Answer
Title Trace nested if/else statements
Section 3.3 Nested Branches
if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")
A. W
B. X
C. X followed by Y on the next line Answer
D. X followed by Z on the next line
Title Trace nested if/else statements
Section 3.3 Nested Branches
45. What error will Python display when it attempts to execute the following if/else statement?
if a == b :
print("Equal")
else :
print("Not Equal")
if a > b :
print("a is larger")
else :
print("b is larger")
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 12/30
10/24/2017 Testbank (Chapter 3)
Section 3.3 Nested Branches
46. What error will Python display when it attempts to execute the following if/else statement?
if a = b :
print("Equal")
else :
print("Not Equal")
if a > b :
print("a is larger")
else :
print("b is larger")
A. Python will display an error indicating that = should be replaced with == Answer
B. Python will display an error indicating that an if statement cannot reside inside the body of an else
C. Python will display an error indicating that there is a problem with the indentation
D. No error will be displayed
Title Find the error in an if statement
Section 3.3 Nested Branches
47. What is the output of the following code snippet when the user enters 75 as the grade?
A. A
B. B
C. C
D. D Answer
Title What is the output of an if statement with multiple alternatives?
Section 3.4 Multiple Alternatives
49. Given that the following code is incorrect, what code would fix the following code snippet?
A. Change the if statements, to elif statements (except the first one) Answer
B. Reverse the order of the if statements
C. Use a switch statement
D. Change the last statement to if instead of else
Title How can you correct a code snippet that has multiple alternatives?
Section 3.4 Multiple Alternatives
x = 20
if x <= 20 :
print("1", end="")
if x <=40 :
print("2", end="")
if x <= 30 :
print("3", end="")
A. 1
B. 2
C. 3
D. 123 Answer
Title What is the output of the following code snippet?
Section 3.4 Multiple Alternatives
Assuming that the user input is 40, which block of statements is executed?
A. if number > 30 : ... Answer
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 14/30
10/24/2017 Testbank (Chapter 3)
Assuming that the user input is 60, what is the output of the this code snippet?
A. Too high
B. High Answer
C. Intermediate
D. Too small
Title What is output of the if/elif/else snippet with this input?
Section 3.4 Multiple Alternatives
num1 = 0
num2 = 0
num3 = 0
num4 = 0
num5 = 0
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
if num1 < num2 :
num3 = num1
else :
num3 = num2
Assuming that the user enters the numbers 20 and 12 as the two input values, what is the output of the
code snippet?
A. num1 = 20 num2 = 12 num3 = 20 num4 = 0 num5 = 20
B. num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0 Answer
C. num1 = 20 num2 = 12 num3 = 12 num4 = 0 num5 = 20
D. num1 = 20 num2 = 12 num3 = 20 num4 = 20 num5 = 0
Title What is output of the if/else and if/elif snippet with two input values?
Section 3.4 Multiple Alternatives
54. What is the value of the price variable after the following code snippet is executed?
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 15/30
10/24/2017 Testbank (Chapter 3)
price = 42
if price < 40 :
price = price + 10
if price > 30 :
price = price * 2
if price < 100 :
price = price - 20
A. 42
B. 52
C. 84
D. 64 Answer
Assuming that the user inputs 80 as the age, what is the output?
A.
Child
Young adult
Old
B.
Young adult
Old
C.
Impressively old
Answer
D.
Child
Young adult
Old
Impressive old
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 16/30
10/24/2017 Testbank (Chapter 3)
print("Old", end="")
if age < 100 :
print("Impressively old", end="")
Assuming that the user inputs 80 as the age, what is the output?
A. Child
B. ChildYoung Adult
C. ChildYoung AdultOld Answer
D. ChildYoung adultOldImpressively old
Title What is output of the code snippet with this input?
Section 3.4 Multiple Alternatives
Assuming that the user inputs 5 as the age, what is the output?
A. Child
B. ChildYoung Adult
C. ChildYoung AdultOld Answer
D. ChildYoung adultOldImpressively old
Title What is output of the code snippet with this input?
Section 3.4 Multiple Alternatives
58. Consider the follow code segment. It is supposed to convert numeric marks to letter grades. However, it
may contain a bug. Examine the program, and identify what bug, if any, is present.
grade = "F"
if mark >= 80 :
grade = "A"
if mark >= 70 :
grade = "B"
if mark >= 60 :
grade = "C"
if mark >= 50 :
grade = "D"
A. The greater than or equal signs need to be replaced with equal signs
B. All instances of if, except the first, need to be replaced with elif Answer
C. All instances of if, except the first, need to be replaced with else
D. There is nothing wrong with the code segment (it works as intended)
Title What is wrong with this multiple alternatives statement?
Section 3.4 Multiple Alternatives
59. Consider the follow code segment. It is designed to classify widgets as too small if they are less than
10mm in diameter or too large if they are 15mm in diameter or more. Otherwise they should be classified
as just right. However, this code may contain a bug. Examine the program, and identify what bug, if any,
is present.
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 17/30
10/24/2017 Testbank (Chapter 3)
if size >= 0 :
print("Too small")
elif size >= 10 :
print("Just right")
elif size >= 15 :
print("Too big")
A. The greater than or equal signs need to be replaced with greater than signs
B. All instances of elif need to be replaced with else
C. The order of the conditions (and bodies) must be changed Answer
D. There is nothing wrong with the code segment (it works as intended)
Title What is wrong with this multiple alternatives statement?
Section 3.4 Multiple Alternatives
60. Consider the following code segment. It is designed to convert letter grades to grade points. Examine the
program, and identify what bug, if any, is present.
if letter == "A" :
gradePoints = 4.0
elif letter == "B" :
gradePoints = 3.0
elif letter == "C" :
gradePoints = 2.0
elif letter == "D" :
gradePoints = 1.0
else :
gradePoints = 0.0
A. The double equal signs need to be replaced with greater than or equal signs
B. All instances of elif need to be replaced with else
C. The order of the conditions (and bodies) must be changed
D. There is nothing wrong with the code segment (it works as intended) Answer
Title What is wrong with this multiple alternatives statement?
Section 3.4 Multiple Alternatives
62. The flowchart shows the order in which steps should be executed, and the diamond-shaped boxes indicate:
A. input
B. algorithms
C. tasks
D. decision statements Answer
Title What are the parts of a flowchart?
Section 3.5 Problem Solving: Flowcharts
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 18/30
10/24/2017 Testbank (Chapter 3)
if a == 0 :
print("a is 0")
elif a < 0 :
print("a is less than 0")
else :
print("a is greater than 0")
What is the minimum number of test cases needed to test every line of code in this segment?
A. 2
B. 3 Answer
C. 4
D. 5
Title How many test cases are needed to test every line of code in this segment?
Section 3.6 Problem Solving: Test Cases
65. The boolean (bool) data type has exactly two values:
A. Yes / No
B. True / False Answer
C. 0 / 1
D. -1 / 1
Title What are the values of a boolean variable?
Section 3.7 Boolean Variables and Operators
Assuming the user enters a value of 120, what will be the output:
A. Nothing is printed
B. Liquid
C. Not Liquid Answer
D. LiquidNotLiquid
Title Given a code snippet and an input value, what output is produced?
Section 3.7 Boolean Variables and Operators
68. Which of the following variables is used to store a condition that can be either True or False?
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 19/30
10/24/2017 Testbank (Chapter 3)
A. Logical
B. Boolean Answer
C. Algebraic
D. Conditional
Title What kind of variable is used to store a true/false condition?
Section 3.7 Boolean Variables and Operators
69. Given two variables x and y, how do you test whether exactly one of them is zero?
A. if x == 0 or y == 0
B. if x = 0 or y = 0
C. if x == 0 and y != 0 or y == 0 and x != 0 Answer
D. if x == 0 and y != 0 and y == 0 and x != 0
Title Given a code snippet, determine the correct boolean expression to test for a given condition?
Section 3.7 Boolean Variables and Operators
70. Given two variables x and y, how do you test whether at least one of them is zero?
A. if x == 0 or y == 0 Answer
B. if x = 0 or y = 0
C. if x == 0 and y != 0 or y == 0 and x != 0
D. if x == 0 and y != 0 and y == 0 and x != 0
Title Given a code snippet, determine the correct boolean expression to test for a given condition?
Section 3.7 Boolean Variables and Operators
A. temp = 0 Answer
B. temp = 32
C. temp = 100
D. temp = 75
A. quantity = 0 Answer
B. price/quantity < 10
C. quantity = 5
D. price/quantity > 10
attendance = True
failed = False
Assuming that the user enters 55 as the age, what is the output?
A. Teen
B. Young at heart Answer
C. Child
D. Adult
Title What is output of the code snippet with this input?
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 21/30
10/24/2017 Testbank (Chapter 3)
Section 3.7 Boolean Variables and Operators
78. Which of the following expressions represents a legal way of checking whether a value assigned to the num
variable falls within the range 0 to 150 (inclusive)?
A. if num >= 150 and num <= 0
B. if num >= 0 and num <= 150 Answer
C. if num >= 0 or num <= 150
D. if num >= 150 or num <= 0
Title Which expression checks whether a value falls between 0 and 150 inclusive?
Section 3.7 Boolean Variables and Operators
79. Which of the following expressions represents a legal way of checking whether a value assigned to the num
variable is either less than 100 or more than 200 (exclusive)?
A. if num < 100 and num > 200
B. if num < 100 and num > 200 Answer
C. if num < 100 or num > 200
D. if num <= 100 or num >= 200
Title Which expression checks whether a value falls between 0 and 150 inclusive?
Section 3.7 Boolean Variables and Operators
82. Which of the following conditions is true only when the integer variable middle is between 0 and 10
inclusive?
A. if middle >= 0 and middle <= 10 Answer
B. if 0 <= middle <= 10
C. if 0 <= middle or middle <= 10
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 22/30
10/24/2017 Testbank (Chapter 3)
isFelon = False
answer = input("have you ever committed a felony? ")
if answer == "Yes" or answer == "yes" :
isFelon = True
age = int(input("what is your age? "))
which statement assigns the variable mayVote a value of True if a person may vote if they are 18 or older
and not a felon?
A. mayVote = age > 18 or not isFelon
B. mayVote = not ( age >= 18 and isFelon)
C. mayVote = age >= 18 and not isFelon Answer
D. mayVote = not ( age >= 18 or isFelon)
Title Which of the following statements assigns the Boolean variable correctly?
Section 3.7 Boolean Variables and Operators
MIN_SPEED = 45
MAX_SPEED = 65
speed = 55
if not (speed < MAX_SPEED) :
speed = speed - 10
if not (speed > MIN_SPEED) :
speed = speed + 10
print(speed)
score = 0
price = 100.0
if score > 0 and price < 200 and price / score > 10 :
print("buy!")
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 23/30
10/24/2017 Testbank (Chapter 3)
86. Which of the following options checks that city is neither Atlanta or Philadelphia?
A. if not city == "Atlanta" or not city == "Philadelphia"
B. if not (city == "Atlanta" or city == "Philadelphia") Answer
C. if not (city == "Atlanta" and city == "Philadelphia")
D. if not city == "Atlanta" or city == "Philadelphia"
Title Which of the following options checks that city is neither Atlanta or Philadelphia?
Section 3.7 Boolean Variables and Operators
87. Assuming a user enters 30, 55, and 10 as the input, what is the output of the following code snippet?
A. 55
B. 10
C. 0
D. 30 Answer
Title What is the output of the nested if code snippet given three input values?
Section 3.7 Boolean Variables and Operators
88. Assuming a user enters 30, 55, and 10 as the input, what is the output of the following code snippet?
A. 55 Answer
B. 10
C. 0
D. 30
Title What is the output of the nested if code snippet given three input values?
Section 3.7 Boolean Variables and Operators
89. Which of the following conditions is True only when the variables a, b, and c contain three different
values?
A. if a != b and a != c and b != c : Answer
B. if a != b or a != c or b != c :
C. if not (a == b and b == c and a == c) :
D. if a != b != c :
Title Which of the following conditions is true only when the variables a, b, and c contain three
different values?
Section 3.7 Boolean Variables and Operators
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 24/30
10/24/2017 Testbank (Chapter 3)
90. Consider the following code segment. It should display a message only if the cost is between 50 and 75
dollars. The message should also be displayed if the cost is exactly 50 dollars or exactly 75 dollars.
if _________________ :
print("The cost is in the desired range")
What condition should be placed in the blank to achieve the desired behavior?
A. cost > 50
B. cost < 75
C. cost >= 50 and cost <= 75 Answer
D. cost >= 50 or cost <= 75
91. Water is liquid between 0 and 100 degrees Celsius. The following code segment should display a message
if the water is not liquid. For this question, we will assume that water is liquid if it is exactly 0 degrees or
exactly 100 degrees.
if _________________ :
print("The water is not liquid")
What condition should be placed in the blank to achieve the desired behavior?
A. temp < 0
B. temp > 100
C. temp < 0 and temp > 100
D. temp < 0 or temp > 100 Answer
92. Suppose that b is False and x is 0. Which of the following expressions evaluates to True?
A. b or x == 1
B. b and x == 0
C. not b and x == 0
D. not b or x == 1 Answer
Title Which of the following expressions evaluates to True?
Section 3.7 Boolean Variables and Operators
93. Suppose that b is False and x is 0. Which of the following expressions evaluates to True?
A. not b and x == 1
B. b or x == -1
C. not b or b Answer
D. x == 1 or x == -1
Title Which of the following expressions evaluates to True?
Section 3.7 Boolean Variables and Operators
94. Which of the following checks to see if there is a comma anywhere in the string variable name?
A. if "," in name : Answer
B. if name.contains(",") :
C. if "," not in name :
D. if name.startswith(",") :
Title Which statement tests if a string contains a substring?
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 25/30
10/24/2017 Testbank (Chapter 3)
Section 3.8 Analyzing Strings
95. Which of the following checks to see if the string variable sentence starts with the string "Dear"?
A. if "Dear" in sentence :
B. if sentence.find("Dear") :
C. if "Dear" not in sentence :
D. if sentence.startswith("Dear") : Answer
Title Which statement tests if a string contains a substring at the beginning?
Section 3.8 Analyzing Strings
A. -1
B. 0 Answer
C. 8
D. 1
Title What value is returned when using the find substring command?
Section 3.8 Analyzing Strings
A. -1 Answer
B. 0
C. 8
D. 1
Title What value is returned when using the find substring command?
Section 3.8 Analyzing Strings
98. What String method can be used to determine if the string contained in the variable text only consists of
letters?
A. text.isalnum()
B. text.isalpha() Answer
C. text.isdigit()
D. text.islower()
Title What String method checks for all characters in a variable?
Section 3.8 Analyzing Strings
99. What String method can be used to determine if all characters within a string are lowercase?
A. text.isalnum()
B. text.isalpha()
C. text.isdigit()
D. text.islower() Answer
Title What String method checks for all lowercase characters?
Section 3.8 Analyzing Strings
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 26/30
10/24/2017 Testbank (Chapter 3)
100. What String method can be used to determine if the string contained in the variable text only consists of
numbers?
A. text.isalnum()
B. text.isalpha()
C. text.isdigit() Answer
D. text.islower()
Title What String method checks for all numbers in a variable?
Section 3.8 Analyzing Strings
A. 0
B. 1 Answer
C. 2
D. -1
Title What value is returned when using the String method count?
Section 3.8 Analyzing Strings
A. 0
B. 1
C. 2 Answer
D. -1
Title What value is returned when using the String method count?
Section 3.8 Analyzing Strings
103. Which of the following statements returns the number of blank spaces contained in the string sentence?
A. sentence.count(" ") Answer
B. " " in sentence
C. sentence.find(" ")
D. count(sentence)
Title What method is used to identify the number of spaces in a string variable?
Section 3.8 Analyzing Strings
Which of the following statements correctly determines if the first letter of the string contained in
sentence is an uppercase letter and not a space?
A. if firstCh.isupper() or not firstCh.isspace() : Answer
B. if not (firstCh.isupper() or firstCh.isspace() :)
C. if firstCh.isupper() and not firstCh.isspace() :
D. if firstCh.isupper() or not firstCh.isspace() :
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 27/30
10/24/2017 Testbank (Chapter 3)
Title What method is used to identify the number of spaces in a string variable?
Section 3.8 Analyzing Strings
106. How do you test if a filename (given as a string) has an extension of ".png", ".jpg" or ".gif"?
A. if filename.endswith(".png" or ".jpg" or ".gif") :
B. if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".gif")
: Answer
C. if ".png" in filename or ".jpg" in filename or ".gif" in filename :
D. if filename.contains(".jpg", ".gif", ".png") :
Title How do you test if a filename (given as a string) has a valid extension?
Section 3.8 Analyzing Strings
s = "Computer Science"
x = s.find("TER")
print(x)
A. -1 Answer
B. 0
C. 5
D. 6
Title Search for a substring within a string
Section 3.8 Analyzing Strings
A. -1
B. 0
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 28/30
10/24/2017 Testbank (Chapter 3)
C. False Answer
D. True
Title Determine what is output by the starts with method
Section 3.8 Analyzing Strings
109. Which of the following statements can be used to validate whether the user entered for a grade a value in
the range, 0 to 100, including both 0 and 100?
A. if grade > 0 and grade < 100 :
B. if grade >= 0 and grade <= 100 : Answer
C. if grade <= 0 and grade >= 100 :
D. if grade <=0 or grade >=100 :
Title Which statement validates the user input is between 0 and 100?
Section 3.9 Application: Input Validation
110. Which of the following statements can be used to validate user input when entering a marital status as a
single letter?
A.
if maritalStatus == "s" or maritalStatus == "m" :
B.
if maritalStatus == "S" or maritalStatus == "M" :
C.
if (maritalStatus == "s" or maritalStatus == "m" or
maritalStatus == "S" or maritalStatus == "M") :
Answer
D.
if maritalStatus == "s" or "S" or "m" or "M" :
Title Which statements can be used to validate the user entered a valid marital status?
Section 3.9 Application: Input Validation
maritalStatus = input("Enter your marital status (s for single, m for married): ")
maritalStatus = maritalStatus.upper()
Which of the following statements can be used to validate whether the user entered a valid marital status?
A.
if maritalStatus == "S" or maritalStatus == "M"
Answer
B.
if maritalStatus == "s" or maritalStatus == "m"
C.
if (maritalStatus == "s" or maritalStatus == "m" or
maritalStatus == "S" or maritalStatus == "M"
D.
if maritalStatus == "s" or "S" or "m" or "M"
Title Which statements can be used to validate the user entered a valid marital status?
Section 3.9 Application: Input Validation
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 29/30
10/24/2017 Testbank (Chapter 3)
Which of the following statements checks that the user entered a valid month?
A. if month >=1 or month <= 12 :
B. if month >=1 and month <= 12 Answer
C. if month >1 or month < 12 :
D. if month >1 and month < 12 :
Title Which statements can be used to validate the user entered a valid birth month?
Section 3.9 Application: Input Validation
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 30/30