Chapter 5 - Loops
count < 100 is always True at Point A count < 100 is always False at Point C
Analyze the following code. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C
The program has a syntax error because the arguments in the range must be integers.
Analyze the following statement: sum = 0 for d in range(0, 10, 0.1): sum += sum + d
Pattern C
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(1, 6 + 1): for j in range(6, 0, -1): print(j if j <= i else " ", end = " ") print()
i is 2 followed by 9 is prime
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")
1 2 3 4 5 6 7 8 9 10
The following loop displays _______________. for i in range(1, 11): print(i, end = " ")
0, 1, 2, 3, 4
The function range(5) return a sequence ______________.
add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
6
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)
n
What is the number of iterations in the following loop: for i in range(1, n + 1): # iteration
Yes
Will the following program terminate? balance = 10 while True: if balance < 9: break balance = balance - 9
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
Analyze the following fragment: sum = d = 0 while d != 10.0: d += 0.1 sum += sum + d
100
How many times is the print statement executed? for i in range(10): for j in range(10): print(i * j)
45
How many times is the print statement executed? for i in range(10): for j in range(i): print(i * j)
infinite number of times
How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python")
11
How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") count += 1
15
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)
n-1
What is the number of iterations in the following loop: for i in range(1, n): # iteration
45
What is the output for y? y = 0 for i in range(0, 10): y += i print(y)
20
What is the output for y? y = 0 for i in range(0, 10, 2): y += i print(y)
C
What is the output for y? y = 0 for i in range(10, 1, -2): y += i print(y)
x is 4
What is the output of the following code? x = 0 while x < 4: x = x + 1 print("x is", x)
i is 5 isPrime is False
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)
i is 6 isPrime is False
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)
3 0
What will be displayed when the following code is executed? number = 6 while number > 0: number -= 3 print(number, end = ' ')
range(0, 3.5) range(2.5, 4.5) range(1, 2.5, 4.5)
Which of the following function is incorrect?
range(0, 4) range(4)
Which of the following function returns a sequence 0, 1, 2, 3?
B
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: sum = 0 for i in range(1, 99): sum += i / (i + 1) print("Sum is", sum) B: sum = 0 for i in range(1, 100): sum += i / (i + 1) print("Sum is", sum) C: sum = 0 for i in range(1.0, 99.0): sum += i / (i + 1) print("Sum is", sum) D: sum = 0 for i in range(1.0, 100.0): sum += i / (i + 1) print("Sum is", sum)
BC
Which of the following loops prints "Welcome to Python" 10 times? A: for count in range(1, 10): print("Welcome to Python") B: for count in range(0, 10): print("Welcome to Python") C: for count in range(1, 11): print("Welcome to Python") D: for count in range(1, 12): print("Welcome to Python")
No
Will the following program terminate? balance = 10 while True: if balance < 9: continue balance = balance - 9