wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Repetition Control Structures

Total questions: 50

Worksheet time: 18mins

Name
Class
Date
1.

Which statement best defines looping in programming as presented: it repeats a program or a section of a program, replacing each repetition with new data, and continues while a specified condition is met?

a)

Executing a program once with constant input values

b)

Repeating a program or section with new data while a condition is satisfied

c)

Randomly selecting different code paths on each run

d)

Compiling code multiple times to optimize performance

2.

Which statement about loops is emphasized: loops can execute a block of code as long as a specified condition is reached and substitute new data for each repetition?

a)

Loops always execute exactly once regardless of conditions

b)

Loops run only when input data never changes

c)

Loops execute repeatedly while a condition is satisfied and use new data each time

d)

Loops require manual restarting after each pass

3.

What is the primary purpose of a counter in a program loop?

a)

To randomize loop entries for testing

b)

To keep track of the number of times a program segment is repeated

c)

To store user credentials during iteration

d)

To convert boolean conditions into integers

4.

Before execution of counting in a loop, what should be done to all counters?

a)

Set them to null to avoid overflow

b)

Initialize them (usually to zero) before the loop executes

c)

Multiply them by the loop limit to save time

d)

Declare them as constants to prevent changes

5.

Which statement describes how a predetermined number of passes relates to program termination?

a)

A program cannot terminate based on loop passes

b)

After completing a predetermined number of passes, the program may then be terminated

c)

Termination happens only when the counter overflows

d)

Programs must always ask the user to stop the loop

6.

During the Initialization step in a control loop, where is the counter value set and to what typical value?

a)

Inside the loop; typically set to the loop limit

b)

Outside the loop; typically set to zero or one

c)

Inside the loop; typically set to a random value

d)

Outside the loop; typically left uninitialized

7.

In the Test for Limit Conditions step, when is the loop-terminating condition checked according to the description?

a)

Only in the middle of the loop body

b)

At either the beginning or the end of a loop

c)

After the loop has fully completed

d)

Only when an exception occurs

8.

What happens during Incrementation/Decrementation in a control loop?

a)

The loop condition is replaced with a constant value

b)

One is added or subtracted after each loop so the counter reflects the number of operations performed

c)

The program resets the counter to zero each time

d)

The loop skips directly to termination

9.

Which loop type in Java tests the condition before executing the loop body and repeats while the condition is true?

a)

do...while loop

b)

for-each (enhanced for) loop

c)

while loop

d)

recursive method

10.

Which Java loop type tests its condition at the end of the loop body, allowing the body to execute at least once?

a)

while loop

b)

do...while loop

c)

enhanced for-each loop

d)

switch loop

11.

According to the table, which loop executes a sequence of statements multiple times and abbreviates code that manages the loop variable?

a)

while

b)

do...while

c)

for

d)

try-catch

12.

Which control statement terminates a loop or switch and transfers execution to the statement immediately following it?

a)

continue

b)

break

c)

return from main

d)

throw

13.

What is the effect of the continue statement in a loop as described?

a)

Ends the current loop entirely and moves on

b)

Skips the remainder of the loop body and immediately retests the condition

c)

Pauses the loop and waits for user input

d)

Reinitializes the loop counter to zero

14.

You need a loop that ensures the body executes at least once even if the condition is false initially, and you also need to count iterations starting from zero set outside the loop and increment each pass. Which combination best matches these requirements?

a)

while loop with counter initialized inside and decremented each pass

b)

do...while loop with counter initialized outside and incremented after each iteration

c)

for loop with no counter and condition checked only at the start

d)

recursive calls with no explicit condition check

15.

In Java, what is the primary purpose of a while loop?

a)

To execute a block exactly once regardless of conditions

b)

To repeat a statement or block while a given boolean expression remains true

c)

To iterate a fixed number of times predetermined at compile time

d)

To declare a condition without executing any statements

16.

Select the correct Java syntax template for a while loop that executes one or more statements while a condition holds.

a)

while condition do { statements }

b)

while (boolean_expression) { statement1; statement2; }

c)

do { statement1; } until (boolean_expression)

d)

repeat (boolean_expression) -> { statements; }

17.

According to the description, when do the statements inside a while loop execute?

a)

Only once before the condition is checked

b)

As long as the boolean expression evaluates to true

c)

Only after the loop completes all iterations

d)

When the boolean expression evaluates to false

18.

Based on the described mechanism, what happens immediately after the statements inside a while loop execute?

a)

The loop exits unconditionally

b)

The test expression is evaluated again

c)

The boolean expression is inverted for the next check

d)

The loop counter resets to its initial value

19.

Refer to the flowchart of the while loop. What is the decision made at the diamond-shaped node labeled Test Expression?

a)

Whether to initialize the loop variable

b)

Whether the condition is true to enter/continue the loop body or false to exit

c)

Which of several cases to select using a switch statement

