Testbank Chapter 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

10/24/2017 Testbank (Chapter 3)

Title 3 Decisions
Book Python for Everyone
Edition 1

1. What statement is used to implement a decision?


A. while
B. if Answer
C. for
D. import
Title What is a decision statement?
Section 3.1 The if statement

2. What are the two parts of an if statement?


A. A condition and a body Answer
B. A check and an increment
C. An increment and a body
D. An increment and a return value
Title What are the two parts of an if statement?
Section 3.1 The if statement

3. Which of the following statements is true about the if statement?


A. The if statement can have only one condition that evaluates to an integer value.
B. The if block is optional.
C. The else block is optional. Answer
D. The if and else blocks can be aligned to any indentation level.
Title Which statement is true about the if statement
Section 3.1 The if statement

4. Which of the following is the correct syntax for an if statement?

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"

Title What is the correct syntax for an if statement?


Section 3.1 The if statement

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)

A. Nothing, the program runs as intended


B. The statement after the if statement must be indented
C. The statement after the if statement and the statement after the else statement must be
indented Answer
D. No colon is needed after the else statement
Title What is the correct syntax for an if statement?
Section 3.1 The if statement

6. Assuming that the user provides 303 as input, what is the output of the following code snippet?

y = int(input("Please enter a number: "))


if y > 300 :
x = y
else :
x = 0
print("x:", x)

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

7. The following code snippet contains an error. What is the error?

if cost > 100


cost = cost - 10
print("Discounted cost:", cost)

A. Syntax error: missing colon after if statement Answer


B. Logical error: use of an uninitialized variable
C. Syntax error: missing an else statement
D. Syntax error: incorrect indentation
Title Find the error in a code snippet containing an if statement
Section 3.1 The if statement

8. Assuming that the user provides 95 as input, what is the output of the following code snippet?

y = int(input("Please enter a number: "))


if y > 300 :
x = y
else :
x = 0
print("x:", x)

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

Title What is the output of a compound statement?


Section 3.1 The if statement

10. What is the output of the following code snippet if the cost contains 100:

if cost > 150 :


discount = 0.8 * cost
else :
discount = cost
print("Your cost is:", discount)

A. Nothing, the code contains a syntax error


B. Your cost is: 0
C. Your cost is: 80
D. Your cost is: 100 Answer

Title What is the output of a conditional expression?


Section 3.1 The ifstatement

11. Consider the following code segment:

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

12. Consider the following code segment:

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

13. Consider the following code segment:

c = 2
b = 1

if b == 0 :
c = c + 1
else :
c = c - 1

print(c)

What value is printed by this code segment?

A. 1 Answer
B. 2
C. 3
D. 4
Title Trace an if/else statement
Section 3.1 The if Statement

14. What is the error in this 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

15. What is the opposite of this condition: count > 10?


A. count >= 10
B. count < 9
C. count <= 10 Answer
D. count <= 9
Title What is the opposite of a conditional 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")

A. Count is an even number


B. Count is an odd number
C. Nothing, there is a syntax error Answer
D. Nothing, the program runs but does not print any output
Title What is the output of a code snippet with relational operators?
Section 3.2 Relational Operators

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

A. Count is an even number Answer


B. Count is an odd number
C. Nothing, there is a syntax error
D. Nothing, the program runs but does not print any output
Title What is the output of a code snippet with relational operators?
Section 3.2 Relational Operators

18. What type of operator is <= operator?


A. Lexicographic
B. Arithmetic
C. Inequality
D. Relational Answer
Title What type of operator is !< operator?
Section 3.2 Relational Operators

19. The operator >= stands for


A. greater than
B. greater than or equal to Answer
C. not equal to
D. this is not a valid Python operator
Title Which relational operator is this?
Section 3.2 Relational Operators

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?

number = int(input("Please enter a number: "))


if number >= 20 :
print("The numer is big")
else :
print("The number is small")
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 5/30
10/24/2017 Testbank (Chapter 3)

A. There is no output due to a syntax error


B. The number is big
C. The number is small Answer
D. The program runs successfully but does not print any output
Title What is the output of a given code snippet and given input value?
Section 3.2 Relational Operators

22. What is the output of the following code snippet if the input is 34?

