WorksheetsIntro to Java Loops
Total questions: 15
Worksheet time: 22mins
Why are loops used in programming?
To make your code look more complex.
To avoid using if statements.
To make your program look more professional.
To run blocks of code repeatedly.
Each time through a loop is called a(n) ______________.
iteration
culmination
concatenation
itemization
What is the final value of x?
int x = 0;
while (x < 5)
x++;
6
5
4
0
infinite loop
What is the final value of x?
int x = 4;
while (x <= 5)
x++;
6
5
4
0
infinite loop
What is the final value of x?
int x = 3;
while (x > 5)
x++;
6
5
4
3
infinite loop
What is the final value of x?
int x = 0;
for(int i = 1; i < 8; i += 3)
x += i;
12
22
7
0
infinite loop
What is the final value of x?
int x = 0;
for(int i = 5; i > 0; i --)
x ++;
5
4
6
0
infinite loop
What is the output of the following code?
for(int i = 3; i < 20; i +=4)
System.out.print(i + " ");
3 7 11 15 19 23
19
3 7 11 15 19
7 11 15 19
23
What is the final value of x?
int x = 3;
for(int i = 4; i > 3; i += 2)
x++;
3
4
5
7
infinite loop
What is the output of the following code?
int x = 7;
while(x <= 23) {
System.out.print(x + " ");
x+=4; }
7 11 15 19
7 11 15 19 23
23
7 11 15 19 23 27
27
What is the output of the following code?
for(int i = 2; i < 60; i *= 3)
System.out.print(i + " ");
162
2 6 18 54
6 18 54
2 6 18 54 162
54
What is the output of the following code?
int x = 26;
while(x > 0) {
System.out.print(x + " ");
x /= 2; }
26 13 6 3 1
26 13 6.5 3.25 1.625
26 13 6 3 1 0
26 13 6 3
infinite loop
What is the output of the following code?
String str = "Loop";
for(int i = 0; i < str.length(); i++) {
System.out.print("x");
}
xxxxx
xxxx
x
xxx
There will be no output.
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));
}
pppppppp
For Loop
FFFFFFFF
For Loo
pooL roF
What will be printed by the code shown?
**
**
**
**
****
****
********
********
***
***
***
***
***
*
*
*
