WorksheetsLoops in Dart Quiz
Total questions: 20
Worksheet time: 10mins
Which loop is used when the number of iterations is known in advance?
While Loop
Do-While Loop
For Loop
For-In Loop
Which loop ensures that the code inside it is executed at least once before checking the condition?
For Loop
While Loop
Do-While Loop
ForEach Loop
What will this code print? var list = [1, 2, 3]; for (var i in list) { print(i); }
1, 2, 3
1
Error
Infinite loop
Which loop type is best suited to iterate over each item in an iterable like a List or Set?
While Loop
Do-While Loop
For-In Loop
For Loop
What does the following code do? var i = 0; while (i < 5) { print(i); i++; }
Prints 0 through 5
Prints 0 through 4
Prints an error
Infinite loop
Which loop would you use when you want to iterate over a list with a callback function for each element?
For-In Loop
While Loop
Do-While Loop
ForEach (on Iterable)
Which of the following is true about a Do-While loop?
The loop condition is checked before the first iteration.
The loop runs infinitely.
The loop executes at least once regardless of the condition.
It is not possible to break the loop.
What will the following code print? var i = 0; do { print(i); i++; } while (i < 3);
0, 1, 2
1, 2, 3
Infinite loop
Error
What is the primary difference between a for loop and a while loop in Dart?
A for loop executes an unknown number of times, while a while loop executes a known number of times.
A for loop is used for iterating over collections, while a while loop is used for fixed iterations.
A for loop checks the condition before each iteration, while a while loop does so after.
There is no difference; they are functionally identical.
Which of the following is NOT a loop type in Dart?
For Loop
For-In Loop
Do-While Loop
Repeat-Until Loop
Which loop type is used when you don't know the number of iterations in advance, but you know the condition that needs to be true to stop the loop?
For Loop
While Loop
Do-While Loop
ForEach Loop
What will the following code print? var i = 5; do { print(i); i--; } while (i > 0);
5, 4, 3, 2, 1
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1
Infinite loop
What is the output of the following code? var list = ['apple', 'banana', 'cherry']; list.forEach((fruit) => print(fruit));
apple, banana, cherry
apple
Error
banana, cherry
In the context of a for loop, what does the expression i++ do?
Decreases the value of i by 1
Increases the value of i by 1
Resets the value of i to 0
Multiplies i by 2
What happens if the condition in a while loop is initially false?
The loop will run infinitely.
The loop will never execute.
The loop will execute once.
The loop will execute twice.
Which loop type can be used to iterate through a map in Dart?
For Loop
For-In Loop
ForEach Loop
Do-While Loop
What is the correct syntax to create an infinite while loop in Dart?
while (true) {}
while (1) {}
while (true) {}
for (;;)
What will this for loop do? for (var i = 0; i < 3; i++) { print(i); }
Print 3 times
Print 0, 1, 2
Print 0, 1
Print 1, 2, 3
How do you break out of a while loop early in Dart?
stop;
break;
exit;
end;
Which of the following is NOT true about a forEach loop in Dart?
It can be used to iterate over elements in a List.
It takes a callback function as a parameter.
It can be used to break out of the loop.
It is mostly used for working with collections.
