wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Understanding Loops in Programming

Total questions: 54

Worksheet time: 54mins

Name
Class
Date
1.

Naira is learning programming and wants to understand how to use a for loop. What is the basic syntax of a for loop?

a)

for(initialization; condition; increment) { // code }

b)

for(condition; initialization; increment) { // code }

c)

for(initialization; increment; condition) { // code }

d)

for(initialization: condition: increment) { // code }

2.

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)

A while loop checks the condition before execution; a do-while loop executes at least once before checking the condition.

b)

A while loop executes at least once; a do-while loop may not execute at all.

c)

A while loop is used for infinite loops; a do-while loop is for finite loops.

d)

Both loops check the condition after execution.

3.

Akhil is learning programming and wants to understand how loops work. What is the purpose of a loop control statement?

a)

To terminate the program immediately.

b)

To increase the number of iterations in a loop.

c)

To define the variables used in the loop.

d)

To control the execution flow of loops.

4.

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.

a)

for (int i = 0; i < 10; i++) { if (i == 5) break; }

b)

for (int i = 0; i < 10; i++) { if (i == 5) exit(0); }

c)

for (int i = 0; i < 10; i++) { if (i == 5) return; }

d)

while (i < 10) { if (i == 5) continue; }

5.

Aarav is trying to teach his friend Akhil about programming concepts. He asks, 'What is an infinite loop? Give an example.'

a)

do { // do something } while(false);

b)

for(;;) { // do something }

c)

An example of an infinite loop is: 'while(true) { // do something }'.

d)

for(int i = 0; i < 10; i++) { // do something }

6.

How does a for loop differ from a while loop in terms of initialization when Divya is programming her project?

a)

A for loop does not require initialization; a while loop does.

b)

A for loop initializes within its declaration; a while loop initializes separately before the loop.

c)

Both for and while loops initialize in the same way, outside the loop.

d)

A for loop initializes after the loop starts; a while loop initializes within its declaration.

7.

What will happen if Neha sets a condition in a while loop that is always true?

a)

The program will skip the loop entirely.

b)

The program will terminate immediately.

c)

The loop will run a fixed number of times.

d)

The program will enter an infinite loop.

8.

Aarav wants to print the numbers from 1 to 3 using a do-while loop. How should he write it?

a)

do { i++; } while (i < 3);

b)

while (i <= 3) { console.log(i); i++; }

c)

for (i = 1; i <= 3; i++) { console.log(i); }

d)

do { console.log(i); i++; } while (i <= 3);

9.

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?

a)

10

b)

5

c)

-1

d)

0 1 2 3 4

10.

Can Vanya use a for loop to create an infinite loop while coding? If yes, how?

a)

Yes, a for loop can be used to create an infinite loop.

b)

You can only create an infinite loop with a while loop.

c)

No, a for loop cannot create an infinite loop.

d)

A for loop can only run a fixed number of times.

11.

In a programming class, Saisha is learning about loops. What is the main advantage of using a do-while loop?

a)

It executes the loop body only if the condition is true.

b)

It allows for infinite loops without any condition.

c)

The main advantage of using a do-while loop is that it ensures the loop body executes at least once.

d)

It is more efficient than a for loop in all cases.

12.

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)

A break statement is used to pause the loop for a specified duration.

b)

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.

c)

A break statement is useful for skipping the current iteration of the loop.

d)

A break statement can be used to increase the loop counter by a fixed amount.

13.

Aashi is learning programming and wants to know the syntax for a while loop. Can you help her?

a)

while (condition) { // code to be executed }

b)

do { // code to be executed } while (condition)

c)

for (condition) { // code to be executed }

d)

repeat (condition) { // code to be executed }

14.

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?

a)

Ensure a clear exit condition and modify loop variables appropriately.

b)

Use a random number generator to control the loop.

c)

Increase the loop variable by a constant value each iteration.

d)

Always set the loop variable to zero before starting the loop.

15.

Riya is trying to understand the difference between a for loop and a while loop. Can you explain it to her?

a)

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.

b)

A for loop can only iterate over arrays; a while loop can iterate over any condition.

c)

A for loop executes at least once; a while loop may not execute at all.

d)

Both loops are identical in functionality.

16.

Meera is curious about loop control statements. What is the purpose of a continue statement in a loop?

a)

To terminate the loop immediately.

b)

To skip the current iteration and continue with the next iteration of the loop.

c)

To exit the loop and return to the main program.

d)

To pause the loop for a specified duration.

17.

Vikram is trying to understand the concept of loop iteration. What does it mean to iterate over a loop?

a)

To execute the loop body multiple times based on a condition.

b)

To exit the loop immediately.

c)

To define the loop's exit condition.

d)

To initialize loop variables.

18.

Priya is curious about the differences between a for loop and a while loop. Which statement is true regarding their usage?

a)

A for loop is generally used when the number of iterations is known beforehand.

b)

A while loop is always more efficient than a for loop.

c)

Both loops can only be used for counting iterations.

d)

A for loop cannot be used for infinite iterations.

19.

What will the following program segment display?


for(int a = 2; a <= 10; a += 3)

System.out.print(a+””);

a)

2 5 8

b)

2 5

c)

2 5 8 11

d)

2

20.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
 System.out.println("Welcome to Java");
 count++;
}
a)
8
b)
9
c)
10
d)
0
21.
What is the output of the following code?
int x = 0;
while (x < 4) {
 x = x + 1;
}
System.out.println("x is " + x);
a)
0
b)
1
c)
3
d)
4
22.

Repeating a task multiple times in programming is known as ?

a)

Iterators

b)

Scope

