Loops Flashcards
What is the output of the following code?
count = 1
while count < 4:
count += 1
print(count, end = ‘ ‘)
2 3 4
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
C. 10
Hint: The count is initialized to 0 before the loop. The loop is executed 10 times for count from 1 to 9. When count is 10, the loop continuation condition becomes false. The loop is finished. So, the correct answer is C.
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
E. x is 4
Hint: x is 4 becasue x is incremented four times starting from 0.
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
E. infinite number of times
What is the output of the following code?
count = 4
while count > 1:
count -= 1
print(count, end = ‘ ‘)
4 3 2
How many times are the following loop bodies repeated? What is the printout of each loop? (a) i = 1 while i < 10: if i % 2 == 0: print(i)
(b) i = 1 while i < 10: if i % 2 == 0: print(i) i += 1
(c) i = 1 while i < 10: if i % 2 == 0: print(i) i += 1
(a) Infinite number of times.
(b) Infinite number of times.
(c) The loop body is executed nine times. The printout is 2, 4, 6, 8 on separate lines.
Show the errors in the following code: (a) count = 0 while count < 100: print(count)
(b) count = 0 while count < 100: print(count) count -= 1
(c)
count = 0
while count < 100:
count += 1
(a) and (b) are infinite loops, (c) has an indentation error.
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 Your answer B is incorrect Your answer is wrong The correct answer is C. 61.46% of users answered it correctly
C. 3 0
Hint: number is 6 before the loop. In the first iteration, number is reduced to 3. In the second iteration, number is reduced to 0. The loop is now finished. The loop body is executed 2 times for number 6 and 3. Since number is reduced by 3 before the print statement. 3 and 0 and displayed. So, the correct answer is C.Hint: number is 6 before the loop. In the first iteration, number is reduced to 3. In the second iteration, number is reduced to 0. The loop is now finished. The loop body is executed 2 times for number 6 and 3. Since number is reduced by 3 before the print statement. 3 and 0 and displayed. So, the correct answer is C.
Can you convert any for loop to a while loop? List the advantages of using for loops.
Yes. The advantages of for loops are simplicity and readability. Compilers can produce more efficient code for the for loop than for the corresponding while loop.
Convert the following for loop statement to a while loop:
sum = 0
for i in range(1001):
sum = sum + i
while loop: sum = 0 i= 0 while i <= 1000: sum += i i += 1
Convert the following while loop into a for loop.
i = 1
sum = 0
while sum < 10000:
sum = sum + i
i += 1
sum = 0
for i in range(1, 10000):
if sum < 10000:
sum = sum + i
Analyze the following statement:
sum = 0
for d in range(0, 10, 0.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.
B. The program has a syntax error because the arguments in the range must be integers.
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)
A. range(0, 3.5)
D. range(2.5, 4.5)
E. range(1, 2.5, 4.5)
Hint: The arguments in the range function must be integers.
What is the output for y?
y = 0
for i in range(0, 10):
y += i
print(y) A. 10 B. 11 C. 12 D. 13 E. 45
E. 45
Hint: y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45v
What is the output for y?
y = 0
for i in range(0, 10, 2):
y += i
print(y) A. 9 B. 10 C. 11 D. 20 Your answer is correct Your answer is correct 79.24% of users answered it correctly
Hint: i is 0, 2, 4, 6, 8 What is the output for y?
y = 0
for i in range(0, 10, 2):
y += i
print(y) A. 9 B. 10 C. 11 D. 20
D. 20
Hint: i is 0, 2, 4, 6, 8