wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Control and Loop -2

Total questions: 20

Worksheet time: 3600secs

Name
Class
Date
1.

Which of the following will be the output of the above program?

public class Compute {

public static void main (string args [ ]){

int result, x ;

x = 1 ;

result = 0;

while (x < = 10) {

if (x%2 == 0)

result + = x ;

+ + x ;

}

System.out.println(result) ;

}

}

a)

55

b)

30

c)

25

d)

35

2.

class Test {

public static void main(String[] args){

int i = 0, j = 9;

do {

i++;

if (j-- < i++) {

break;

}

} while (i < 5);

System.out.println(i + "" + j);

}

}

a)

44

b)

55

c)

66

d)

77

3.

public class Test{

public static void main(String args[]){

int i = 0, j = 5 ;

for( ; (i < 3) && (j++ < 10) ; i++ ){

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

}

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

}

}

a)

0 6 1 7 2 8 3 8

b)

0 6 1 7 2 8 3 9

c)

0 6 1 5 2 5 3 5

d)

Compilation Error

4.

class Test{

public static void main(String args[]){

int x=7;

if(x==2); // Note the semicolon

System.out.println("NumberSeven");

System.out.println("NotSeven");

}

}

a)

NumberSeven NotSeven

b)

NumberSeven

c)

NotSeven

d)

Error

5.

public class Test{

public static void main(String args[]){

int i, j;

for(i=1, j=0;i<10;i++) j += i;

System.out.println(i);

}

}

a)

10

b)

11

c)

9

d)

20

6.

public class Test{

public static void main(String[] args){

double sum = 0;

for(double d = 0; d < 10;){

d += 0.1;

sum += sum + d;

}

}

}

a)

The program has a compile error because the adjustment is missing in the for loop.

b)

The program has a compile error because the control variable in the for loop cannot be of the double type.

c)

The program runs in an infinite loop because d<10 would always be true.

d)

The program compiles and runs fine.

7.

Which of the following for loops will be an infinite loop?

a)

for(; ;)

b)

for(i=0 ; i<1; i--)

c)

for(i=0; ; i++)

d)

All of the above

8.

What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};

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

a[i] = a[(a[i] + 3) % a.length];

a)

0

b)

1

c)

2

d)

3

9.
Given the nested if-else structure above, If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
10.
Given the nested if-else structure above, if x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
11.
Given the nested if-else structure above, if x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
5
12.
Consider the above code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score.  
a)
This code will work correctly in all cases
b)
This code will work correctly only if grade >= 60
c)
This code will work correctly only if grade < 60
d)
This code will work correctly only if grade < 70
13.
If x is an int where x = 1, what will x be after the above loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
14.
If x is an int where x = 0, what will x be after the following loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
15.
Given the above code, where x = 0, what is the resulting value of x after the for-loop terminates?
for(int i=0;  i<5; i++)    
            x += i;
a)
4
b)
5
c)
10
d)
15
16.
How many times will the above loop iterate?
a)
0
b)
9
c)
10
d)
11
17.
The above nested loop structure will execute the inner most statement (x++) how many times?
a)
100
b)
200
c)
10000
d)
20000
18.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

19.

Which of the following for loop declaration is not valid?

a)

for ( int i = 99; i >= 0; i / 9 )

b)

for ( int i = 7; i <= 77; i += 7 )

c)

for ( int i = 20; i >= 2; - -i )

d)

for ( int i = 2; i <= 20; i = 2* i )

20.

What will be the output of the following program?


public class Test

{

public static void main(String[] args)

{

int count = 1;

while (count <= 15)

{ System.out.println(count % 2 == 1 ? "***" : "+++++"); ++count;

} // end while

} // end main

}

a)

15 times ***

b)

15 times +++++

c)

8 times *** and 7 times +++++

d)

Both will print only once