Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Intro to Java Loops

Total questions: 15

Worksheet time: 22mins

Name
Class
Date
1.

Why are loops used in programming?

a)

To make your code look more complex.

b)

To avoid using if statements.

c)

To make your program look more professional.

d)

To run blocks of code repeatedly.

2.

Each time through a loop is called a(n) ______________.

a)

iteration

b)

culmination

c)

concatenation

d)

itemization

3.

What is the final value of x?

int x = 0;

while (x < 5)

x++;

a)

6

b)

5

c)

4

d)

0

e)

infinite loop

4.

What is the final value of x?

int x = 4;

while (x <= 5)

x++;

a)

6

b)

5

c)

4

d)

0

e)

infinite loop

5.

What is the final value of x?

int x = 3;

while (x > 5)

x++;

a)

6

b)

5

c)

4

d)

3

e)

infinite loop

6.

What is the final value of x?

int x = 0;

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

x += i;

a)

12

b)

22

c)

7

d)

0

e)

infinite loop

7.

What is the final value of x?

int x = 0;

for(int i = 5; i > 0; i --)

x ++;

a)

5

b)

4

c)

6

d)

0

e)

infinite loop

8.

What is the output of the following code?

for(int i = 3; i < 20; i +=4)

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

a)

3 7 11 15 19 23

b)

19

c)

3 7 11 15 19

d)

7 11 15 19

e)

23

9.

What is the final value of x?

int x = 3; 

for(int i = 4; i > 3; i += 2)

x++;

a)

3

b)

4

c)

5

d)

7

e)

infinite loop

10.

What is the output of the following code?

int x = 7;

while(x <= 23) {

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

x+=4; }

a)

7 11 15 19

b)

7 11 15 19 23

c)

23

d)

7 11 15 19 23 27

e)

27

11.

What is the output of the following code?

for(int i = 2; i < 60; i *= 3)

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

a)

162

b)

2 6 18 54

c)

6 18 54

d)

2 6 18 54 162

e)

54

12.

What is the output of the following code?

int x = 26;

while(x > 0) {

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

x /= 2; }

a)

26 13 6 3 1

b)

26 13 6.5 3.25 1.625

c)

26 13 6 3 1 0

d)

26 13 6 3

e)

infinite loop

13.

What is the output of the following code?

String str = "Loop";

for(int i = 0; i < str.length(); i++) {

System.out.print("x");

}

a)

xxxxx

b)

xxxx

c)

x

d)

xxx

e)

There will be no output.

14.

What is the output of the following code?

String str = "For Loop";

for(int i = 0; i < str.length() - 1; i++) {

System.out.print(str.charAt(i));

}

a)

pppppppp

b)

For Loop

c)

FFFFFFFF

d)

For Loo

e)

pooL roF

15.

What will be printed by the code shown?

a)

**

**

**

**

b)

****

****

c)

********

********

d)

***

***

***

***

***

e)

*

*

*