wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java: repetition control structure for & while

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What should be written in the bracket?

int count = 0;

for (______? ){

count = count + 2;

}

System.out.print(count);


output : 6

a)

int i = 1; i < 3; i=i+1

b)

int i = 0; i <= 3; i=i+1

c)

int i = 0; i < 3; i=i+1

d)

int i = 0; i = 3; i=i+1

2.

________?__________ ; //initialization

int max = 20;

for (int i = 0; i > max; i=i+1) {

count = pow(count , i);

}

System.out.print(count);


output : 2

a)

double count = 2

b)

Double count = 2

c)

double[ ] count = 2

d)

new double [ ] count = 2

3.

output : 1 2 3 4 5 6 7 8 9

a)

int n = 10;

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

{

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

}

b)

int n = 10;

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

{

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

}

c)

int n = 9;

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

{

System.out.println("\t"+i);

}

d)

int n = 9;

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

{

System.out.println("\t"+i);

}

4.

output :

10

9

8

7

6

a)

for(int x=10;x>5;x--){

System.out.print(x"\n");

}

b)

for(int x=0;x>10;x++){

System.out.print(x"\n");

}

c)

for(int x=6;x>10;x--){

System.out.print(x"\n");

}

d)

for(int x=10;x>=5;x--){

System.out.print(x"\n");

}

5.

output :

0

2

4

6

8

10

12

14

a)

int n = 15;

for(i= 0; i <= n; i+= 2)

{

System.out.print(i"\t");

}

b)

int n = 0;

for(i= 0; i <= 15; i+= 2)

{

System.out.print(i"\n");

}

c)

int n = 15;

for(i= 0; i <= n; i++)

{

System.out.print(i"\n");

}

d)

int n = 15;

for(i= 0; i <= n; i+= 2)

{

System.out.print(i"\n");

}

6.

int count = 0;

int j = 4;

while( j >= 1) {

count = count + j;

j=j-1;

}

System.out.println(count);


what is the output?

a)

12

b)

8

c)

10

d)

14

7.

int i = 0;

while (i < 5) {

System.out.println(i);

i++;

}


what is the output?

a)

0

1

2

3

4

b)

1

2

3

4

5

c)

0

1

2

3

4

5

d)

5

8.

for (int i = 0; i < 5; ) {

System.out.println("BRAVO")

}

what is the output for above statement?

a)

print BRAVO four times

b)

print BRAVO five times

c)

print BRAVO one time

d)

print BRAVO six times

9.

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

if (i == 5) {

break ;

}

System.out.println(i);

}

Statements above is to ___________________

a)

Stop the loop if i is less than 5.

b)

output 1 2 3 4 5 6 7 8 9 10

c)

output 0 1 2 3 4 5

d)

Stop the loop if i is 5.

10.

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

if (i == 4) {

____________ ;

}

System.out.println(i);

}


In the loop, when the value is "4", jump directly to the next value.

a)

break

b)

continue

c)

else

d)

else if