Quiz: If-Statements and Loops in C#
1 / 10
a) To create a variable.
b) To repeat code a certain number of times.
c) To execute a block of code only if a condition is true.
d) To define a function.
2 / 10
2. What will the following code print?
int x = 5; if (x > 3) { Console.WriteLine("x is greater than 3"); } else { Console.WriteLine("x is 3 or less"); }
a) x is 3 or less
b) x is greater than 3
c) Nothing
3 / 10
3. What is the difference between an if and else if statement?
if
else if
a) else if is only used when there is more than one condition.
b) else if is only used for boolean conditions.
c) else if is used to check another condition if the first if statement is false.
4 / 10
5. What is the output of this while loop?
while
int i = 1; while (i <= 3) { Console.WriteLine("Loop iteration: " + i); i++; }
a) Loop iteration: 1, Loop iteration: 2, Loop iteration: 3
b) Loop iteration: 1, Loop iteration: 2, Loop iteration: 3, Loop iteration: 4
c) Loop iteration: 1
5 / 10
4. How many times will the following for loop run?
for
for (int i = 0; i < 4; i++) { Console.WriteLine(i); }
a) 3 times
b) 4 times
c) 5 times
d) 1 time
6 / 10
6. Which type of loop guarantees that the code inside the loop will run at least once?
a) for loop
b) while loop
c) do-while loop
d) All of the above
7 / 10
7. What does the following code do?
int i = 0; do { Console.WriteLine(i); i++; } while (i < 3);
a) It prints the numbers 1, 2, and 3.
b) It prints the numbers 0, 1, and 2.
c) It prints nothing.
d) It causes an error.
8 / 10
8. How would you make the following loop run exactly 10 times?
for (int i = 0; ___; i++) { Console.WriteLine(i); }
a) i < 9
b) i <= 10
c) i < 10
d) i <= 9
9 / 10
9. What happens if you forget to increase the counter inside a while loop (e.g., i++)?
i++
a) The loop will only run once.
b) The loop will run an infinite number of times.
c) The program will crash immediately.
d) The loop will skip to the next iteration.
10 / 10
break
a) It repeats the loop.
b) It skips the current iteration and continues with the next one.
c) It terminates the loop completely, even if the loop condition is still true.
d) It changes the loop variable.
Your score is
The average score is 0%
Restart quiz