Search Header Logo

Iteration in Python Quiz

Authored by Andrew Asante

Computers

12th Grade

Used 5+ times

Iteration in Python Quiz
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

9 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

1. Write a Python program to display all the prime numbers within a range using a for loop.

for num in range(lower, upper + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)

for num in range(1, upper + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)

for num in range(upper, lower - 1, -1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)

for num in range(lower, upper + 1): if num > 1: for i in range(2, num + 1): if (num % i) == 0: break else: print(num)

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

2. How does a for loop work in Python? Provide an example.

A for loop in Python can only iterate over a list

The correct answer for the question is: A for loop in Python iterates over a sequence or iterable object, executing a block of code for each item in the sequence.

A for loop in Python only works with integers

A for loop in Python executes the block of code only once

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

3. Create a Python program to print the Fibonacci sequence using a while loop.

Print the prime numbers using a do-while loop

Print the Fibonacci sequence using a for loop

Print the factorial sequence using a while loop

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

5. Write a Python program to display the multiplication table using nested loops.

print('Multiplication table')

for i in range(1, 11):

for j in range(1, 11):

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

6. What are nested loops in Python? Provide an example.

Nested loops are loops that only run once

Nested loops are loops that cannot be used in Python

Nested loops are loops that run in parallel

Nested loops in Python are loops within a loop. They are used to iterate over multiple sequences or perform repetitive tasks. For example: for i in range(3): for j in range(2): print(i, j)

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

7. Write a Python program to iterate through a list and display the elements.

print(my_list)

while my_list: print(my_list.pop())

for i in range(len(my_list)): print(my_list[i])

for item in my_list: print(item)

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

8. Explain how to iterate through a list in Python with an example.

for i in range(len(my_list)): print(my_list[i])

while item in my_list: print(item)

if item in my_list: print(item)

for item in my_list: print(item)

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?