Testbank Chapter 4 PDF
Testbank Chapter 4 PDF
Title 4 Loops
Book Python for Everyone
Edition 1
i = 0
while i <= 10 :
print(i)
i = i + 1
# This code prints the first 10 numbers starting with zero
A. i = 1
B. while i < 10 Answer
C. i = i + 2
D. while i + 1< 10
i = 0
while i < 10 :
print(i)
i = i + 1
A. 0
B. 8
C. 9
D. 10 Answer
i = 0
while i != 9 :
print(i, end = " ")
i = i + 2
A. No output
B. 02468
C. 10 12 14 16 18…. (infinite loop)
D. 0 2 4 6 8 10 12 14 …. (infinite loop) Answer
Title What is output of while loop?
Section 4.1 The while Loop
4. How many times does the code snippet given below display "Loop Execution"?
i = 1
while i != 10 :
print("Loop Execution")
i = i + 1
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 1/41
10/24/2017 Testbank (Chapter 4)
A. Infinite times
B. 8 times
C. 9 times Answer
D. 10 times
Title How many iterations of while loop?
Section 4.1 The while Loop
i = 0
j = 0
while i < 27 :
i = i + 2
j = j + 1
print("j=", j)
A. j=27
B. j=12
C. j=13
D. j=14 Answer
i = 1
while i < 10 :
print(i, end = " ")
i = i + 2
if i == 5 :
i = 9
A. 135
B. 1 3 9 Answer
C. 13579
D. 1359
Title What does this while loop print?
Section 4.1 The while Loop
7. The code snippet below checks whether a given number is a prime number. What will be the result of
executing it?
j = 2
result = 0
number = input("Please enter a number:")
while (j < number) :
if number % j == 0 :
result = 1
j = j + 1
if result == 1 :
print("Number: ", number, " is Not Prime.")
else :
print("Number: ", number, " is Prime. ")
a = 2
n = 16
r = 1
b = a
i = n
while i > 0 :
if i % 2 == 0 : # n is even
b = b * b
i = i / 2
else :
r = r * b
i = i - 1
print("r = ", r)
A. r = 16.0
B. r = 128.0
C. r = 4096.0
D. r = 65536.0 Answer
Title What is the output of while loop with nested if?
Section 4.1 The while Loop
i = 0
j = 0
while i < 125 :
i = i + 2
j = j + 1
print(j)
A. 0
B. 62
C. 63 Answer
D. The code fragment displays no output because it does not compile.
Title What is output of while loop?
Section 4.1 The while Loop
s = 1
n = 1
while s < 10 * n :
s = s + n
n = n + 1
print(s)
A. 211 Answer
B. 210
C. 120
D. 123
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 3/41
10/24/2017 Testbank (Chapter 4)
Title What is the output of the following while loop?
Section 4.1 The while Loop
11. What will be the result of running the following code fragment?
year = 0
rate = 5
principal = 10000
interest = 0
while year < 10 :
interest = (principal * year * rate) / 100
print("Interest ", interest)
A. The code fragment will display the interest calculated for nine years.
B. The code fragment will continue to display the calculated interest forever because the loop will
never end. Answer
C. The code fragment will not display the calculated interest and halt abruptly.
D. The code fragment will not display any output because it will not compile.
Title What is result of while loop?
Section 4.1 The while Loop
12. Which of the following code snippets displays the output exactly 10 times?
A.
i = 0
while i <= 10 :
print("This is example 1.")
i = i + 1
B.
i = 0
while i < 10 :
print("This is example 2.")
i = i + 1
Answer
C.
i = 0
while i < 10 :
print("This is example 3.")
D.
i = 1
while i < 10 :
print("This is example 4.")
i = i + 1
i = 1
while i != 9 :
print(i , end = " ")
i = i + 1
if i == 9 :
print("End")
A. 1 End
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 4/41
10/24/2017 Testbank (Chapter 4)
14. What changes do you need to make in the following code snippet to display Let us learn Python exactly 10
times?
i = 0
while i <= 10 :
print("Let us learn Python")
i = i + 1
A.
while i < 9
B.
while i < 11
C.
while i < 12
D.
i = 1
Answer
15. How many times is the text Let's have fun with Python. printed when this code snippet is run?
i = 0
while i <= 10 :
print("Let's have fun with Python.")
i = i + 1
if i % 2 == 0 :
i = 10
A. 1
B. 2
C. 3 Answer
D. 10
Title How many times does do loop with nested if execute?
Section 4.1 The while Loop
16. Select the statement that correctly completes the loop in this code snippet.
years = 20
rate = 0.05
balance = 10000
while years > 0 :
# Place code here
interest = balance * rate / 100
balance = balance + interest
A.
years = years + 1
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 5/41
10/24/2017 Testbank (Chapter 4)
B.
years = years - 1
Answer
C.
balance = balance + 1
D.
balance = balance - 1
b = False
while b != b :
print("Do you think in Python?")
i = 1
while i < 20 :
print(i , " ")
i = i + 2
if i == 15 :
i = 19
A. 1 3 5 7 9 11 13 15 17 19
B. 1 3 5 7 9 11 13 19 Answer
C. 1 3 5 7 9 11 13 15 17
D. 1 3 5 7 9 11 13 17 19
Title What is output of while loop with nested if?
Section 4.1 The while Loop
19. What are the values of i and j after the following code snippet is run?
i = 10
j = 20
count = 0
while count < 5 :
i = i + i
i = i + 1
j = j - 1
j = j - j
count = count + 1
print("i = ", i , ", j = ", j)
A. i = 45, j = 1
B. i = 351, j = 0 Answer
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 6/41
10/24/2017 Testbank (Chapter 4)
C. i = 351, j = 2
D. i = 1311, j = 35
Title What are values of i and j after while loop?
Section 4.1 The while Loop
i = 1
sum = 0
while i <= 15 :
sum = sum + i
i = i + 1
print("The value of sum is ", sum)
21. What are the values of i and j after the following code fragment runs?
i = 60
j = 50
count = 0
while count < 5 :
i = i + i
i = i + 1
j = j - 1
j = j - j
count = count + 1
print("i=", i, ", j=", j)
A.
i = 1951, j = 0
Answer
B.
i = 1951, j = 45
C.
i = 65, j = 1
D.
i = 65, j = 45
22. Which error type does the "off-by-one" error belong to?
A. Logic error
B. Compile-time error
C. Run-time error Answer
D. Infinite loop
Title What type of error is an off-by-one error?
Section 4.1 The while Loop
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 7/41
10/24/2017 Testbank (Chapter 4)
i = 0
while i != 11 :
print(i , end = " ")
i = i + 3
A. 0 3 6 9 12
B. 0 3 6 9 12 15 18
C. 013579
D. No output (infinite loop) Answer
Title What is output of while loop?
Section 4.1 The while Loop
24. How many times does the following code fragment display "Hi"?
i = 10
while i >= 0 :
print("Hi")
i = i - 1
A. 9 times
B. 10 times
C. 11 times Answer
D. 12 times
Title How many times does while loop execute?
Section 4.1 The while Loop
i = 1
sum = 0
while i <= 11 :
sum = sum + i
i = i + 1
print("The value of sum is ", sum)
n = 0
while n * n < 100 :
print(n * n, end = " ")
n = n + 1
A. 0 1 4 9 16 25 36 49 64 81 Answer
B. 0 1 2 3 4 5 6 7 ... 99 100
C. 0 1 1 2 3 5 8 13 21 34 55 89
D. 0 0 0 0 0 0 (infinite loop)
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 8/41
10/24/2017 Testbank (Chapter 4)
Title What is output of this while loop?
Section 4.1 The while Loop
i = 0
total = 0
while total < 0 :
i = i + 1
total = total - i
print(i, total)
A. 00
B. 11
C. No output Answer
D. 0 -1
Title What does this code snippet produce?
Section 4.1 The while Loop
i = 0
j = 1
while j >= 1 :
print("" , i , ";" , j)
i = n + 1
if i % 2 == 0 :
j = j - 1
A. 0 times
B. 1 times
C. 2 times Answer
D. 4 times
Title How many times does do loop with nested if execute?
Section 4.1 The while Loop
s = "abcde"
length = len(s)
i = 1
while i < length :
print(s[i])
i = i + 1
A. No output
B. abcd
C. abcde
D. bcde Answer
Title What is the output of while loop with the slice operator?
Section 4.1 The while Loop
s = "abcde"
i = 1
while i < 5 :
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 9/41
10/24/2017 Testbank (Chapter 4)
if i > 1 :
print(s[i])
A. No output
B. No output (infinite loop) Answer
C. abcde
D. bcde
Title What is output of while loop with nested if?
Section 4.1 The while Loop
s = "abcde"
j = len(s)
while j >= 0 :
print(s[j])
j = j - 1
A. abcd
B. bcde
C. bcbcd
D. edcba Answer
Title What is output of the while loop?
Section 4.1 The while Loop
s = "12345"
i = 0
while i < 5 :
print(s[i])
i = i + 1
A. No output
B. 1234
C. 12345 Answer
D. 2345
Title What is output of while loop?
Section 4.1 The while Loop
s = "12345"
i = 1
while i < 5 :
if i > 1 :
print(s[i])
A. No output
B. No output (infinite loop) Answer
C. 12345
D. 2345
Title What is output of while loop with nested if?
Section 4.1 The while Loop
34. How many times does the code snippet below display "Hello"?
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 10/41
10/24/2017 Testbank (Chapter 4)
i = 0
while i != 15 :
print("Hello")
i = i + 1
A. Infinite times
B. 14 times
C. 15 times Answer
D. 16 times
Title How many times does while loop display result?
Section 4.1 The while Loop
i = 0
while i != 11 :
print(" " , i)
i = i + 2
A. No output
B. 02468
C. 10 12 14 16 18…. (infinite loop)
D. No output … (infinite loop) Answer
Title What is output of while loop?
Section 4.1 The while Loop
i = 0
j = 1
while j >= 1 :
print(i , ";" , j)
i = i + 1
if i % 3 == 0 :
j = j - 1
A. 1 time
B. 2 times
C. 3 times Answer
D. 4 times
Title How many times does this loop with a nested if run?
Section 4.1 The while Loop
token = False
while token :
print("Hello")
A. "Hello" will continue to be displayed until the user stops the program.
B. No output because of compilation error.
C. No output after successful compilation. Answer
D. "Hello" will be displayed only once.
Title What is output of while loop with Boolean condition?
Section 4.1 The while loop
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 11/41
10/24/2017 Testbank (Chapter 4)
39. How many copies of the letter A are printed by the following loop?
i = 0
while i < 5 :
print("A")
i = i + 1
A. 0
B. 4
C. 5 Answer
D. Infinity
Title How many copies of the letter A are printed by the following loop?
Section 4.1 The while Loop
40. How many copies of the letter B are printed by the following loop?
i = 0
while i == 5 :
print("B")
i = i + 1
A. 0 Answer
B. 4
C. 5
D. Infinity
Title How many copes of the letter B are printed by the following loop?
Section 4.1 The while Loop
41. How many copies of the letter C are printed by the following loop?
i = 0
while i < 5 :
print("C")
i = i - 1
A. 0
B. 4
C. 5
D. Infinity Answer
Title How many copies of the letter C are printed by the following loop?
Section 4.1 The while Loop
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 12/41
10/24/2017 Testbank (Chapter 4)
42. What is the value of i at the end of the following code segment?
i = 1
while i < 32 :
i = i * 2
A. 16
B. 31
C. 32 Answer
D. 64
Title What is the value of i at the end of this loop?
Section 4.1 The while Loop
43. The following while loop should continue to run as long as the user does not enter a negative number.
What condition should be used to achieve this behavior?
A. x != 0
B. x == 0
C. x <= 0
D. x >= 0 Answer
44. What are the final values of the variables i, j, and n at the end of this loop?
i = 0
j = 12
n = 0
while i != j :
i = i + 2
j = j - 2
n = n + 1
A. 2 10 1
B. 482
C. 6 6 3 Answer
D. 0 12 0
Title What are final values of three variables in a for loop?
Section 4.2: Problem Solving: Hand-Tracing
45. When hand-tracing the loop in the code snippet below, which variables are important to evaluate?
i = 10
j = 5
k = -10
sum = 0
while i > 0 :
sum = sum + i + j
i = i - 1
print("Iteration: ", i)
46. When hand tracing, drawing a line through the value stored in a variable means that
A. The value stored there has changed to something new Answer
B. The variable is the wrong data type for the code being executed
C. The expression being evaluated uses that variable
D. The variable must be inside a loop
Title What does it mean to draw a line through values when hand-tracing?
Section 4.2 Problem solving: hand-tracing
47. When hand-tracing a portion of code, which statement about Boolean conditions is true?
A. They typically are too complex to be evaluated.
B. They do not need to be monitored because their result usually is not stored in a variable.
C. It is rare to encounter a Boolean condition.
D. They are crucial to evaluate since they determine if-statement conditions and looping. Answer
Title Which statement about Boolean conditions is true?
Section 4.2 Problem solving: hand-tracing
s = 1
n = 1
while s < 3 * n :
s = s + n
print(s , end = " ")
n = n + 1
A. 2 4 7 11 16 22 Answer
B. 13579
C. 23567
D. 2468
Title What is output of do-while loop?
Section 4.2 Problem Solving: Hand-Tracing
49. What are the values of i and j after the following code snippet executes?
i = 60
j = 50
count = 0
while count < 5 :
i = i + i
i = i + 1
j = j - 1
j = j - j
count = count + 1
print(i)
print(j)
A. i = 65, j = 1
B. i = 65, j = 45
C. i = 1951, j = 0 Answer
D. i = 1951, j = 45
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 14/41
10/24/2017 Testbank (Chapter 4)
Title What are values of variables after while loop executes?
Section 4.2 Problem Solving: Hand-Tracing
s = "aeiou"
i = 0
while i < 5 :
print(s[i], s[i + 1], end = " ")
i = i + 1
if i >= 3 :
i = 5
A. a
B. ae
C. aeiou
D. aeeiio Answer
Title What is output of the loop with nested if?
Section 4.2 Problem Solving: Hand-Tracing
value = 15
x = int(input("Enter an integer: "))
while x != 0 :
value = value * 2
print(value + 3)
x = int(input("Enter an integer: "))
A. 0 Answer
B. 2
C. 3
D. 15
Title What is the sentinel value in the following code segment?
Section 4.3 Application: Processing Sentinel Values
53. What value should the user enter to cause the following while loop to terminate?
done = False
while not done :
x = float(input("Enter a number: "))
if x > 5.0 :
print(x)
elif x > 0.0 :
done = False
elif x < -5.0 :
print(-x)
else :
done = True
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 15/41
10/24/2017 Testbank (Chapter 4)
A. -7.5
B. -2.5 Answer
C. 2.5
D. 7.5
Title What value will cause the while loop to terminate?
Section 4.3 Application: Processing Sentinel Values
val1 = True
val2 = False
while val1 :
if val1 :
print("Hello")
val1 = val2
56. Which statement is correct about the execution of the loop in the following code fragment?
token1 = True
while token1 :
for i in range(0,10) :
print("Hello")
token1 = False
A. No output.
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 16/41
10/24/2017 Testbank (Chapter 4)
age = 0
sumOfAges = 0
stop = 1
age = input("Enter an age (-1 to stop):")
while age != -1 :
sumOfAges = sumOfAges + age
age = input("Enter an age (-1 to stop):")
print("Sum of ages ", sumOfAges)
A. 0
B. 1
C. 2
D. -1 Answer
Title Which is the sentinel in this snippet?
Section 4.3 Application: Processing Sentinel Values
59. What will be the final output of the following code snippet when a user enters input values in the order 10,
20, 30, 40, 50, and -1?
sum = 0
count = 0
salary = 0
average = 0
while salary != -1 :
salary = input("Enter salaries (-1 to stop): ")
if salary != -1 :
sum = sum + salary
count = count + 1
if count > 0 :
average = sum / count
print("The average salary: ", average)
else :
print("No data!")
60. Insert a statement that will correctly terminate this loop when the end of input is reached.
done = False
while done != True :
x = input("Enter a value")
if x == "Q" :
_________________
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 17/41
10/24/2017 Testbank (Chapter 4)
A.
stop
B.
done = 1
C.
exit
D.
done = True
Answer
61. Storyboards are a helpful part of the design process because the storyboard develops
A. A pseudocode description of the algorithm being designed
B. The mathematical formulas required for computing a correct answer
C. The information needed to solve the problem, and how to present that information Answer
D. The amount of time and space needed to find a solution
Title What is the role of the storyboard?
Section 4.4 Problem Solving: Storyboards
63. Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution
of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which
must be converted into roll-out for all different gear combinations. How can the flow of user interaction
for this problem be designed?
A. Hand-tracing can confirm code that implements gear selection.
B. Pseudocode can guide algorithm design through divide-and-conquer strategy.
C. A storyboard can be used. Answer
D. The physical gears can lead to ideas for the correct algorithm to use.
Title How can user interaction be designed for an example problem?
Section 4.4 Problem Solving: Storyboards
str = "abcdEfghI"
found = False
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 18/41
10/24/2017 Testbank (Chapter 4)
count = 0
while found == False :
if str(count).isupper() :
print(letter)
found = True
count = count + 1
A. 9 times
B. 8 times
C. 5 times Answer
D. 1 time
Title Finding the first match
Section 4.5 Common Loop Algorithms
66. Given the following code snippet, what should we change to make sure the user did not enter the same
letter multiple times?
A.
letter == letter
B.
alphabet[0] == letter
C.
letter == previous
Answer
D.
alphabet[0] == previous
67. What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5?
total = 0
validNumber = False
while not validNumber :
value = int(input("Please enter a positive value < 100: "))
if value > 0 and value < 100 :
total = total + value
else :
print("Invalid input.")
validNumber = True
print(total)
A. 15
B. 14
C. 12
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 19/41
10/24/2017 Testbank (Chapter 4)
D. 10 Answer
Title What is the output of this code snippet with this user input?
Section 4.5 Common Loop Algorithms
a = 10
while a > 5 :
print(a , end = " ")
a = a - 2
A. 10 9 8 7 6 5
B. 10 8 6 4
C. 10 8 6 Answer
D. 10 8
Title What will be printed by the statements below?
Section 4.5 Common Loop Algorithms
a = 10
while a > 5 :
a = a - 2
print(a , end = " ")
A. 10 8 6
B. 10 8 6 4
C. 86
D. 8 6 4 Answer
Title What will be printed by the statements below?
Section 4.5 Common Loop Algorithms
val = 1
sum = 0
while val < 5 :
sum = sum + val
val = val + 1
print(sum)
A. 4
B. 5
C. 10 Answer
D. 15
Title What will be printed by the statements below?
Section 4.5 Common Loop Algorithms
val = 1
sum = 0
while val < 5 :
sum = 0
sum = sum + val
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 20/41
10/24/2017 Testbank (Chapter 4)
val = val + 1
print(sum)
A. 15
B. 10
C. 5
D. 4 Answer
Title What will be printed by the statements below?
Section 4.5 Common Loop Algorithms
A. 0 1 2 3 4 5 6 7 8 9 10
B. 0 1 2 3 4 5 6 7 8 9 Answer
C. 02468
D. 013579
Title What will be printed by the statements below?
Section 4.5 Common Loop Algorithms
A. 10 9 8 7 6 5
B. 10 9 8 7 6 Answer
C. 5 6 7 8 9 10
D. 6 7 8 9 10
Title What will be printed by the statements below?
Section 4.5 Common Loop Algorithms
74. Which of the following loops will print the odd numbers between 0 and 20?
A.
num = 1
while num < 20 :
print(num , " ")
num = num + 2
Answer
B.
num = 1
while num < 20 :
print(num , " ")
num = num + 1
C.
num = 0
while num < 20 :
print(num , " ")
num = num + 2
D.
num = 1
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 21/41
10/24/2017 Testbank (Chapter 4)
75. Which of the following loops will print the odd numbers between 0 and 20?
A.
num = 1
while num < 11 :
value = num * 2 - 1
print(value , " ")
num = num + 1
Answer
B.
num = 1
while num < 20 :
value = num * 2 - 1
print(value , " ")
num = num + 1
C.
num = 1
while num < 10 :
print(num , " ")
num = num + 2
D.
num = 1
while num < 20 :
num = num + 2
print(num , " ")
76. Which of the following conditions can be added to the code below so it will loop until the value of sum is
greater than 100?
A.
sum != 0
B.
sum <= 100
Answer
C.
sum > 100
D.
sum == 100
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 22/41
10/24/2017 Testbank (Chapter 4)
Title Which of the following conditions can be added to the code below so it will loop until the value
of sum is greater than 100?
Section 4.5 Common Loop Algorithms
sum = 0
count = 0
value = input("enter an integer")
while value > 0 :
sum = sum + value
count = count + 1
value = input("enter next integer")
result = sum * 1.0 / count
print(result)
position = 0
str = input("Enter a string")
while position < len(str) and str[position] != 'e' :
position = position + 1
print(position)
A. The position of the first 'e' in the string or the length of the string if there is no 'e' Answer
B. The position of the last 'e' in the string or the length of the string if there is no 'e'
C. The position of the first character that is not an 'e' in the string or the length of the string if there is
no character that is not an 'e'
D. The position of the last character that is not an 'e' in the string or the length of the string if there is
no character that is not an 'e'
Title What does the method below return?
Section 4.5 Common Loop Algorithms
A. +0+00+000+0000
B. +000+000+000+000
C. ++0+00+000 Answer
D. ++++000000
Title What is the output of the code below?
Section 4.5 Common Loop Algorithms
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 23/41
10/24/2017 Testbank (Chapter 4)
num = 1
for val in range(0, 4) :
sum = val
for x in range(0, val, num) :
sum = sum + x
print(sum , end = " ")
A. 136
B. 1236
C. 1 2 3 3 4 6 Answer
D. 01233
Title What is the output of the code below?
Section 4.5 Common Loop Algorithms
i = 0
found = False
while i < 100 and found != True :
i = i + 1
print(i, end = " ")
j = i * i
if i * i * i % j == j :
found = True
A. 10 times
B. 20 times
C. 100 times Answer
D. An infinite number of times
Title How many times does the following loop execute?
Section 4.5 Common Loop Algorithms
82. Which code snippet produces the sum of the first n positive even numbers?
A.
sum = 0
for i in range(1, n) :
if i % 2 == 0 :
sum = sum + i
B.
sum = 0
for i in range(1, n + 1) :
sum = sum + i * 2
C.
sum = 0
for i in range (0, n) :
if i % 2 == 0 :
sum = sum + i
D.
sum = 0
for i in range(1, n) :
sum = sum + i * 2
Answer
Title Which code snippet produces the sum of the first n even numbers?
Section 4.5 Common Loop Algorithms
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 24/41
10/24/2017 Testbank (Chapter 4)
i = 0
found = False
while i < 20 and found != True :
sum = i * 2 + i * 3
print(sum, " ")
if sum > 50 :
found = True
i = i + 1
A. 0 5 10 15 20 25 30 35 40 45 50 55
B. 0
C. No output, compilation error Answer
D. 0 5 10
Title What is the output of loop with Boolean?
Section 4.5 Common Loop Algorithms
84. The following program is supposed to sum all of the numbers entered by the user. What line of code must
be inserted in the blank so that the program will achieve this goal?
total = 0.0
inputStr = input("Enter a value: ")
while inputStr != "" :
value = float(inputStr)
____________________
inputStr = input("Enter a value: ")
Title Complete the while loop that sums the numbers entered by the user
Section 4.5 Common Loop Algorithms
85. The following program is supposed to count how many even numbers are entered by the user. What line of
code must be inserted in the blank so that the program will achieve this goal?
evens = 0
inputStr = input("Enter a value: ")
____________________
value = int(inputStr)
if value % 2 == 0:
evens = evens + 1
inputStr = input("Enter a value: ")
A. while inputStr != 0 :
B. while inputStr % 2 == 0 :
C. while inputStr == 2 or 4 or 6 or 8 or 10 or ... :
D. while inputStr != "" : Answer
Title Complete the while loop that counts the number of even numbers entered by the user
Section 4.5 Common Loop Algorithms
86. The following program is supposed to continue reading values from the user until a value between 25 and
75 is entered. What line of code must be inserted in the blank so that the program will achieve this goal?
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 25/41
10/24/2017 Testbank (Chapter 4)
Title Complete the while loop that reads values until the user enters a value between 25 and 75
Section 4.5 Common Loop Algorithms
87. The following program is supposed to print a message any time the user enters two consecutive values that
are the same. What line of code must be inserted in the blank so that the program will achieve this goal?
A. if value == inputStr :
B. if value == input :
C. if previous == inputStr :
D. if previous == value : Answer
Title Complete the while loop for finding consecutive identical values
Section 4.5 Common Loop Algorithms
counter = 1
for i in range(1, 100) :
counter = counter + 1
print(counter)
A. 100 Answer
B. 49
C. 60
D. 10
Title What is the output of for loop
Section 4.6 The for Loop
fruitName = "banana"
for letter in fruitName :
print(letter, end = " ")
A. banana
B. b a n a n a Answer
C. Nothing, there is a syntax error
D. Nothing, this is an infinite loop
Title What does the for loop print?
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 26/41
10/24/2017 Testbank (Chapter 4)
Section 4.6 The for Loop
for i in range(4) :
for j in range(3) :
print("*", end="")
print()
91. How many times does the loop execute in the following code fragment?
A. 11
B. 12
C. 13 Answer
D. 14
Title How many times does for loop execute?
Section 4.6 The for Loop
92. How many times does the following code snippet display "Loop Execution"?
93. Which of the following is considered an equivalent while loop for this for loop?
s = 0
for i in range(1, 10) :
s = s + i
A.
s = 0
i = 0
while i <= 10 :
s = s + i
i = i + 1
B.
s = 0
i = 1
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 27/41
10/24/2017 Testbank (Chapter 4)
while i < 10 :
s = s + i
i = i + 1
Answer
C.
s = 0
i = 1
while i <= 10 :
s = s + i
i = i + 1
D.
s = 0
i = 0
while i < 10 :
s = s + i
i = i + 1
years = 50
balance = 10000
targetBalance = 20000
rate = 3
for i in range(1 , years + 1) :
if balance >= targetBalance :
i = years + 1
else :
interest = balance * rate / 100
balance = balance + interest
95. What values does counter variable i assume when this loop executes?
A. 20, 14, 8, 2
B. 20, 14, 8, 2, -4
C. 20, 14, 8 Answer
D. 14, 8, 2
Title Which values does the counter variable assume in for loop?
Section 4.6 The for Loop
B.
for i in range(0) :
Answer
C.
for i in range(0, k) :
D.
for i in range( , ) :
f1 = 0
f2 = 1
print(f1, " ")
print(f2, " ")
for i in range(1, 11) :
fRes = f1 + f2
print(fRes, " ")
f1 = f2
f2 = fRes
print()
A. 0 1 5 7 9 11 13 15 17 19 55
B. 0 1 1 2 3 5 8 13 21 34 55 89 Answer
C. 0 1 4 6 8 10 12 14 16 18 34
D. 0 1 6 7 9 12 14 17 19 21 55
Title What is the output of for loop?
Section 4.6 The for Loop
98. How many iterations does the following loop carry out?
A. infinite
B. None because of compilation error
C. 11 times Answer
D. 20 times
Title How many times does the loop execute?
Section 4.6 The for loop
A. 10
B. 9 Answer
C. 8
D. An infinite number of times
Title How many times does the following loop execute?
Section 4.6 The for Loop
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 29/41
10/24/2017 Testbank (Chapter 4)
100. Which of the following for loops will run the loop body 5 times?
A. for i in range(0, 4) :
B. for i in range(0, 5) : Answer
C. for i in range(0, 6) :
D. for i in range(1, 5) :
Title Which of the following for loops will run the loop body 5 times
Section 4.6 The for Loop
101. Which of the following for loops will run the loop body 5 times?
A. for i in range(4, 0, -1) :
B. for i in range(5, 0, -1) : Answer
C. for i in range(5, 1, -1) :
D. for i in range(6, 0, -1) :
Title Which of the following for loops will run the loop body 5 times
Section 4.6 The for Loop
102. What is the value of j at the end of the following code segment?
j = 0
for i in range(0, 4) :
j = j + i
A. 4
B. 6 Answer
C. 8
D. 10
Title Trace a for loop
Section 4.6 The for Loop
103. What is the value of j at the end of the following code segment?
j = 0
for i in range(1, 10) :
if j < 10 :
j = j + i
A. 0
B. 1
C. 9
D. 10 Answer
Title Trace a for loop
Section 4.6 The for Loop
Which of the following while loops will generate the same output?
A.
i = 0
while i < 10 :
print(i)
i = i + 1
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 30/41
10/24/2017 Testbank (Chapter 4)
Answer
B.
i = 0
while i <= 10 :
print(i)
i = i + 1
C.
i = 1
while i < 10 :
print(i)
i = i + 1
D.
i = 1
while i <= 10 :
print(i)
i = i + 1
j = 10
while j >= 5 :
print("X")
j = j - 1
Which of the following for loops will generate the same output?
A.
for j in range(10, 5) :
print("X")
B.
for j in range(10, 5, -1) :
print("X")
C.
for j in range(10, -1, -2) :
print("X")
Answer
D.
for j in range(0, 5) :
print("X")
106. When does the execution switch from the inner to the outer loop?
j = 1
for i in range(0, 10) :
while(j < 5) :
print("Hello")
if j == 2 :
j = 6
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 31/41
10/24/2017 Testbank (Chapter 4)
j = j + 1
print("switch from inner to outer", i, " ", j)
108. What is the first and last value of i to be displayed by the following code snippet?
n = 20
for i in range(0, n) :
for j in range(0, i) :
print(i)
A. 0 and 20
B. 1 and 20
C. 0 and 19
D. 1 and 19 Answer
Title What are first and last values displayed by nested for loop?
Section 4.7 Nested Loops
109. How many times will the output line be printed in the following code snippet?
A. 3 times
B. 6 times
C. 9 times Answer
D. 12 times
Title How many times will inner for loop execute?
Section 4.7 Nested Loops
110. What is the last output line of the code snippet given below?
for i in range(3) :
for j in range(5) :
if i % 2 == j % 2 :
print("*", end="")
else :
print(" ", end="")
print()
A. No output
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 32/41
10/24/2017 Testbank (Chapter 4)
B. * * * * * * … infinite loop
C. * * * *
D. * * * Answer
Title What is output of nested loops?
Section 4.7 Nested Loops
111. What is the last output line of the code snippet given below?
i = 0
j = 0
while i < 10 :
num = 1
j = i
while j > 1 :
print(j, end = " ")
num = num * 2
j = j - 1
print("***")
i = i + 1
A. 3 2 ***
B. 9 8 7 6 5 4 3 2 *** Answer
C. 8 7 6 5 4 3 2 ***
D. 2 ***
Title What is output of nested loops?
Section 4.7 Nested Loops
113. Which for loop prints data across each row in the following code snippet?
for i in range(1, 4) :
for j in range(1, 4) :
print("X", end="")
print("")
for i in range(0,7) :
for j in range(7, i, -1) :
print("*", end="")
print("")
A. A rectangle with six rows and seven columns of asterisks. The number of rows increments by one
on completion of one iteration of the inner loop.
B. A right triangle with six rows and seven columns of asterisks. The number of columns increments
by one on completion of one iteration of the inner loop.
C. A rectangle with seven rows and six columns of asterisks. The number of rows increments by one
on completion of one iteration of the inner loop.
D. A right triangle with six rows and seven columns of asterisks. The number of columns decrements
by one on completion of one iteration of the inner loop. Answer
Title What is output of nested for loops?
Section 4.7 Nested Loops
115. In the following code snippet, how many times will "Hello" be printed?
A. 40 Answer
B. 15
C. 39
D. 14
Title When does execution switch from inner to outer loop in snippet?
Section 4.7 Nested Loops
B.
while i < 0 :
while x == 10 :
Answer
C.
if i < 0 :
while x == 10 :
D.
if i < 0 :
if x == 10 :
for i in range(4) :
____________________
print("*", end="")
print()
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 34/41
10/24/2017 Testbank (Chapter 4)
***
***
***
***
Which line of code should be placed in the blank to achieve this goal?
A. for j in range(3) Answer
B. for j in range(4)
C. for j in range(i)
D. for j in range(j)
Title Draw a pattern with nested for loops
Section 4.7 Nested Loops
118. How many copies of the letter A will the following code segment display?
for i in range(100) :
for j in range(5) :
print("A")
A. 400
B. 495
C. 500 Answer
D. 605
Title How many copies of the letter A will be displayed?
Section 4.7 Nested Loops
str = "ABCabc"
i = 0
while i < len(str) :
ch = str[i]
if islower(ch) :
print(i , " ")
else :
i = i + 1
A. 345
B. 3
C. 3 3 3 3 3 ... (infinite loop) Answer
D. 012
Title What is the output of code snippet with while loop?
Section 4.8 Processing strings
120. Consider the following code segment. It is supposed to count the number of digits (0 - 9) in a string, text.
count = 0
for char in text :
____________________
count = count + 1
What line of code should be placed in the blank to achieve this goal?
A. if text[char] >= "0" and text[char] <= "9" :
B. if text[count] >= "0" and text[count] <= "9" :
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 35/41
10/24/2017 Testbank (Chapter 4)
s = "1234"
for i in range (0, 4) :
print(s[i], s[i + 1])
A. Yes.
B. No; there should not be a colon at the end of line 2.
C. No; for i = 3, s[i + 1] will result in an string index out of range error. Answer
D. No; for i = 0, s[i] will result in an string index out of range error
Title Are there errors in for loop?
Section 4.8 Processing Strings
found = False
position = 0
text = "Hello World!"
while not found and position < len(text) :
if text[position] == "o" :
found = True
else :
position = position + 1
123. Consider the following code segment. It is designed to identify the first location within a string, text
where two adjacent characters are the same.
i = 1
found = False
while not found and i < len(text) :
____________________ :
found = True
else :
i = i + 1
What line of code should be placed in the blank to achieve this goal?
A. if text[i] == text[0] :
B. if text[i] == text[i - 1] : Answer
C. if text[i] == text[i] :
D. if text[i] == text[i + 1] :
Title Complete the loop that finds the location of two adjacent characters that are the same
Section 4.8 Processing Strings
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 36/41
10/24/2017 Testbank (Chapter 4)
124. What will be the range of the random numbers generated by the following code snippet?
A. Between 1 and 49
B. Between 0 and 50
C. Between 0 and 49
D. Between 1 and 50 Answer
Title What is range of random numbers generated by snippet?
Section 4.9 Application: Random Numbers and Simulations
125. Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers
on two dice between 2 and 12 with the same probability as when throwing actual dice?
A.
randint(1, 6)
B.
randint(2, 12)
C.
randint(1, 6) + randint(1, 6)
Answer
D.
randint(1, 12) - 2
Title Which code simulates throwing two dice and summing the result?
Section 4.9 Application: Random Numbers and Simulations
126. Suppose that a program asks a user to enter multiple integers, either positive or negative, to do some
calculation. The data entry will stop when the user enters a certain value to indicate the end of the data.
What value should the code use as the sentinel?
A. 0
B. -1
C. 999
D. An alphabetic character Answer
Title What should be the sentinel value in this situation?
Section 4.9 Application: Processing Sentinel Values
A. I only
B. II only
C. I and II only
D. I, II, and III Answer
Title Which activities can be computer simulated?
Section 4.9 Application: Random Numbers and Simulations
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 37/41
10/24/2017 Testbank (Chapter 4)
sum = 0
COUNT = 1000
for i in range(1,COUNT + 1) :
sum = sum + randint(0, 100)
print(sum / COUNT)
Answer
B.
found = False
while i < 10 and found != True :
i = i + 1
if i % 10 == 0 :
found = True
C.
i = 0
while i <= 10 :
i = i + 1
D.
i = 0
for i in range (1, 10) :
print(i)
131. Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with
different probabilities?
A.
randint(0, 1)
B.
randint(1, 1)
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 38/41
10/24/2017 Testbank (Chapter 4)
C.
randint(0, 2), " ", randint(0, 2)
D.
randint(0, 1), " ", randint(0, 1)
Answer
132. Which of the following code snippets will generate a random number between 0 and 79?
A.
val = random() % 80
B.
val = random() * 80 - 1
C.
val = random() % 79
D.
val = random() * 80
Answer
133. When will the loop in the following code snippet stop?
sum = 0
count = 0
str = input("Enter values, Q to quit: ")
while count < 100 and str != "Q" :
value = eval(str)
sum = sum + value
count = count + 1
str = input("Enter values, Q to quit: ")
A. I or II
B. II only
C. III only
D. II or III Answer
Title When does do loop with sentinel stop?
Section 4.9 Application: Processing Sentinel Values
134. Assume the following variable has been declared and given a value as shown:
Which of the following will generate a random integer in the range 20 to 20, inclusive, where each value
has an equal chance of being generated?
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 39/41
10/24/2017 Testbank (Chapter 4)
A.
randint (-20, 20)
B.
randint(20) - 41
C.
randint (-20) + 40
D.
randint(41) - 20
Answer
Title Which of the following will generate a random integer in the range 20 to 20, inclusive, where
each value has an equal chance of being generated?
Section 4.9: Application: Random Numbers and Simulations
135. Assume the following variable has been declared and given a value as shown:
What are the smallest and largest values that may be assigned to number?
A. 3, 55
B. 0, 27
C. 3, 57 Answer
D. 0, 26
Title What are the smallest and largest values that may be assigned to number?
Section 4.9: Application: Random Numbers and Simulations
136. Assume the following variable has been declared and given a value as shown:
What are the smallest and largest values that may be assigned to number?
A. 3.0, 5.0 (excluding 5.0) Answer
B. 0.0, 6.0 (excluding 6.0)
C. -3.0, 3.0 inclusive
D. 0.0, 3.0 inclusive
Title What are the smallest and largest values that may be assigned to number?
Section 4.9: Application: Random Numbers and Simulations
137. Which line of code will generate a random integer from 1 up to and including 10, and store it in x?
Assume that the randint function has been imported from the random module.
A. x = randint(0, 10)
B. x = randint(0, 11)
C. x = randint(1, 10) Answer
D. x = randint(1, 11)
Title Which line of code will generate a random integer from 1 up to and including 10?
Section 4.9 Application: Random Numbers and Simulations
138. Which line of code will generate a random floating-point number between 0 and 6, and store it in x?
Assume that the random function has been imported from the random module.
A. x = random()
B. x = random() * 6 Answer
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 40/41
10/24/2017 Testbank (Chapter 4)
C. x = random(6)
D. x = random(0, 6)
Title Which line of code will generate a random floating-point number between 0 and 6?
Section 4.9 Application: Random Numbers and Simulations
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 41/41