Search Header Logo

Python Loop Quiz

Authored by ruth sutton

Other

12th Grade

Used 1+ times

Python Loop Quiz
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

20 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is a for loop used for in Python?

To execute a block of code indefinitely

To iterate over a sequence

To evaluate conditions only

To terminate a program

Answer explanation

A for loop in Python is specifically designed to iterate over a sequence, such as a list, tuple, or string, allowing you to execute a block of code for each item in that sequence.

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the output of the following code? python Copy code for i in range(3): print(i)

1 2 3

0 1 2

0 1 2 3

None

Answer explanation

The code uses a for loop with range(3), which generates numbers starting from 0 up to, but not including, 3. Therefore, the output will be 0, 1, and 2, making the correct answer '0 1 2'.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What keyword is used to stop a while loop in Python?

exit

stop

break

continue

Answer explanation

In Python, the keyword 'break' is used to exit a while loop prematurely. The other options, 'exit', 'stop', and 'continue', do not serve this purpose. 'Continue' actually skips to the next iteration of the loop.

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How many times will this loop run? python Copy code count = 0 while count < 5: count += 1

4

5

Infinite

6

Answer explanation

The loop starts with count = 0 and increments count by 1 each iteration. It runs while count is less than 5. Therefore, it will run 5 times (count = 1 to 5) before exiting, making the correct answer 5.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does range(5) generate?

1 to 5

0 to 5

0 to 4

Infinite values

Answer explanation

The function range(5) generates a sequence of numbers starting from 0 up to, but not including, 5. Therefore, it produces the values 0, 1, 2, 3, and 4, making the correct answer 0 to 4.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What does the continue keyword do in a loop?

Stops the loop completely

Skips the current iteration and continues to the next

Repeats the current iteration

Breaks the loop

Answer explanation

The continue keyword in a loop skips the current iteration when encountered and proceeds to the next iteration. This allows the loop to continue running without executing the remaining code in the current iteration.

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What will this code output? python Copy code for i in range(4): if i == 2: continue print(i)

0 1 2 3

0 1 3

1 3

None

Answer explanation

The loop iterates from 0 to 3. When i is 2, the 'continue' statement skips the print. Thus, the output is 0, 1, and 3, making the correct answer '0 1 3'.

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?