Introduction to Programming Using Python, Y. Daniel Liang

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 5 Loops


Section 5.2 The while Loop
5.1  How many times will the following code print "Welcome to Python"?

count = 0
while count < 10:
    print("Welcome to Python")
    count += 1
A. 8
B. 9
C. 10
D. 11
E. 0

5.2  What is the output of the following code?

x = 0
while x < 4:
    x = x + 1

print("x is", x)
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4

5.3  Analyze the following code.

 count = 0
 while count < 100:
     # Point A
     print("Welcome to Python!")
     count += 1
     # Point B

 # Point C
A. count < 100 is always True at Point A
B. count < 100 is always True at Point B
C. count < 100 is always False at Point B
D. count < 100 is always True at Point C
E. count < 100 is always False at Point C

5.4  How many times will the following code print "Welcome to Python"?

count = 0
while count < 10:
    print("Welcome to Python")
    
A. 8
B. 9
C. 10
D. 11
E. infinite number of times

5.5  What will be displayed when the following code is executed?

    number = 6
    while number > 0:
        number -= 3
        print(number, end = ' ')
A. 6 3 0
B. 6 3
C. 3 0
D. 3 0 -3
E. 0 -3

Section 5.3 The for Loop
5.6  Analyze the following statement:

sum = 0
for d in range(0100.1):
    sum += sum + d
A. The program has a syntax error because the range function cannot have three arguments.
B. The program has a syntax error because the arguments in the range must be integers.
C. The program runs in an infinite loop.
D. The program runs fine.

5.7  Which of the following loops prints "Welcome to Python" 10 times?

A:
for count in range(110):
    print("Welcome to Python")

B:
for count in range(010):
    print("Welcome to Python")

C:
for count in range(111):
  print("Welcome to Python")

D:
for count in range(112):
  print("Welcome to Python")
A. BD
B. ABC
C. AC
D. BC
E. AB

5.8  The function range(5) return a sequence ______________.
A. 1, 2, 3, 4, 5
B. 0, 1, 2, 3, 4, 5
C. 1, 2, 3, 4
D. 0, 1, 2, 3, 4

5.9  Which of the following function returns a sequence 0, 1, 2, 3?
A. range(0, 3)
B. range(0, 4)
C. range(3)
D. range(4)

5.10  Which of the following function is incorrect?
A. range(0, 3.5)
B. range(10, 4, -1)
C. range(1, 3, 1)
D. range(2.5, 4.5)
E. range(1, 2.5, 4.5)

5.11  Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100?

A:
sum = 0
for i in range(199):
    sum += i / (i + 1)

print("Sum is", sum)

B:
sum = 0
for i in range(1100):
    sum += i / (i + 1)

print("Sum is", sum)

C:
sum = 0
for i in range(1.099.0):
    sum += i / (i + 1)

print("Sum is", sum)

D:
sum = 0
for i in range(1.0100.0):
    sum += i / (i + 1)

print("Sum is", sum)
A. BCD
B. ABCD
C. B
D. CDE
E. CD

5.12  The following loop displays _______________.

for i in range(111):
    print(i, end = " ")
A. 1 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1 2 3 4 5
D. 1 3 5 7 9
E. 2 4 6 8 10

5.13  What is the output for y?

y = 0
for i in range(010):
    y += i

print(y)
A. 10
B. 11
C. 12
D. 13
E. 45

5.14  What is the output for y?

y = 0
for i in range(0102):
    y += i

print(y)
A. 9
B. 10
C. 11
D. 20

5.15  What is the output for y?

y = 0
for i in range(101-2):
    y += i

print(y)
A. 10
B. 40
C. 30
D. 20

5.16  Given the following four patterns,

Pattern A        Pattern B        Pattern C        Pattern D
1                1 2 3 4 5 6                1      1 2 3 4 5 6
1 2              1 2 3 4 5                2 1        1 2 3 4 5
1 2 3            1 2 3 4                3 2 1          1 2 3 4
1 2 3 4          1 2 3                4 3 2 1            1 2 3
1 2 3 4 5        1 2                5 4 3 2 1              1 2
1 2 3 4 5 6      1                6 5 4 3 2 1                1


Which of the pattern is produced by the following code?

for i in range(16 + 1):
    for j in range(6, 0, -1):
       print(j if j <= i else " ", end = " ")
    print()
A. Pattern A
B. Pattern B
C. Pattern C
D. Pattern D

Section 5.5 Minimizing Numerical Errors
5.17  Analyze the following fragment:

sum = d = 0
while d != 10.0:
    d += 0.1
    sum += sum + d
A. The program does not run because sum and d are not initialized correctly.
B. The program never stops because d is always 0.1 inside the loop.
C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

5.18  To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
B. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.

Section 5.6 Case Studies
5.19  How many times is the print statement executed?

for i in range(10): 
    for j in range(10):
        print(i * j)
A. 100
B. 20
C. 10
D. 45

5.20  How many times is the print statement executed?

for i in range(10): 
    for j in range(i):
        print(i * j)
A. 100
B. 20
C. 10
D. 45

Section 5.7 Keywords break and continue
5.21  Will the following program terminate?

balance = 10

while True:
    if balance < 9: break
    balance = balance - 9
A. Yes
B. No

5.22  What is sum after the following loop terminates?

sum = 0
item = 0
while item < 5:
    item += 1
    sum += item
    if sum > 4: break

print(sum)
A. 5
B. 6
C. 7
D. 8

5.23  What is sum after the following loop terminates?

sum = 0
item = 0
while item < 5:
    item += 1
    sum += item
    if sum >= 4: continue

print(sum)
A. 15
B. 16
C. 17
D. 18

5.24  Will the following program terminate?

balance = 10

while True:
    if balance < 9: continue
    balance = balance - 9
A. Yes
B. No

Section 5.8 Case Study: Displaying Prime Numbers
5.25  What will be displayed by after the following loop terminates?

number = 25
isPrime = True
i = 2 
while i < number and isPrime:
    if number % i == 0:
        isPrime = False

    i += 1

print("i is", i, "isPrime is", isPrime)
A. i is 5 isPrime is True
B. i is 5 isPrime is False
C. i is 6 isPrime is True
D. i is 6 isPrime is False

5.26  What will be displayed by after the following loop terminates?

number = 25
isPrime = True
for i in range(2, number):
    if number % i == 0:
        isPrime = False
        break

print("i is", i, "isPrime is", isPrime)
A. i is 5 isPrime is True
B. i is 5 isPrime is False
C. i is 6 isPrime is True
D. i is 6 isPrime is False

5.27  What is the number of iterations in the following loop:

  for i in range(1, n):
      # iteration
A. 2*n
B. n
C. n - 1
D. n + 1

5.28  What is the number of iterations in the following loop:

  for i in range(1, n + 1):
      # iteration
A. 2*n
B. n
C. n - 1
D. n + 1

5.29  Suppose the input for number is 9. What will be displayed by the following program?

number = eval(input("Enter an integer: "))

isPrime = True
for i in range(2, number):
    if number % i == 0:
        isPrime = False

    print("i is", i)

    if isPrime:
        print(number, "is prime")
        break
    else:
        print(number, "is not prime")
A. i is 3 followed by 9 is prime
B. i is 3 followed by 9 is not prime
C. i is 2 followed by 9 is prime
D. i is 2 followed by 9 is not prime