NEW
Font size
WorksheetsUnderstanding Loops in Programming
Total questions: 54
Worksheet time: 54mins
Naira is learning programming and wants to understand how to use a for loop. What is the basic syntax of a for loop?
for(initialization; condition; increment) { // code }
for(condition; initialization; increment) { // code }
for(initialization; increment; condition) { // code }
for(initialization: condition: increment) { // code }
Aanya is trying to understand the difference between a while loop and a do-while loop in her programming class. Can you explain it to her?
A while loop checks the condition before execution; a do-while loop executes at least once before checking the condition.
A while loop executes at least once; a do-while loop may not execute at all.
A while loop is used for infinite loops; a do-while loop is for finite loops.
Both loops check the condition after execution.
Akhil is learning programming and wants to understand how loops work. What is the purpose of a loop control statement?
To terminate the program immediately.
To increase the number of iterations in a loop.
To define the variables used in the loop.
To control the execution flow of loops.
Tara is learning how to use loops in programming. She wants to know how to exit a loop when a certain condition is met. Provide an example of a break statement in a loop.
for (int i = 0; i < 10; i++) { if (i == 5) break; }
for (int i = 0; i < 10; i++) { if (i == 5) exit(0); }
for (int i = 0; i < 10; i++) { if (i == 5) return; }
while (i < 10) { if (i == 5) continue; }
Aarav is trying to teach his friend Akhil about programming concepts. He asks, 'What is an infinite loop? Give an example.'
do { // do something } while(false);
for(;;) { // do something }
An example of an infinite loop is: 'while(true) { // do something }'.
for(int i = 0; i < 10; i++) { // do something }
How does a for loop differ from a while loop in terms of initialization when Divya is programming her project?
A for loop does not require initialization; a while loop does.
A for loop initializes within its declaration; a while loop initializes separately before the loop.
Both for and while loops initialize in the same way, outside the loop.
A for loop initializes after the loop starts; a while loop initializes within its declaration.
What will happen if Neha sets a condition in a while loop that is always true?
The program will skip the loop entirely.
The program will terminate immediately.
The loop will run a fixed number of times.
The program will enter an infinite loop.
Aarav wants to print the numbers from 1 to 3 using a do-while loop. How should he write it?
do { i++; } while (i < 3);
while (i <= 3) { console.log(i); i++; }
for (i = 1; i <= 3; i++) { console.log(i); }
do { console.log(i); i++; } while (i <= 3);
Naira is learning Java and writes the following code: for(int i=0; i<5; i++) { System.out.println(i); }. What will be the output of this code?
10
5
-1
0 1 2 3 4
Can Vanya use a for loop to create an infinite loop while coding? If yes, how?
Yes, a for loop can be used to create an infinite loop.
You can only create an infinite loop with a while loop.
No, a for loop cannot create an infinite loop.
A for loop can only run a fixed number of times.
In a programming class, Saisha is learning about loops. What is the main advantage of using a do-while loop?
It executes the loop body only if the condition is true.
It allows for infinite loops without any condition.
The main advantage of using a do-while loop is that it ensures the loop body executes at least once.
It is more efficient than a for loop in all cases.
Riyaan is working on a project where he needs to search through a list of items. Describe a scenario where a break statement would be useful in his loop.
A break statement is used to pause the loop for a specified duration.
A break statement is useful in a loop when Riyaan wants to exit the loop early upon meeting a certain condition, such as finding a specific item.
A break statement is useful for skipping the current iteration of the loop.
A break statement can be used to increase the loop counter by a fixed amount.
Aashi is learning programming and wants to know the syntax for a while loop. Can you help her?
while (condition) { // code to be executed }
do { // code to be executed } while (condition)
for (condition) { // code to be executed }
repeat (condition) { // code to be executed }
Vanya is writing a program to calculate the sum of numbers, but she is worried about creating an infinite loop. How can she prevent an infinite loop in her code?
Ensure a clear exit condition and modify loop variables appropriately.
Use a random number generator to control the loop.
Increase the loop variable by a constant value each iteration.
Always set the loop variable to zero before starting the loop.
Riya is trying to understand the difference between a for loop and a while loop. Can you explain it to her?
A for loop is typically used when the number of iterations is known; a while loop is used when the number of iterations is not known.
A for loop can only iterate over arrays; a while loop can iterate over any condition.
A for loop executes at least once; a while loop may not execute at all.
Both loops are identical in functionality.
Meera is curious about loop control statements. What is the purpose of a continue statement in a loop?
To terminate the loop immediately.
To skip the current iteration and continue with the next iteration of the loop.
To exit the loop and return to the main program.
To pause the loop for a specified duration.
Vikram is trying to understand the concept of loop iteration. What does it mean to iterate over a loop?
To execute the loop body multiple times based on a condition.
To exit the loop immediately.
To define the loop's exit condition.
To initialize loop variables.
Priya is curious about the differences between a for loop and a while loop. Which statement is true regarding their usage?
A for loop is generally used when the number of iterations is known beforehand.
A while loop is always more efficient than a for loop.
Both loops can only be used for counting iterations.
A for loop cannot be used for infinite iterations.
What will the following program segment display?
for(int a = 2; a <= 10; a += 3)
System.out.print(a+””);
2 5 8
2 5
2 5 8 11
2
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
Repeating a task multiple times in programming is known as ?
Iterators
Scope
Looping
Which loop loops a set number of times?
for
while
infinite
loopy
Which of these is the correct syntax for a loop?
for(int i = 0; i<10; 1++){}
fore(integer i = 0; i>10; i++){}
for(int i = 0; i<10){}
for(int i){}
Which of the following is the correct way to create a String?
String s = "I am cool";
string string = i am cool;
String s = I am cool
String s = "I" am cool;
Which of the following is a type of loop in Java?
loop
while
iteration
i
What does the following loop print?
for(int i = 1; i<3; i++)
{ System.out.print(i); }
12
012
0123
123
What will the following loop print?
int i = 0;
while (i>0)
{ System.out.println(i); }
Infinite loop
012345678910
0
Nothing
What will the following loop print?
int i = 1;
while (i>0)
{ System.out.println(i); }
012345678910
nothing
infinite loop
0
What does the following loop print?
for(i = i +1; i<5; int i = 0;)
{ System.out.println(i) }
01234
12345
error
0
Which of the following includes the correct syntax for a for loop?
for(int i = 0, i<10, i++)
for(int i =0, run = 10)
for(int i = 0; run = 10)
for(int i = 0; i < 10; i++)
for(int i; i<10; i++) {} Why won’t this code segment run?
There is a missing semicolon at the end.
For should be capitalized
The person did not set the beginning value of i.
The person did not set the ending value of i.
What does this loop do?
There is an error; it won’t run.
It prints all the odd numbers from 0 - 20 inclusive on both ends.
It prints all the even numbers from 0 - 20 inclusive on both ends.
It prints all the even numbers from 0 - 20 inclusive with 0 and exclusive with 20.
What loop should you use if you know how many times you are going to loop?
For loop
Do while loop
While loop
No loop
This loop can only be used when the programmer knows the number of times a code is to be repeated
while
for
do while
repeat until
Which of the following control structure are needed to repeat a code?
sequence
loop
selection
if
FOR loops are
loops which run an unknown number of times
loops which run for a specific number of times
the same as if statements
not part of programming
Which kind of loop would be used?
I need a program that will keep letting me add a monthly payment for 12 months.
FOR Loop
WHILE Loop
Which of these operators means EQUAL TO?
'='
'=>'
'<='
'=='
In a While loop a code will be repeated until the condition becomes
FALSE
TRUE
User choice
Which is the correct format of a do-while loop?
do-while(condition);
{
//code
}
do{
//code
}while(condition);
do-while(condition)
{
//code
}
do{
//code
}while(condition)
14) What is the output of the below Java program?
int a=1;
while(a<4)
{
System.out.print(a + " ");
a++;
}
1 2 3 4
1 2 3
6
Compiler error
What is the output of the below Java program with a decrement operator and WHILE-loop?
int a=4;
while(a>0)
{
System.out.print(a + " ");
a--;
}
4 3 2 1
3 2 1
Compiler error
None
What is the main difference between a WHILE and a DO-WHILE loop in Java?
WHILE loop executes the statements inside of it at least once even if the condition is false.
DO-WHILE loop executes the statements inside of it at least once even if the condition is false.
WHILE loop is fast.
DO-WHILE loop is fast.
What is the value of "age" in the below Java program with a DO-WHILE loop?
int age=20;
do
{
age++;
}while(age<20);
System.out.println(age);
20
21
Compiler error
None
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println(x);
Which of these is the correct syntax for a loop?
for(int i = 0; i<10; 1++){}
fore(integer i = 0; i>10; i++){}
for(int i = 0; i<10){}
for(int i){}
What does the following loop print?
for(int i = 1; i<3; i++)
{ System.out.print(i); }
12
012
0123
123
What will the following loop print?
int i = 0;
while (i>0)
{ System.out.println(i); }
Infinite loop
012345678910
0
Nothing
What will the following loop print?
int i = 1;
while (i>0)
{ System.out.println(i); }
012345678910
nothing
infinite loop
0
for(int i; i<10; i++) {} Why won’t this code segment run?
There is a missing semicolon at the end.
For should be capitalized
The person did not set the beginning value of i.
The person did not set the ending value of i.
What does this loop do?
There is an error; it won’t run.
It prints all the odd numbers from 0 - 20 inclusive on both ends.
It prints all the even numbers from 0 - 20 inclusive on both ends.
It prints all the even numbers from 0 - 20 inclusive with 0 and exclusive with 20.
