Loops Flashcards

1
Q

What is the output of the following code?

count = 1
while count < 4:
count += 1
print(count, end = ‘ ‘)

A

2 3 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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
A

E. x is 4

Hint: x is 4 becasue x is incremented four times starting from 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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
A

E. infinite number of times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the output of the following code?

count = 4
while count > 1:
count -= 1
print(count, end = ‘ ‘)

A

4 3 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
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

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
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

(a) and (b) are infinite loops, (c) has an indentation error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you convert any for loop to a while loop? List the advantages of using for loops.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Convert the following for loop statement to a while loop:

sum = 0
for i in range(1001):
sum = sum + i

A
while loop:
sum = 0
i= 0
while i <= 1000:
    sum += i 
    i += 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Convert the following while loop into a for loop.
i = 1
sum = 0

while sum < 10000:
sum = sum + i
i += 1

A

sum = 0
for i in range(1, 10000):
if sum < 10000:
sum = sum + i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

B. The program has a syntax error because the arguments in the range must be integers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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
A

E. 45

Hint: y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45v

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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
A

D. 20

Hint: i is 0, 2, 4, 6, 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the output for y?

y = 0
for i in range(10, 1, -2):
y += i

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

C. 30

Hint: i is 10, 8, 6, 4, 2, 0

17
Q

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
A

D. 45

Hint: The outer loop is executed 10 times for i from 0 to 9. For each i, the inner loop is executed i times. So, the println statement is executed 0 + 1 + 2 + … + 9 = 45 times.

18
Q

What is the keyword break for?

A

The keyword break is used to exit the current loop.

19
Q

What is the keyword continue for?

A

The keyword continue causes the rest of the loop body to be skipped for the current iteration.

20
Q

Will the following program terminate?

balance = 10

while True:
if balance < 9:
break
balance = balance - 9

A. Yes
B. No

A

A. Yes

21
Q

Will the following program terminate?

balance = 10

while True:
if balance < 9:
continue
balance = balance - 9

A. Yes
B. No

A

B. No

22
Q

What is sum after the following loop terminates?

sum = 0
item = 0
while item < 5:
    item += 1
    if sum >= 4:
        continue
    sum += item
print(sum)
 A. 5
 B. 6
 C. 7
 D. 8
A

B. 6

Hint: When sum >= 4, item is not added to sum.

23
Q

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
A

B. 6

24
Q

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

A

D. i is 6 isPrime is False

Hint: When i is 5, isPrime is assigned False, i += 1 is executed. So, i becomes 6. Now the loop ends since isPrime is False.

25
Q

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

A

B. n

Hint: Since range(1, n + 1) returns a sequence of 1, 2, …, n. The number of iterations is n.

26
Q

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

number = int(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

A

C. i is 2 followed by 9 is prime

27
Q

A one-time execution of a loop body is referred to as an __________.

A

iteration of the loop

28
Q

There are two types of repetition statements: the ________ and the ________.

A

while loop and the for loop