c)

Looping

23.

Which loop loops a set number of times?

a)

for

b)

while

c)

infinite

d)

loopy

24.

Which of these is the correct syntax for a loop?

a)

for(int i = 0; i<10; 1++){}

b)

fore(integer i = 0; i>10; i++){}

c)

for(int i = 0; i<10){}

d)

for(int i){}

25.

Which of the following is the correct way to create a String?

a)

String s = "I am cool";

b)

string string = i am cool;

c)

String s = I am cool

d)

String s = "I" am cool;

26.

Which of the following is a type of loop in Java?

a)

loop

b)

while

c)

iteration

d)

i

27.

What does the following loop print?

for(int i = 1; i<3; i++)

{ System.out.print(i); }

a)

12

b)

012

c)

0123

d)

123

28.

What will the following loop print?


int i = 0;

while (i>0)

{ System.out.println(i); }

a)

Infinite loop

b)

012345678910

c)

0

d)

Nothing

29.

What will the following loop print?

int i = 1;

while (i>0)

{ System.out.println(i); }

a)

012345678910

b)

nothing

c)

infinite loop

d)

0

30.

What does the following loop print?


for(i = i +1; i<5; int i = 0;)

{ System.out.println(i) }

a)

01234

b)

12345

c)

error

d)

0

31.

Which of the following includes the correct syntax for a for loop?

a)

for(int i = 0, i<10, i++)

b)

for(int i =0, run = 10)

c)

for(int i = 0; run = 10)

d)

for(int i = 0; i < 10; i++)

32.

for(int i; i<10; i++) {} Why won’t this code segment run?

a)

There is a missing semicolon at the end.

b)

For should be capitalized

c)

The person did not set the beginning value of i.

d)

The person did not set the ending value of i.

33.

What does this loop do?

a)

There is an error; it won’t run.

b)

It prints all the odd numbers from 0 - 20 inclusive on both ends.

c)

It prints all the even numbers from 0 - 20 inclusive on both ends.

d)

It prints all the even numbers from 0 - 20 inclusive with 0 and exclusive with 20.

34.

What loop should you use if you know how many times you are going to loop?

a)

For loop

b)

Do while loop

c)

While loop

d)

No loop

35.

This loop can only be used when the programmer knows the number of times a code is to be repeated

a)

while

b)

for

c)

do while

d)

repeat until

36.

Which of the following control structure are needed to repeat a code?

a)

sequence

b)

loop

c)

selection

d)

if

37.

FOR loops are

a)

loops which run an unknown number of times

b)

loops which run for a specific number of times

c)

the same as if statements

d)

not part of programming

38.

Which kind of loop would be used?


I need a program that will keep letting me add a monthly payment for 12 months.

a)

FOR Loop

b)

WHILE Loop

39.

Which of these operators means EQUAL TO?

a)

'='

b)

'=>'

c)

'<='

d)

'=='

40.

In a While loop a code will be repeated until the condition becomes

a)

FALSE

b)

TRUE

c)

User choice

41.
In programming, what is iteration?
a)
The repetition of steps within a program
b)
The order in which instructions are carried out
c)
A decision point in a program
d)
Testing a program to make sure it works
42.

Which is the correct format of a do-while loop?

a)

do-while(condition);

{

//code

}

b)

do{

//code

}while(condition);

c)

do-while(condition)

{

//code

}

d)

do{

//code

}while(condition)

43.

14) What is the output of the below Java program?

int a=1;

while(a<4)

{

System.out.print(a + " ");

a++;

}

a)

1 2 3 4

b)

1 2 3

c)

6

d)

Compiler error

44.

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--;

}

a)

4 3 2 1

b)

3 2 1

c)

Compiler error

d)

None

45.

What is the main difference between a WHILE and a DO-WHILE loop in Java?

a)

WHILE loop executes the statements inside of it at least once even if the condition is false.

b)

DO-WHILE loop executes the statements inside of it at least once even if the condition is false.

c)

WHILE loop is fast.

d)

DO-WHILE loop is fast.

46.

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);

a)

20

b)

21

c)

Compiler error

d)

None

47.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
 System.out.println("Welcome to Java");
 count++;
}
a)
8
b)
9
c)
10
d)
0
48.

What is the output of the following code? int x = 0; while (x < 4) {  x = x + 1; } System.out.println(x);

a)
0
b)
1
c)
3
d)
4
49.

Which of these is the correct syntax for a loop?

a)

for(int i = 0; i<10; 1++){}

b)

fore(integer i = 0; i>10; i++){}

c)

for(int i = 0; i<10){}

d)

for(int i){}

50.

What does the following loop print?

for(int i = 1; i<3; i++)

{ System.out.print(i); }

a)

12

b)

012

c)

0123

d)

123

51.

What will the following loop print?


int i = 0;

while (i>0)

{ System.out.println(i); }

a)

Infinite loop

b)

012345678910

c)

0

d)

Nothing

52.

What will the following loop print?

int i = 1;

while (i>0)

{ System.out.println(i); }

a)

012345678910

b)

nothing

c)

infinite loop

d)

0

53.

for(int i; i<10; i++) {} Why won’t this code segment run?

a)

There is a missing semicolon at the end.

b)

For should be capitalized

c)

The person did not set the beginning value of i.

d)

The person did not set the ending value of i.

54.

What does this loop do?

a)

There is an error; it won’t run.

b)

It prints all the odd numbers from 0 - 20 inclusive on both ends.

c)

It prints all the even numbers from 0 - 20 inclusive on both ends.

d)

It prints all the even numbers from 0 - 20 inclusive with 0 and exclusive with 20.