number = int(input("Please enter a number: "))


if number != 20 :
number = number + 1
else :
number = number - 1
print(number)

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?

number = int(input("Please enter a number: "))


if number < 100 :
number = number + 5
if number < 500 :
number = number - 2
if number > 15 :
number = number + 1
else :
nmber = number - 1
print(number)

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

Title Which statement DOES NOT correctly compute the discount?


Section 3.2 Relational Operators

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

26. In Python, which of the following orderings is used to compare strings?


A. Semantic
B. Alphabetic
C. Syntatic
D. Lexicographic Answer
Title Which ordering is used to compare strings?
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

29. Consider the following code segment:

s1 = "CAT"
s2 = "cat"

Which of the following if statements has a condition that evaluates to True?

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

32. What is the definition of a nested statement?


A. When a decision statement is contained inside the branch of another decision statement Answer
B. A compound statement that consists of a header and a statement block
C. Allows a program to carry out different actions depending on the nature of the data to be processed
D. A statement that is used to validate user input
Title What is the definition of a nested statement?
Section 3.3 Nested Branches

33. Assuming a user enters 30, 20, and 10 as the input values, what is the output of the following code
snippet?

num1 = int(input("Enter a number: "))


num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if num1 > num2 :
if num1 > num3 :
print(num1)
else :
print(num3)
else :
if num2 > num3 :
print(num2)
else :
print(num3)

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

35. What is the output of the following code snippet?

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

38. Which of the following code segments is an example of a nested if statement?


A.
if a == b :
print(a)

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

39. Consider the following code segment:

if a > b :
print("X")
if a == b :
print("Y")

What is displayed if a is 1 and b is 0?


A. X Answer
B. Y
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 10/30
10/24/2017 Testbank (Chapter 3)

C. X followed by Y on the next line


D. Nothing
Title Trace a nested if statement
Section 3.3 Nested Branches

40. Consider the following code segment:

if a > b :
print("X")
if a == b :
print("Y")

What is displayed if a is 0 and b is 0?


A. X
B. Y
C. X followed by Y on the next line
D. Nothing Answer
Title Trace a nested if statement
Section 3.3 Nested Branches

41. Consider the following code segment:

if a > b :
print("X")
if a == b :
print("Y")

What is displayed if a is 1 and b is 2?


A. X
B. Y
C. X followed by Y on the next line
D. Nothing Answer
Title Trace a nested if statement
Section 3.3 Nested Branches

42. Consider the following code segment:

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)

43. Consider the following code segment:

if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")

If a is 0, b is 1 and c is 0 then the output generated by this code segment is:

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

44. Consider the following code segment:

if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")

If a is 0, b is 1 and c is 1 then the output generated by this code segment is:

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

A. Python will display an error indicating that == should be replaced with =


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 Answer
D. No error will be displayed
Title Find the error in an if statement

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?

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
if grade >= 80 :
letterGrade = "B"
if grade >= 70 :
letterGrade = "C"
if grade >= 60 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

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

48. What is the wrong with the following code snippet?

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
if grade >= 80 :
letterGrade = "B"
if grade >= 70 :
letterGrade = "C"
if grade >= 60 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

A. Everyone will get an "E"


B. Anyone with a grade higher than 60 will receive a "D" Answer
file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 13/30
10/24/2017 Testbank (Chapter 3)

C. Nothing is wrong, students will get the correct grade


D. The code block will not compile
Title What is the wrong with the code snippet that has multiple alternatives?
Section 3.4 Multiple Alternatives

49. Given that the following code is incorrect, what code would fix the following code snippet?

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
if grade >= 80 :
letterGrade = "B"
if grade >= 70 :
letterGrade = "C"
if grade >= 60 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

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

50. What is the output of the following code snippet?

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

51. Consider the following code snippet:

number = int(input("Enter a number: "))


if number > 30 :
...
elif number > 20 :
...
elif number > 10 :
...
else :
...

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)

B. else if number > 20 : ...


C. else if number > 10 : ...
D. else : ...
Title Which statement is executed when the user enters 40 for the input value?
Section 3.4 Multiple Alternatives

52. Consider the following code snippet:

number = int(input("Enter a number: "))


