0% found this document useful (0 votes)
867 views41 pages

Testbank Chapter 4 PDF

The document contains a testbank with 15 multiple choice questions about while loops in Python. The questions cover topics like fixing off-by-one errors, determining the number of loop iterations, understanding loop output, and using conditions to exit loops early. Correct answers are provided for each question.

Uploaded by

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

Testbank Chapter 4 PDF

The document contains a testbank with 15 multiple choice questions about while loops in Python. The questions cover topics like fixing off-by-one errors, determining the number of loop iterations, understanding loop output, and using conditions to exit loops early. Correct answers are provided for each question.

Uploaded by

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

10/24/2017 Testbank (Chapter 4)

Title 4 Loops
Book Python for Everyone
Edition 1

1. Which statement corrects the off-by-one error in the following code:

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

Title How do you fix an off-by-one error?


Section 4.1 The while loop

2. How many times will the following loop run?

i = 0
while i < 10 :
print(i)
i = i + 1

A. 0
B. 8
C. 9
D. 10 Answer

Title How many iterations of while loop?


Section 4.1 The while loop

3. What is the output of the code snippet given below?

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

5. What is the output of the code fragment given below?

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

Title What is output of while loop?


Section 4.1 The while Loop

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

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. The code snippet contains a compile error.


B. The code snippet displays the desired result. Answer
C. The code snippet displays an incorrect result.
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 2/41
10/24/2017 Testbank (Chapter 4)

D. The code snippet causes an infinite loop.


Title Will the while loop with if/else produce desired result?
Section 4.1 The while Loop

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

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

9. What is the output of the code fragment given below?

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

10. What is the output of the following 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

Title Which while loop executes 10 times?


Section 4.1 The while Loop

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

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)

B. 1 End (infinite loop)


C. 1 2 3 4 5 6 7 8 End Answer
D. 1 2 3 4 5 6 7 8 End (infinite loop)
Title What is output of while loop?
Section 4.1 The while Loop

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

Title What changes needed so while loop executes 10 times?


Section 4.1 The while Loop

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

Title Insert appropriate code in while loop


Section 4.1 The while Loop

17. Is the following code snippet legal?

b = False
while b != b :
print("Do you think in Python?")

A. Yes, it is legal but does not print anything. Answer


B. Yes, it is legal and prints "Do you think in Python?" once.
C. Yes, it is legal and prints "Do you think in Python?" twice.
D. No, it is not legal and gives a compilation error.
Title While loops with Boolean conditions
Section 4.1 The while Loop

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

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

20. What is the output of the following code fragment?

i = 1
sum = 0
while i <= 15 :
sum = sum + i
i = i + 1
print("The value of sum is ", sum)

A. The value of sum is 0


B. The value of sum is 105
C. The value of sum is 120 Answer
D. The value of sum is 136
Title What is the output of while loop that sums?
Section 4.1 The while Loop

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

Title What are values of variables after while loop executes?


Section 4.1 The while Loop

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)

23. What is the output of the code snippet given below?

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

25. What is the output of the following code fragment?

i = 1
sum = 0
while i <= 11 :
sum = sum + i
i = i + 1
print("The value of sum is ", sum)

A. The value of sum is 65


B. The value of sum is 66 Answer
C. The value of sum is 55
D. The value of sum is 56
Title What is output of while loop?
Section 4.1 The while Loop

26. What is the output of the code snippet given below?

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

27. What is the last output of the code snippet below?

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

28. How many times does the following loop run?

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

29. What is the output of the code snippet given below?

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

30. What is the output of the code snippet given below?

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

31. What is the output of the code snippet given below?

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

32. What is the output of the code snippet given below?

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

33. What is the output of the code snippet given below?

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

35. What is the output of the code snippet given below?

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

36. How many times does the following loop run?

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

37. What will be the output of the following code snippet?

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)

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


i = 1
while i <= 10 :
print("Inside the while loop")
i = i + 10

A. No output because of compilation error.


B. "Inside the while loop" will be displayed 10 times.
C. No output after successful compilation.
D. "Inside the while loop" will be displayed only once. Answer
Title What is output of while loop?
Section 4.1 The while Loop

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?