d)

Whether to print the current value before incrementing

20.

Consider the Java code:

int num = 1;

while (num <= 5) {

System.out.println("Line " + num);

++num; }

What is the last value printed and why does the loop stop?

a)

Line 5; after printing 5, num becomes 6, making num <= 5 false

b)

Line 6; the loop prints after incrementing to 6 and then stops

c)

Line 4; the loop stops before reaching the boundary value

d)

Line 5; the loop stops because ++num prevents the print from executing on the last iteration

21.

If the increment statement in the example were moved above the println, which output would result? Assume the code becomes:

int num = 1;

while (num <= 5) {

++num;

System.out.println("Line " + num); }

a)

Line 1 to Line 5 (unchanged)

b)

Line 2 to Line 6 (six lines printed)

c)

Line 2 to Line 6 (five lines printed)

d)

Line 1 to Line 4 (four lines printed)

22.

In Java, consider:

int sum = 0, i = 5;

while (i != 0) {

sum += i; --i;

}

System.out.println("Sum = " + sum);

What value is printed for sum?

a)

5

b)

10

c)

15

d)

20

23.

Given the snippet:

int i = 4;

while (i > 0) {

System.out.print(i);

i--; }

What is the output and why does the loop terminate?

a)

1234 because i increases to reach 4

b)

4321 because i starts at 4 and is decremented until i becomes 0

c)

43210 because the loop includes 0 before stopping

d)

3210 because printing starts after the first decrement

24.

In the code

int i = 4;

while (i > 0) {

System.out.print(i);

/* missing i--; */ }

what behavior occurs?

a)

The loop runs exactly four times

b)

The program throws a compile-time error

c)

An infinite loop occurs because i never changes and the condition stays true

d)

The loop runs once due to automatic decrement

25.

A countdown program reads an integer n and executes:

while (n > 0) {

System.out.print(n + "," );

--n; }

System.out.print("Fire!");

If the user enters 4, what is the exact console output?

a)

4,3,2,1,Fire!

b)

4 3 2 1 Fire!

c)

4,3,2,1, Fire! (with a space before Fire!)

d)

3,2,1,0,Fire!

26.

In the countdown example, when does the loop body execute?

a)

Only when n is equal to 0

b)

While the condition n > 0 remains true

c)

Exactly n times regardless of n's value

d)

Until the user presses Enter

27.

In the Java snippet: while (n<5) { System.out.print("Enter the score: "); score = scan.nextInt(); sum = score + sum; n++; } Which variable controls the loop termination, and what change makes the loop eventually stop?

a)

score controls the loop; entering 0 stops it automatically

b)

n controls the loop; incrementing n each iteration leads to n reaching 5

c)

sum controls the loop; it stops when sum exceeds 5

d)

average controls the loop; assigning average = sum/5 ends the loop

28.

Given the console interaction shown:

Enter the score: 1, 3, 5, 6, 3.

The program then prints The Average is: 3.6.

Which expression in the code computes this value?

a)

average = sum/5;

b)

average = sum/n; with n equal to the number of inputs

c)

average = (score + sum)/n;

d)

average = sum/3;

29.

In the example using JOptionPane, what input will cause the while loop to terminate? The condition is:

while (Integer.parseInt(jop.showInputDialog("Type non-zero digit to accept and zero to stop")) != 0) {

continue;

}

a)

Any non-zero digit, because continue exits the loop

b)

The digit 0, because the condition becomes false

c)

A negative number only, because parseInt rejects zero

d)

Cancel button, because parseInt returns -1

30.

Which statement about continue in Example 5 is accurate?

a)

continue immediately ends the loop and moves to the code after the loop

b)

continue skips the current iteration and re-evaluates the while condition

c)

continue converts the user input to zero

d)

continue displays the 'Bye' message dialog

31.

After the GUI loop in the example ends, which actions occur according to the code?

a)

The program prints the average of all inputs and then exits.

b)

A message dialog saying "Bye" is shown, then System.exit(0) terminates the program.

c)

Inputs are written to a file and the window closes automatically.

d)

The loop restarts because System.exit(0) returns control to main.

32.

Which statement best describes a unique characteristic of the do-while loop compared to the while loop?

a)

The condition is checked before any statements execute, so the body may never run.

b)

The loop body executes at least once before the condition is evaluated.

c)

It can only iterate a fixed number of times determined at compile time.

d)

It requires multiple conditions to continue looping.

33.

According to the explanation, what happens first in a do-while loop’s execution cycle?

a)

The test expression is evaluated and, if true, the body executes.

b)

The body executes once, then the test expression is checked.

c)

Both the test expression and one statement execute simultaneously.

d)

The loop checks for a false condition before running the body.

34.

Refer to the flowchart of the do-while loop. After the body executes, the flow proceeds to the decision node labeled Test Expression. If the outcome is True, what occurs next?