if number < 10 :
print("Too small")
elif number < 50 :
print("Intermediate")
elif number < 100 :
print("High")
else :
print("Too high")

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

53. Consider the following code snippet.

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

if num1 < num2 + 10 :


num4 = num1
elif num1 < num2 + 20 :
num5 = num1
print("num1 =", num1, "num2 =", num2, "num3 =", num3,
"num4 =", num4, "num5 =", num5)

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

Title What is value of a variable after (if/if/if) snippet is executed?


Section 3.4 Multiple Alternatives

55. Consider the following code snippet:

age = int(input("Enter your age: "))


if age < 10 :
print("Child")
if age < 30 :
print("Young Adult")
if age < 70 :
print("Old")
if age < 100 :
print("Impressively old")

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

Title What is output of the code snippet with this input?


Section 3.4 Multiple Alternatives

56. Consider the following code snippet:

age = int(input("Enter your age:"))


if age < 10 :
print("Child", end="")
if age < 30 :
print("Young Adult", end="")
if age < 70 :

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

57. Consider the following code snippet:

age = int(input("Enter your age: "))


if age < 10 :
print("Child", end="")
if age < 30 :
print("Young Adult", end="")
if age < 70 :
print("Old", end="")
if age < 100 :
print("Impressively old", end="")

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

61. Flowcharts are made up of all the following elements, EXCEPT:


A. elements for tasks
B. elements for input/output
C. elements for print Answer
D. elements for decisions
Title What are the parts of a flowchart?
Section 3.5 Problem Solving: Flowcharts

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

63. When testing code for correctness, it always makes sense to


A. Aim for complete coverage of all decision points Answer
B. Identify boundary cases and test them
C. Check all cases using hand-tracing

file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 18/30
10/24/2017 Testbank (Chapter 3)

D. Assume invalid input will never occur


Title What is reasonable for checking/testing code?
Section 3.6 Problem Solving: Test Cases

64. Consider the following code segment:

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

66. Which operators listed below are considered boolean operators:


A. < / >
B. and / or Answer
C. == / !=
D. <= / >=
Title What are boolean operators?
Section 3.7 Boolean Variables and Operators

67. Consider the following code snippet:

emp = int(input("Enter Celsius temperature: "))


if temp > 0 and temp < 100 :
print("Liquid")
if temp <= 0 or temp >= 100 :
print("Not liquid")

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

71. Rewrite the following algebraic expression to an equivelant Python expression:

32 <= temp <= 100

A. if temp <= 32 and temp <= 100


B. if temp <= 32 or temp <= 100
C. if temp >= 32 and temp <= 100 Answer
D. if temp >= 32 or temp <= 100

Title Rewrite an algebraic expression into the equivelant Python expression?


Section 3.7 Boolean Variables and Operators

72. What value causes the following logical expression to 'short-circuit'?


if temp >= 32 and temp <= 100

A. temp = 0 Answer
B. temp = 32
C. temp = 100
D. temp = 75

Title What value causes a given expression to short-circuit?


Section 3.7 Boolean Variables and Operators

73. What value causes the following logical expression to 'short-circuit'?


if quantity > 0 and price/quantity <
10

A. quantity = 0 Answer
B. price/quantity < 10
C. quantity = 5
D. price/quantity > 10

Title What value causes a given expression to short-circuit?


file:///C:/Horstmann1e_TB/ch03.testbank.xhtml 20/30
10/24/2017 Testbank (Chapter 3)
Section 3.7 Boolean Variables and Operators

74. Using De Morgan's law, what is the equivelant to this statement?


if not (state == "PA" or state == "OH")

A. if state != "PA" and state != "OH" Answer


B. if state != "PA" or state != "OH"
C. if state == "PA" and state == "OH"
D. if state == "PA" or state == "OH"

Title Using De Morgan's law, identify an equivalent expression?


Section 3.7 Boolean Variables and Operators

75. Using De Morgan's law, what is the equivalent to this statement?


if not (state == "PA" and state == "OH")

A. if state != "PA" and state != "OH"


B. if state != "PA" or state != "OH" Answer
C. if state == "PA" and state == "OH"
D. if state == "PA" or state == "OH"

Title Using De Morgan's law, identify an equivelant expression?


Section 3.7 Boolean Variables and Operators

76. Consider the following code snippet:

