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

Loops and Conditions in C#

שאלות 10

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

  1. שאלה פתוחהc#continue statementfor looploop controlmodulo operatorvariable tracing

    Tracing a for loop with continue

    What is the final value of sum after this C# code runs? int sum = 0; for (int i = 1; i <= 5; i++) { if (i % 2 == 0) continue; sum += i; }


    The continue statement skips the rest of the current iteration. Work out which values of i reach sum += i, then add them up.
  2. שאלה פתוחהc#for looploop iterationsdecrement operatorC syntaxcode tracing

    Counting loop iterations

    How many times does the body of this loop execute? for (int i = 10; i > 0; i -= 3) { Console.WriteLine(i); }


    List the value of i at the start of each pass through the loop, stopping as soon as the condition i > 0 is false.
  3. שאלה פתוחהc#while looploop conditiondo-while loop

    while versus do-while

    Explain the key difference between a while loop and a do-while loop in C#, including when the condition is checked.


    Think about where the condition appears in each loop's structure, relative to the loop body.
  4. שאלה פתוחהc#logical operatorsdivision by zeroshort-circuit evaluation

    Short-circuit evaluation

    In C#, the expression x != 0 && 10 / x > 2 is safe even when x is 0. Explain why no division-by-zero error occurs, and name the operator behaviour responsible.


    Consider what the && operator needs to know before it evaluates its right-hand side.
  5. שאלה פתוחהc#conditional statementsif-else ifswitch statement

    Choosing between if-else if and switch

    Describe a situation where an if-else if chain is more suitable than a switch statement in C#, and explain why. Give one reason based on the type of condition being tested.


    Consider whether the tests compare one value for equality or check ranges and combined conditions.
  6. שאלה פתוחהc#while loopuser inputinput validation

    Writing a loop with a condition

    Write a short C# while loop that repeatedly reads an integer from the user with Console.ReadLine and Convert.ToInt32, and keeps asking until the number entered is between 1 and 100 inclusive. Describe the key parts: the loop condition and how the value is updated.


    The loop should keep running while the input is invalid, so write the condition as the negation of the valid range, and make sure the variable is updated inside the body.
  7. השלמהc#break statementloop controlflow control

    Breaking out of a loop

    The _______ keyword immediately exits the innermost loop in C#.

    throwbreakreturngoto

    Think of the keyword that stops a loop entirely rather than skipping only the current pass.
  8. השלמהc#iterationloop structureloop condition

    Parts of a for loop

    A C# for loop has three parts: an initializer, a ________________, and an iterator. Each pass through the loop body is called an ________________.

    exit labeldeclarationloop conditioniterationterminationloop bodyreturn value

    The middle part of the for header decides whether another pass happens, and each pass is named after what it does.
  9. השלמהc#conditional statementsbool typeequality operator

    Conditions in if statements

    An if statement in C# runs its block only when its condition evaluates to __________. A condition must produce a value of type __________, so writing an int in an if test causes a compile-time error. To compare two values, a programmer uses the __________ operator rather than the assignment operator.

    intstringfalseequalityzeronulltruebool

    Consider the Boolean value that lets the if block run, and the type that conditions must have in C#.
  10. השלמהc#iteration variableread-onlyIEnumerable

    Using foreach loops

    A foreach loop in C# visits each element of a collection in turn, so it needs no counter variable. It works with any type that implements the _____________ interface. The iteration variable is _____________, so you cannot assign a new value to it inside the loop, or the compiler reports an error. Use a _____________ loop instead when you need to change elements by index.

    IEquatableprivatestaticforIDisposableIComparableread-onlyIEnumerable

    The interface name starts with I and describes something that can be looped over; the iteration variable cannot be changed, and an index-based loop keyword is needed.

פרטים

A quiz on if statements, for and while loops, and simple C# programs that use them.
מקצועות
c#
קהל יעד
תיכון · כיתות י׳–י״ב
רמת קושי
7 מתוך 10
שפה
אנגלית
מי יכול לראות
כולם
הושלם על ידי AI
מה שמסומן בניצוץ נוסף על ידי AI.
נוצר באמצעות AI
Claude Haiku 5.5
נוצר

תגובות

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