a)

Control exits the loop immediately.

b)

The body of the do-while loop executes again.

c)

The condition is ignored and the program terminates.

d)

The loop converts into a while loop automatically.

35.

In the provided Java example, which input value causes the loop to terminate?

a)

Any negative value

b)

A non-integer numeric value

c)

The value 0.0

d)

The largest value entered so far

36.

In the Java example, what is the role of the statement sum += number; placed inside the do block?

a)

It resets the sum to zero before each iteration.

b)

It adds the current input number to the running total.

c)

It checks whether the user entered 0 to stop the loop.

d)

It converts the input to an integer before summing.

37.

A programmer wants to ensure that a prompt for user input displays at least once, even when the test condition is initially false. Based on the material, which loop structure should be chosen and why?

a)

Use a while loop because it evaluates the condition before the first execution.

b)

Use a do-while loop because it executes the body once before testing the condition.

c)

Use a for loop because it has initialization, condition, and update in one line.

d)

Use recursion because it always runs the base case before any checks.

38.

Given the Java do-while snippet for squaring numbers:

int y, x = 1, total = 0;

do {

y = x * x;

System.out.println(y + total);

total += y;

++x; }

while (x <= 5);

What is the final value printed by System.out.print("Total is " + total); after the loop finishes?

a)

25

b)

30

c)

55

d)

60

39.

In the square-sum program that prints 1, 5, 14, 30, 55 and then "Total is 55", what does the intermediate line 14 represent?

a)

The cube of 3

b)

The running total after adding 323^2

c)

The square of 4

d)

The number of iterations left

40.

Consider this guidance: "The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, where the user input within the block is what is used to determine if the loop has to end." Which scenario best matches this usage?

a)

Iterating over a fixed array of 10 items

b)

Repeating a menu until the user types 0 to quit

c)

Running a loop exactly five times using a counter

d)

Processing a file until end-of-file using a for loop

41.

In the password-checking program, the code uses pass.equalsIgnoreCase("jru"). What is the effect of equalsIgnoreCase here?

a)

It trims whitespace before comparing

b)

It compares strings while ignoring letter case differences

c)

It accepts any substring that contains "jru"

d)

It converts the input to lowercase permanently

42.

Study the password program structure: a Scanner reads a username, then in a do-while loop it asks for a password up to three attempts. If the password matches, it prints "accepted" and calls System.exit(0). What happens if the user never enters the correct password within three attempts?

a)

The program loops forever asking for passwords

b)

The program throws a compile-time error

c)

The loop ends after three tries and the program continues past the loop

d)

The program restarts and asks for the username again

43.

Which output demonstrates that equalsIgnoreCase accepts both lowercase and uppercase forms of the same password?

a)

Enter Password: jru → accepted, Enter Password: JRU → accepted

b)

Enter Password: PUP → invalid, Enter Password: ceu → invalid

c)

Enter Password: upLB → invalid only

d)

Enter Password: jru → invalid each time

44.

According to the coding guidelines, which is a common mistake when writing a do-while loop?

a)

Placing the condition before the loop body

b)

Using break instead of continue

c)

Forgetting the semicolon after the while(condition)

d)

Declaring loop variables outside the method

45.

Based on the guideline "make sure that your do-while loops will terminate at some point," which change best ensures termination in a user-prompt loop?

a)

Remove any input checks so the loop always executes

b)

Add a counter limit or a sentinel input such as 0 to exit

c)

Replace do-while with while(true)

d)

Use System.exit(0) in every iteration

46.

In the number-entry example preceding the square-sum, the loop keeps prompting "Enter a number:" and then prints the running sum, stopping when the user enters 0. Which control pattern is being used to stop the loop?

a)

Counter-controlled loop with fixed iterations

b)

Flag-controlled loop without input

c)

Sentinel-controlled loop using 0 as the sentinel

d)

Exception-controlled loop using try-catch

47.

In the Java snippet for computing a power, which variable is initialized to 1 so that repeated multiplication produces the correct result of base^exp inside the while loop?

a)

base

b)

exp

c)

power

d)

counter

48.

Given the power program: base and exp are read via JOptionPane, power starts at 1, counter starts at 0, and while(counter < exp){ power = power * base; counter++; }. If base = 3 and exp = 4, what value is displayed for power?

a)

9

b)

12

c)

27

d)

81

49.

In the vowel/consonant counting program, which control structure checks each character of the input string to classify it as a vowel or consonant?

a)

An if-else chain outside the loop

b)

A switch statement inside a while loop

c)

A for-each loop over characters

d)

A do-while loop with nested if statements

50.

According to the vowel/consonant program, which set of characters is treated as vowels during the switch classification?

a)

a, e, i, o, u in lowercase only

b)

A, E, I, O, U in uppercase only

c)

Both lowercase and uppercase a, e, i, o, u

d)

All letters except y