wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Looping Concepts

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

James is trying to complete a task repeatedly until a certain condition is met. What is the basic syntax of a while loop in Java that he should use for this?

a)

for (condition) { // code }

b)

repeat (condition) { // code }

c)

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

d)

do while (condition) { // code }

2.

Imagine Charlotte is trying to print the numbers from 0 to 4 using a do while loop in Java. How would she write this loop?

a)

for (int k = 0; k < 5; k++) { System.out.println(k); }

b)

int i = 0; do { System.out.println(i); i++; } while (i < 5);

c)

int j = 0; while (j < 5) { System.out.println(j); j++; }

d)

int x = 0; do { x++; } while (x < 5);

3.

Ava is learning how to code in Java and wants to understand how to create a loop that repeats a certain action. She comes across the structure of a for loop. What is the structure of a for loop in Java?

a)

for(initialization; condition; increment/decrement) { // code to be executed }

b)

for(increment; condition; initialization) { // perform action }

c)

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

d)

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

4.

Mia is keeping track of her savings in a Java program. How does she increment her savings variable?

a)

Use 'savings += 1' to increment her savings in Java.

b)

Apply 'savings = savings + 1' for incrementing her savings in Java.

c)

Use 'savings++' or '++savings' to increment her savings in Java.

d)

Invoke 'increment(savings)' to increase her savings in Java.

5.

Grace is managing a countdown timer for a project deadline. How does she decrement the timer variable in Java?

a)

timer--1

b)

timer-- or --timer

c)

timer+=1

d)

timer=timer+1

6.

Imagine Zoe is organizing a multi-layered cake for a party. She needs to ensure that each layer has a different flavor and decoration. To do this, she decides to use nested loops: the outer loop represents the layers of the cake, while the inner loop represents the different flavors and decorations for each layer. Explain the concept of nested loops in Java in this context.

a)

Nested loops in Java are loops that cannot contain other loops.

b)

Nested loops in Java are used for single-dimensional iteration.

c)

Nested loops in Java are loops that run only once each.

d)

Nested loops in Java are loops placed inside another loop, enabling multi-dimensional iteration.

7.

Imagine a classroom where a teacher is counting the number of students who have raised their hands to answer questions. The teacher starts counting from 0 and continues until they reach 5, announcing each number as they go: '0', '1', '2', '3', '4'. What will be the output of the following code: 'int i = 0; while(i < 5) { System.out.println(i); i++; }'?

a)

0 1 2 3 4

b)

3.5

c)

5

d)

-1

8.

Sophia is organizing a small event and wants to print the numbers of the guests arriving from 1 to 5. Write a do while loop that helps her achieve this.

a)

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

b)

do { console.log(i); i--; } while (i >= 1);

c)

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

d)

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

9.

Maya is trying to decide whether to go for a run. She checks the weather conditions. If it is raining, she might not go at all. However, if she decides to go for a run, she will definitely step outside at least once to see if the rain stops. What is the difference between a while loop and a do while loop in this scenario?

a)

The main difference is that a while loop may not execute at all if the condition is false, while a do while loop always executes at least once.

b)

Both loops execute the same number of times regardless of the condition.

c)

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

d)

A while loop checks the condition after executing, while a do while loop checks it before.

10.

Noah is a programmer who wants to display all the elements of an array he created. How can he use a for loop to iterate through this array in Java?

a)

for (int i = 0; i < array.length; i++) { System.out.print(array[i]); }

b)

for (int i = 0; i < array.length; i++) { System.out.println(i); }

c)

for (int i = 0; i < array.length; i++) { System.out.println(array[i]); }

d)

for (int i = 1; i <= array.length; i++) { System.out.println(array[i]); }

11.

Imagine Daniel is trying to find a hidden treasure in a cave. He has a map that says he can only find the treasure if he reaches a specific spot. However, if he never finds the right path to that spot, what happens?

a)

The search will happen once.

b)

The search will not happen.

c)

The search will continue forever.

d)

The search will result in an error.

12.

Oliver is organizing a small event and needs to create a seating arrangement. He has 2 tables, and at each table, he can seat 4 guests. Can you give an example of how he might use nested loops to print out the seating arrangement?

a)

for m in range(2): for n in range(4): print(m - n)

b)

for x in range(5): for y in range(3): print(x + y)

c)

for a in range(4): for b in range(1): print(a * b)

d)

for i in range(3): for j in range(2): print(i, j)

13.

Grace is organizing a charity run and needs to keep track of the number of laps each participant completes. How does she use the increment operator in a for loop to count the laps?

a)

Write 'for (int i = 1; i < n; i++)' to increment 'i' by 1.

b)

Use 'for (int i = 0; i <= n; i++)' to increment 'i' by 1.

c)

Use 'for (int i = 0; i < n; i++)' to increment 'i' by 1 in each iteration.

d)

Implement 'for (int i = 0; i < n; i+=2)' to increment 'i' by 2.

14.

Emma is counting down the days until her birthday. She starts from 5 and counts down to 1, saying each number out loud. What does she say?

a)

-1

b)

0

c)

6

d)

5 4 3 2 1

15.

Imagine Michael is teaching a group of students how to create a multiplication table for their math class. He decides to use a nested loop to help them understand how to generate the table efficiently.

a)

A scenario where a nested loop would be useful is creating a simple text-based game.

b)

A scenario where a nested loop would be useful is calculating the average of a dataset.

c)

A scenario where a nested loop would be useful is generating a multiplication table.

d)

A scenario where a nested loop would be useful is sorting a list of names.