attendance = True
failed = False

Which of the followingif statements include a condition that evaluates to True?


A. if attendance == "true" :
B. if attendance : Answer
C. if failed :
D. if attendance == failed :
Title What if statements include a condition that evaluates to true?
Section 3.7 Boolean Variables and Operators

77. Consider the following code snippet:

age = int(input("Enter your age: "))


if age < 13 :
print("Child", end="")
if age >= 13 and age <= 19 :
print("Teen", end="")
if age > 19 and age < 30 :
print("Young adult", end="")
if age >= 30 and age <= 50 :
print("Adult", end="")
if age > 50 :
print("Young at heart", end="")

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

80. Given the following code snippet:

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
elif grade >= 80 and grade < 90 :
letterGrade = "B"
elif grade >= 70 and grade < 80 :
letterGrade = "C"
elif grade >= 60 and grade < 70 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

what is value of grade when the user enters 75?


A. "A"
B. "B"
C. "C" Answer
D. "D"
Title What is the output of a code snippet given a specific input value?
Section 3.7 Boolean Variables and Operators

81. Which of the following operators is used to invert a conditional statement?


A. or
B. and
C. not Answer
D. equal
Title Which operator is used to invert a conditional statement?
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)

D. if middle > 0 and middle < 10


Title Which of the following conditions is true only when the integer variable middle is between 0
and 10?
Section 3.7 Boolean Variables and Operators

83. Given that the following code snippet:

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

84. Given that the following code snippet:

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)

what output is produced?


A. 45
B. 55 Answer
C. 65
D. 50
Title What is the output of a Boolean not code snippet?
Section 3.7 Boolean Variables and Operators

85. Given that the following code snippet

score = 0
price = 100.0
if score > 0 and price < 200 and price / score > 10 :
print("buy!")

which of the following statements is true?


A. The output is buy!
B. The code snippet runs, but there is no output Answer
C. The code snippet has syntax errors
D. The code snippet causes a divide-by-zero exception
Title Which statement is true on the basis of this code snippet?
Section 3.7 Boolean Variables and Operators

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?

num1 = int(input("Enter a number: "))


num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if not (num1 > num2 and num1 > num3) :
print(num1)
elif not(num2 > num1 and num2 > num3) :
print(num2)
elif not (num3 > num1 and num3 > num2) :
print(num3)

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?

num1 = int(input("Enter a number: "))


num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if num1 > num2 and num1 > num3 :
print(num1)
elif num2 > num1 and num2 > num3 :
print(num2)
elif num3 > num1 and num3 > num2 :
print(num3)

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

Title Complete an if statement with an appropriate condition


Section 3.7 Boolean Variable and Operators

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

Title Complete an if statement with an appropriate condition


Section 3.7 Boolean Variables and Operators

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

96. What value is printed by the following code snippet?

name = "John R. Johnson"


firstName = "John"
location = name.find(firstName)
print(location)

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

97. What value is printed by the following code snippet?

name = "John R. Johnson"


firstName = "Joe"
location = name.find(firstName)
print(location)

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

101. What will be printed by the following code snippet?

name = "Ravi Avalon"


counter = name.count("av")
print(counter)

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

102. What will be printed by the following code snippet?

name = "Dino the Dinosaur"


counter = name.count("Di")
print(counter)

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

104. Review the code snippet below:

sentence = "Today is the first day..."


firstCh = sentence[0]

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

105. Review the code snippet below:

name1 = "Betty joe"


name2 = "Betty Jean"
name3 = "Betty Jane"
if name1 < name2 :
if name1 < name3 :
print(name1, "is first")
else :
print(name3, "is first")
else :
if name2 < name3 :
print(name2, "is first")
else :
print(name3, "is first")

what output is produced?


A. Betty joe is first
B. Betty Jean is first
C. Betty Jane is first Answer
D. Betty joe is firstBetty Jean is firstBetty Jane is first
Title Review the code snippet below; what output is produced?
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

107. What value is displayed by the following code segment?

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

108. What value is displayed by the following code segment?

name = "John Smith"


print(name.startswith("john"))

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

111. Review the code snippet below:

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)

112. Review the code snippet below:

month = int(input("Enter your two digit birth month: "))

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

You might also like