x = int(input("Enter an integer: "))


while ____________ :
x = int(input("Enter an integer: "))

A. x != 0
B. x == 0
C. x <= 0
D. x >= 0 Answer

Title Fill in the correct condition for a while loop


Section 4.1 The while Loop

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)

A. The variables i and j


file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 13/41
10/24/2017 Testbank (Chapter 4)

B. The variables i and sum Answer


C. The variables i, j, and k
D. The variables j and k
Title Which variables are important to monitor when hand-tracing a code snippet?
Section 4.2 Problem solving: hand-tracing

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

48. What is the output of this code snippet?

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

50. The process of hand-tracing code is valuable because


A. It is usually faster than just running the code.
B. It is the best way to design an algorithm.
C. You must already have a working program in order to do it.
D. It gives valuable insight that you do not get by running the code. Answer
Title Why is hand tracing valuable?
Section 4.2 Problem Solving: Hand-Tracing

51. What is the output of the code snippet given below?

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

52. What is the sentinel value in the following code segment?

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

54. What happens when the following loop is executed?

val1 = True
val2 = False
while val1 :
if val1 :
print("Hello")
val1 = val2

A. No output will be displayed because of a compilation error.


B. "Hello" will be displayed only once. Answer
C. "Hello" will be displayed an infinite number of times.
D. No output will be displayed even after successful compilation of the code snippet.
Title What is output of while loop with nested if and Boolean conditions?
Section 4.3 Application: Processing Sentinel Values

55. Which of the following statements is correct about a sentinel?


A. A sentinel is a value that creates a bridge between a data set and unrelated input.
B. A sentinel is a value that is part of the data to be processed by the program.
C. A sentinel is a value that terminates a program.
D. A sentinel is a value that indicates the end of an input sequence. Answer
Title Which statement about sentinels is correct?
Section 4.3 Application: Processing Sentinel Values

56. Which statement is correct about the execution of the loop in the following code fragment?

num = input("Please enter a number (0 when done): ")


incr = 0
while num != 0 :
incr = incr + 1
num = input("Please enter a number (0 when done): ")
print("", incr)

A. The loop will execute only when 0 is entered.


B. The execution of the loop is independent of user input.
C. The program prints the count of positive inputs.
D. The program prints the count of inputs not equal to zero. Answer
Title Which statement is true about do loop with user input?
Section 4.3 Application: Processing Sentinel Values

57. What will be the output of the following code snippet?

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)

B. Hello will be displayed 10 times. Answer


C. Hello will be displayed 9 times.
D. Hello will be displayed infinite times.
Title What is output of while loop with Boolean condition?
Section 4.3 Application: Processing Sentinel Values

58. What is the sentinel value in the following code snippet?

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

A. The average salary: 0


B. The average salary: 30 Answer
C. The average salary: 24.83333
D. There will be no output as the code snippet will not compile.
Title What is output of snippet with input that includes a sentinel?
Section 4.3 Application: Processing Sentinel Values

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

Title Insert code to terminate a loop


Section 4.3 Application: Processing Sentinel Values

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

62. When designing storyboards, it is a good idea to use different colors to


A. Make it easy to distinguish between user input and program output. Answer
B. Match the colors your program will use when it is finally designed.
C. Emphasize the difference between numbers and words.
D. Draw lines to divide up panels into different regions.
Title What is the role of colors when designing using storyboards?
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

64. Which statement about storyboards is true?


A. A storyboard can help prevent potential user confusion early in the design process. Answer
B. Storyboards are used primarily to understand how implemented programs work.
C. The storyboard helps to train users about how to use software.
D. Storyboards have no relationship to the structure of an actual working program.
Title Which statement about storyboards is true?
Section 4.4 Problem Solving: Storyboards

65. How many times does the while loop execute?

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?

letter = input("Enter the next letter in the alphabet: ")


alphabet = letter
while letter != "":
previous = letter
letter = input("Enter the next letter")
if ________________________ :
print("Duplicate input")
else:
alphabet = alphabet + letter

A.
letter == letter

B.
alphabet[0] == letter

C.
letter == previous

Answer

