wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java conditional/loop/method/user input

Total questions: 23

Worksheet time: 21mins

Name
Class
Date
1.

(int) (Math.random()*100) generates a random number between 0 and 100, including 0 and 100

a)

True

b)

False

2.

Which of the following represents a single line comment in Java?

a)

/* Comment */

b)

<!-- Comment -->

c)

// Comment

d)

# Comment

3.

Which of the following represents a multi-line comment in Java?

a)

<!-- Comment >

b)

// Comment

c)

/* Comment */

d)

# Comment

4.

public static _____ statement(int x, int y){

String z = "x is: " + x + "and y is: " + y;

return z }

What should the return type be?

a)

void

b)

int

c)

double

d)

String

5.

public static ________ average(int x, int y){

return (double) x / y;

}

What should the return type be?

a)

void

b)

int

c)

double

d)

String

6.

This is an example of a non-return method.

a)

True

b)

False

7.

The name of this method is ....

a)

Oddsum

b)

int Oddsum

c)

int Oddsum (int number)

d)

return totaloddsum

8.

Method make codes more readable and easier to debug.

a)

True

b)

False

9.

What is the correct data type for the following variable:


__________ myAnswer = false;

a)

String

b)

char

c)

int

d)

boolean

10.

what is the meaning of the code below:


x++;

a)

x = 1;

b)

x = x + 1;

c)

x = 2

11.

What is the output of the code shown in the image?

a)

False

b)

Next Round

c)

Game Finished

d)

finished

12.

Which statement(s) properly implements an equality comparison of two String objects?

a)

A, only

b)

B, only

c)

A and C

d)

All are acceptable

13.

What is the output of the code shown?

a)

Sandy

b)

Squidward

c)

Patrick

d)

Spongebob

14.

What is the output of the code shown?

a)

Hello!!

b)

Goodbye!!

15.

A while loop has the following format:

a)

while (condition)

{

...

}

b)

while

{

...

}

c)

continue while (condition)

{

...

}

d)

while [condition]

[

...;

]

16.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

17.

What does the following code print?

a)

5 6 7 8 9

b)

4 5 6 7 8 9 10 11 12

c)

3 5 7 9 11

d)

3 4 5 6 7 8 9 10 11 12

18.
True/False
If a loop condition is never satisfied then the loop does not execute.
a)
True
b)
False
19.

2

a)

5

b)

7

c)

9

d)

13

e)

20

20.
What is the output of the following code?
int x = 0;
while (x < 4) {
 x = x + 1;
}
System.out.println("x is " + x);
a)
0
b)
1
c)
3
d)
4
21.
How many times will the above loop iterate?
a)
0
b)
9
c)
10
d)
11
22.

Which Scanner class method is used to read integer value from the user?

a)

next()

b)

nextInteger()

c)

nextInt()

d)

readInt()

23.

Which is the correct syntax to declare Scanner class object?

a)

Scanner objectName= Scanner();

b)

Scanner objectName= new Scanner();

c)

Scanner objectName= Scanner(System.in);

d)

Scanner objectName= new Scanner(System.in);