Mastering Python Loops

Mastering Python Loops

Assessment

Quiz

Other

University

Practice Problem

Hard

Created by

Shreyas R

FREE Resource

Student preview

quiz-placeholder

16 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

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

3.5

5

-1

0 1 2 3 4

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following correctly defines a for loop in Python?

for each item in collection:

for index in range(start, end):

while variable in iterable:

for variable in iterable:

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What will be the output of the following code: sum = 0; for i in range(1, 5): sum += i; print(sum)?

10

20

15

5

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

In a for loop, what does the 'range()' function do?

The 'range()' function defines the maximum limit of a loop.

The 'range()' function creates a list of strings for iteration.

The 'range()' function creates a sequence of numbers for iteration in a for loop.

The 'range()' function generates random numbers for loops.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the primary difference between a for loop and a while loop?

A for loop runs indefinitely, while a while loop has a fixed number of iterations.

The primary difference is that a for loop iterates a known number of times, while a while loop continues until a condition is false.

A for loop is used for conditional checks, while a while loop is for counting iterations.

A for loop requires a condition, while a while loop does not need any conditions.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following statements will terminate a while loop?

Changing the loop condition to false or using a break statement.

Adding a return statement within the loop body.

Changing the loop condition to true or using a continue statement.

Incrementing the loop counter without changing the condition.

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What will be the output of the following code: count = 0; while count < 3: print(count); count += 1?

-1

4

0 1 2

3

Create a free account and access millions of resources

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?