D.
alphabet[0] == previous

Title Compare adjacent values


Section 4.5 Common Loop Algorithms

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

68. What will be printed by the statements below?

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

69. What will be printed by the statements below?

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

70. What will be printed by the statements below?

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

71. What will be printed by the statements below?

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

72. What will be printed by the statements below?

for ctr in range(0, 10) :


print(ctr , end = " ")

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

73. What will be printed by the statements below?

for ctr in range(10, 5, -1) :


print(ctr , end = " ")

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)

while num < 20 :


num = num + 2
print(num , " ")

Title What will be printed by the statements below?


Section 4.5 Common Loop Algorithms

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

Title What will be printed by the statements below?


Section 4.5 Common Loop Algorithms

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?

sum = input("enter an integer")


while # Put condition here :
sum = sum + input("Enter an integer")

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

77. What does the following loop compute?

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)

A. The average of all the integers in the input


B. The sum of all the positive integers in the input divided by the number of integers in the input
C. The average of all the positive integers in the input Answer
D. The second smallest value in the input
Title What does the following loop compute?
Section 4.5 Common Loop Algorithms

78. What does the method below return?

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

79. What is the output of the code below?

for val in range(0, 4) :


print("+", end = "")
for num in range(0, val) :
print("0", end = "")

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

80. What is the output of the code below?

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

81. How many times does the following loop execute?

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)

83. What is the output of this loop?

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

A. value = value + inputStr


B. value = value + total
C. total = total + inputStr
D. total = total + value Answer

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)

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


____________________
value = int(input("Enter a value: "))

A. while value >= 25 or value <= 75 :


B. while value >= 25 and value <= 75 :
C. while value < 25 or value > 75 : Answer
D. while value < 25 and value > 75 :

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?

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


inputStr = input("Enter a value: ")
while inputStr != "" :
previous = value
value = int(inputStr)
____________________
print("Found consecutive values that are the same")
inputStr = input("Enter a value: ")

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

88. What is the output of this loop?

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

89. What does the following code snippet print?

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

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

for i in range(4) :
for j in range(3) :
print("*", end="")
print()

A. Prints 3 rows of 4 asterisks each


B. Prints 12 rows of asterisks
C. Prints 4 rows of 3 asterisks each Answer
D. Prints 12 rows of 3 asterisks each
Title Which is a loop with a problematic condition?
Section 4.6 The for Loop

91. How many times does the loop execute in the following code fragment?

for i in range(0, 50, 4) :


print(i)

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

for i in range(0, 10) :


print("Loop Execution")

A. Ten times. Answer


B. The code snippet does not run because of a compile error.
C. Infinite loop.
D. Only one time.
Title How many times does for loop execute?
Section 4.6 The for Loop

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

Title Rewrite a for loop using a while loop


Section 4.6 The for Loop

94. Which statement about this code snippet is accurate?

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

A. The loop will run 50 times. Answer


B. The loop will never stop.
C. The loop will run at most 50 times, but may stop earlier when balance exceeds or equals
targetBalance.
D. There is a compilation error.
Title For loop with inside if statement
Section 4.6 The for Loop

95. What values does counter variable i assume when this loop executes?

for i in range(20, 2, -6) :


print(i, end = ",")

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

96. Which of the following for loops is illegal?


A.
for i in range(0, ) :
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 28/41
10/24/2017 Testbank (Chapter 4)

B.
for i in range(0) :

Answer

C.
for i in range(0, k) :

D.
for i in range( , ) :

Title Which of the following for loops is illegal?


Section 4.6 The for Loop

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

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?

for i in range (-10, 11, 2) :

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

99. How many times does the following loop execute?

for d in range(1, 10) :


d = d / 3
print(d , " ")

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

104. Consider the following for loop:

for i in range(0, 10) :


print(i)

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

Title Convert a for loop to a while loop


Section 4.6 The for Loop

105. Consider the following while loop:

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

Title Convert a while loop to a for loop


Section 4.6 The for Loop

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)

A. When the value of j becomes 6 Answer


B. When the program executes completely
C. When the condition for the outer loop is met
D. When the value of i is incremented
Title When does execution switch from inner to outer loop?
Section 4.7 Nested Loops

