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

Loops in Programming

שאלות 10

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

  1. רב־ברירהשפות תכנות

    Output of a simple for loop

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

    1. 0 1 2 3

    2. 1 2 3

    3. 0 1 2

    4. 1 2


    Recall that range(n) produces n values, starting from 0 and stopping before n.
  2. רב־ברירהשפות תכנות

    Counting iterations of a while loop

    How many times does the body of the following loop execute?
    x = 5
    while x > 0:
    x = x - 2

    1. 4

    2. 5

    3. 3

    4. 2


    Trace the value of x after each pass and check the condition before every pass, including the one that fails.
  3. רב־ברירהשפות תכנות

    Identifying an infinite loop

    Which of the following loops never terminates?

    1. for i in range(10): print(i)

    2. for i in range(10, 0): print(i)

    3. i = 0; while i < 10: print(i)

    4. i = 10; while i > 0: i = i - 1


    Ask whether the condition of the while loop can ever become false, meaning whether the variable inside the condition changes.
  4. רב־ברירהשפות תכנות

    Counting iterations of a C-style for loop

    How many times does the body of the following C-style loop execute?
    for (int i = 10; i > 0; i = i - 3) { count++; }

    1. 5

    2. 3

    3. 10

    4. 4


    List the values of i in order: start at 10 and subtract 3 each time, stopping when i is no longer greater than 0.
  5. רב־ברירהשפות תכנות

    When a while condition is checked

    In a standard while loop, when is the loop condition evaluated?

    1. After each iteration, so the body always runs at least once

    2. Only once, after the loop body has finished running

    3. Before each iteration, so the body may run zero times

    4. Only when the loop body contains a break statement


    Think about what happens when a while loop's condition is False the very first time it is reached.
  6. רב־ברירהשפות תכנות

    Accumulating a sum with a for loop

    What value is stored in total after the following code runs?
    total = 0
    for n in range(1, 5):
    total = total + n

    1. 12

    2. 15

    3. 8

    4. 10


    Remember that range(1, 5) stops before 5, so the values of n are 1, 2, 3 and 4.
  7. רב־ברירהשפות תכנות

    Loops that run at least once

    Which type of loop guarantees that its body is executed at least once, because its condition is checked after the body?

    1. A post-test loop such as do-while

    2. A while loop whose condition is initially False

    3. A for loop over range

    4. Python's while loop


    Consider which loop structure places its condition check at the end of the loop instead of at the beginning.
  8. רב־ברירהשפות תכנות

    Nested loop iteration count

    How many times is count increased by the following nested loops?
    count = 0
    for i in range(3):
    for j in range(2):
    count = count + 1

    1. 9

    2. 5

    3. 3

    4. 6


    The inner loop runs completely for each value of the outer loop, so multiply the two loop lengths.
  9. רב־ברירהשפות תכנות

    Effect of continue in a for loop

    Which values are printed by the following code?
    for i in range(1, 6):
    if i == 3:
    continue
    print(i)

    1. 1 2 4 5

    2. 1 2 3 4 5

    3. 2 4

    4. 1 2 3 5


    The continue statement skips the rest of the current iteration and moves on to the next value of i.
  10. רב־ברירהשפות תכנות

    Preventing an infinite while loop

    The following code is meant to print the numbers 1 to 5, but it currently never ends. Which line should be added inside the loop body to fix it?
    n = 1
    while n <= 5:
    print(n)

    1. n == n + 1

    2. n = 5

    3. n = n + 1

    4. print(n + 1)


    The loop condition depends on n, so the body must change n so that the condition can eventually become False.

פרטים

A quiz on for-loops and while-loops: how they work, how to trace their output, and how to avoid infinite loops.
מקצועות
שפות תכנות
קהל יעד
תיכון · כיתות י׳–י״ב
רמת קושי
6 מתוך 10
שפה
אנגלית
מי יכול לראות
כולם
נוצר באמצעות AI
Claude Haiku 5.5
נוצר

תגובות

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