7. For Loops with Range Quiz

7.1. Question 1

By default, what integer value does the sequence generated by the range() function start at if a specific starting point is not provided?

Correct. The documentation specifies that the range() function returns a sequence of numbers starting from 0 by default.

Incorrect. In Python, ranges and indices start counting at 0 by default rather than 1.

Incorrect. Negative starting integers must be explicitly defined in multi-argument ranges.

Incorrect. The function generates a strict deterministic sequence.


7.2. Question 2

By default, what value does each step increment by inside the sequence generated by a standard range() statement?

Incorrect. The range function operates exclusively with integer step values.

Correct. The range() function increments by 1 by default.

Incorrect. To step by 2, an explicit third step parameter must be supplied.

Incorrect. It produces a linear arithmetic sequence, not a geometric progression.


7.3. Question 3

What exact integer sequence is generated by evaluating the statement range(3)?

Incorrect. It starts at 0 by default and stops before reaching the stop value.

Incorrect. The sequence stops right before the specified boundary number.

Correct. It starts at 0, goes up by 1, and stops before 3, at 2.

Incorrect. Without multiplication or custom parameters, it increments sequentially by 1.


7.4. Question 4

If you run the code below, what is the final number scrolled across the micro:bit LED display panel?

from microbit import *

while True:
    for n in range(3):
        display.scroll(n, delay=80)
    sleep(500)

Incorrect. The range(3) statement halts execution before reaching 3.

Correct. The sequence contains 0, 1, and 2, making 2 the final value displayed in the block.

Incorrect. The sequence continues past 1 to include the index 2.

Incorrect. 0 is the starting number scrolled, not the final one.


7.5. Question 5

A student wants to loop through code a specified number of times. According to the reference guide, which Python function is designed for this task?

Incorrect. This function animates data values on the screen but does not manage loops.

Incorrect. This function pauses execution timing instead of generating loop counts.

Correct. To loop through a set of code a specified number of times, use the range() function.

Incorrect. There is no built-in function named loop() for sequence generation in Python.


7.6. Question 6

Look at the following mathematical loop block configuration:

for n in range(3):
    display.scroll(n * 2, delay=80)

What values will be output sequentially to the screen?

Incorrect. This would be the result if n was scrolled directly without multiplying by 2.

Correct. The range numbers (0, 1, 2) are each multiplied by 2, resulting in 0, 2, and 4.

Incorrect. The first number generated is 0, and 0 multiplied by 2 equals 0.

Incorrect. This multiplies the sequence values by an incorrect factor.


7.7. Question 7

Which of the following range code fragments correctly outputs the precise numeric list 0, 3, 6, 9?

Correct. range(4) generates 0, 1, 2, 3. Multiplying each by 3 gives 0, 3, 6, 9.

Incorrect. This loop stops early, only outputting 0, 3, and 6.

Incorrect. This adds 3 instead of scaling, outputting 3, 4, 5, 6.

Incorrect. This outputs all consecutive integers from 0 up to 8.


7.8. Question 8

Which code pattern produces a negative decreasing sequence displaying 0, -1, -2, -3 sequentially?

Incorrect. This creates a positive sequence containing 0, 1, 2, 3.

Correct. range(4) creates 0, 1, 2, 3. Multiplying by -1 converts them into 0, -1, -2, -3.

Incorrect. Passing a single negative integer to range() returns an empty sequence.

Incorrect. This loop terminates early, displaying only 0, -1, and -2.


7.9. Question 9

What happens if you program an infinite while True: statement directly around a for loop block?

Incorrect. Nesting loops is a valid control structure.

Correct. The outer while loop forces the entire internal sequential for loop to repeat continuously.

Incorrect. The inner display commands will execute normally on every cycle.

Incorrect. The stop parameters inside range() stay fixed unless updated via code logic.


7.10. Question 10

A student wants to use the range function to display the numbers from 0 up to but not including 5. Which range loop statement is correct?

Incorrect. This stops before 4, only generating numbers up to 3.

Correct. range(5) yields numbers starting at 0 and stops before the stop value of 5, which matches 0, 1, 2, 3, 4.

Incorrect. This generates numbers up to 5, which violates the requirement.

Incorrect. This evaluates to an empty sequence and won’t execute the loop block.