107. A loop inside another loop is called:


A. A sentinel loop
B. A nested loop Answer
C. A parallel loop
D. A while loop
Title What is a loop inside another loop?
Section 4.7 Nested Loops

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?

for num2 in range(1, 4) :


for num1 in range(0, 3) :
print(num2, " ", num1)

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

112. What does the following code snippet display?

for n in range(1, 11) :


for x in range(1, 11) :
print(n*x, end = " ")
print()

A. It displays a multiplication table for numbers 1-10 times 1-10 Answer


B. Nothing because it has compilation error.
C. It displays a table of all numbers squared from 1-10
D. It displays a multiplication table for numbers 1-11 times 1-11
Title What does for loop with character creation display?
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("")

A. The inner for loop Answer


B. The outer for loop
C. Both for loops
D. Another missing for loop
Title Which for loop in snippet represents rows?
Section 4.7 Nested Loops

114. What will be the output of the following code snippet?


file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 33/41
10/24/2017 Testbank (Chapter 4)

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?

for i in range(0, 10) :


for j in range(1, 5) :
print("Hello")

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

116. Which of the following code segments is an example of a nested loop?


A.
while i < 0 :
if x == 10 :

B.
while i < 0 :
while x == 10 :

Answer

C.
if i < 0 :
while x == 10 :

D.
if i < 0 :
if x == 10 :

Title Which of the following code segments is an example of a nested loop?


Section 4.7 Nested Loops

117. Consider the following code segment:

for i in range(4) :
____________________
print("*", end="")
print()
file:///C:/Horstmann1e_TB/ch04.testbank.xhtml 34/41
10/24/2017 Testbank (Chapter 4)

It is supposed to generate the following output:

***
***
***
***

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

119. What is the output of this code snippet?

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)

C. if char >= "0" and char <= "9" : Answer


D. if text >= "0" and char <= "9" :
Title Complete the for loop that counts the number of digits in a string
Section 4.8 Processing Strings

121. Is the code snippet written below legal?

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

122. Consider the following code segment:

found = False
position = 0
text = "Hello World!"
while not found and position < len(text) :
if text[position] == "o" :
found = True
else :
position = position + 1

What is the value of position at the end of this code segment?


A. 4 Answer
B. 5
C. 7
D. 8
Title Trace a while loop that processes a string
Section 4.8 Processing Strings

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?

from random import randint


randomNum = randint(1,50)

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

127. Which of the following activities can be simulated using a computer?

I. Waiting time in a line at a restaurant

II. Tossing a coin

III. Shuffling cards for a card game

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)

128. What range of numbers are generated by the random() function?


A. greater than or equal to zero and less than one Answer
B. greater than zero and less than one
C. greater than zero and less than or equal to one
D. greater than or equal to zero and less than or equal to one
Title What is output of the code snippet with random()?
Section 4.9 Application: Random Numbers and Simulations

129. What does the following code do?

sum = 0
COUNT = 1000
for i in range(1,COUNT + 1) :
sum = sum + randint(0, 100)
print(sum / COUNT)

A. It simulates the outcome of throwing a coin.


B. It calculates the average of 1000 random numbers between 0 and 100. Answer
C. It performs Monte Carlo simulation of fluid dynamics.
D. It calculates the average of 1000 random numbers between 1 and 101.
Title What does code snippet with random numbers do?
Section 4.9 Application: Random Numbers and Simulations

130. Which of the following loops executes exactly 10 times?


A.
for i in range(0, 11) :
i = 1

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)

Title Which of the following loops executes exactly 10 times?


Section 4.9 Application: Processing Sentinel Values

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

Title Which is correct for simulating the toss of a pair of coins?


Section 4.9 Application: Random Numbers and Simulations

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

Title Which code generates random numbers 0-79?


Section 4.9 Application: Random Numbers and Simulations

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

I. When the user enters an integer

II. When the user enters the character Q

III. After the user enters 100 numbers

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:

from random import randint

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:

from random import randint


number = randint(0, 27) * 2 + 3

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:

from random import random


number = random() * 2 + 3

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy