דילוג לתוכן
מבחן 10 שאלות

Loops in Python

שאלות 10

מפתח התשובות מוצג למורים מחוברים.

  1. רב־ברירהשפות תכנותfor looprange functionoutput tracing

    Output of a simple for loop with range

    What is printed by the following Python code? for i in range(3): print(i)

    1. 1 2

    2. 0 1 2 3

    3. 0 1 2

    4. 1 2 3


    range(3) produces a sequence that starts at 0 and stops before 3. Think about which values i takes on.
  2. רב־ברירהשפות תכנותwhile looptracing codedecrement

    Tracing a while loop

    What is printed by the following Python code? x = 5; while x > 0: x = x - 2; print(x)

    1. 5 3 1

    2. 5 3 1 -1

    3. 3 1

    4. 3 1 -1


    Trace the value of x step by step. Check the condition x > 0 before each pass, and remember that the print comes after the subtraction.
  3. רב־ברירהשפות תכנותinfinite loopwhile looploop conditions

    Identifying an infinite loop

    Which of the following loops never ends?

    1. while x < 10: print(x) (with x = 0 before the loop, and x is never changed inside the loop)

    2. while x < 10: x = x + 1 (with x = 0 before the loop)

    3. for i in range(-5): print(i)

    4. for i in range(10): x = x + 1


    A while loop keeps running as long as its condition stays True. Ask yourself whether anything inside the loop could make the condition False.
  4. רב־ברירהשפות תכנותfor loopcontrol flowcontinue statement

    Effect of continue in a for loop

    What is printed by the following Python code? for n in range(1, 6): if n == 3: continue; print(n)

    1. 1 2

    2. 4 5

    3. 1 2 4 5

    4. 1 2 3 4 5


    The continue statement skips the rest of the current iteration and moves on to the next value. Remember that range(1, 6) does not include 6.
  5. רב־ברירהשפות תכנותrange functionlist conversionstep

    Values produced by range with a step

    What does list(range(2, 10, 3)) evaluate to in Python?

    1. [2, 5, 8]

    2. [2, 5, 8, 10]

    3. [2, 3, 4, 5, 6, 7, 8, 9]

    4. [3, 6, 9]


    The third argument of range is the step. Start at the first argument and keep adding the step, stopping before the second argument.
  6. רב־ברירהשפות תכנותaccumulatorfor loopvariables

    Accumulating a sum in a for loop

    After the following code runs, what is the value of total? total = 0; for x in [4, 7, 1]: total += x

    1. 7

    2. 12

    3. 14

    4. 13


    The += operator adds the current item to total on each pass. Keep a running total as you go through the list.
  7. רב־ברירהשפות תכנותwhile loopcontrol flowloop else

    The else clause of loops

    In Python, when does the else block attached to a while loop run?

    1. Only when the condition is True at the very start

    2. Only when a break statement is executed inside the loop

    3. Every time after each iteration of the loop

    4. When the loop ends because its condition became False, and not because of a break


    Compare what happens when a loop finishes normally with what happens when it is exited early by break.
  8. רב־ברירהשפות תכנותnested loopsloop countingfor loop

    Counting executions of nested loops

    How many times is print executed by the following code? for i in range(3): for j in range(2): print(i)

    1. 5

    2. 9

    3. 6

    4. 8


    The outer loop runs a fixed number of times, and each of its passes runs the inner loop completely. Multiply the two counts.
  9. רב־ברירהשפות תכנותrange functionlist conversionnegative step

    Counting down with range

    What does list(range(5, 0, -1)) evaluate to in Python?

    1. []

    2. [5, 4, 3, 2, 1, 0]

    3. [4, 3, 2, 1]

    4. [5, 4, 3, 2, 1]


    A negative step counts downward. Remember that the stop value itself is never included in the result.
  10. רב־ברירהשפות תכנותfor loopwhile loopiterationloop selection

    Choosing the right loop type

    A programmer needs to process every item of a list exactly once, from the first item to the last. Which Python loop is the most natural choice for this task?

    1. for loop

    2. goto loop

    3. while loop

    4. do-while loop


    Think about which loop is designed to iterate over the items of a sequence, and which one is built around a condition instead.

פרטים

Questions on for and while loops, ranges, loop counters and common errors in Python code.
מקצועות
שפות תכנות
קהל יעד
תיכון · כיתות י׳–י״ב
רמת קושי
6 מתוך 10
שפה
אנגלית
מי יכול לראות
כולם
הושלם על ידי AI
מה שמסומן בניצוץ נוסף על ידי AI.
נוצר באמצעות AI
Claude Haiku 5.5
נוצר

תגובות

  1. אין תגובות עדיין.
© כל הזכויות שמורות