wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Quiz - Frankenberger 2022

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

How do you compare two String objects (str1 and str2) to see if they are equal?

a)

if (str1 == str2)

b)

if (str1.equals(str2))

c)

if (str1 = str2)

2.

Which of the following would print the integer 13?

a)

System.out.println(arr[0])

b)

System.out.println(arr[1])

c)

System.out.println(arr[2])

d)

System.out.println(arr[3])

3.

Are the following lines a compiler error, or a runtime error?

char c = "a";

System.out.println(c);

a)

Compiler Error

b)

Runtime Error

4.

What is the output of the following code segment?

(a)  

5.

What is the range of the following random number?

double random = Math.random();

a)

(inclusive) 0 - 100 (inclusive)

b)

(inclusive) 0-1 (inclusive)

c)

(inclusive) 0-1 (exclusive)

d)

The code causes a compiler error because Math.random() must be stored as an int

6.
a)

95

b)

45

c)

14

d)

59

7.

Which is the correct way to initialize a Scanner object with the name s?

a)

Scanner(System.in) = new Scanner s;

b)

Scanner s = new Scanner(System.in);

c)

s Scanner = new Scanner(System.in);

8.

Which of the following returns the length of an array with the name arr?

a)

arr.length()

b)

arr.size()

c)

length(arr)

d)

arr.length

9.

Which of the following Java keywords allows you to exit a loop?

a)

final

b)

static

c)

break

d)

abstract

10.

Which of the following operators is used to concatenate two or more strings?

a)

&&

b)

||

c)

*

d)

+

11.

Which operator allows you to check for more than one clause at a time, but only one has to be correct?

a)

&&

b)

||

c)

==

d)

!=

12.

What is returned from the following method call?

System.out.println(mystery(10));

a)

10

b)

10.0

c)

The code causes a compile error, because the method returns a double, but the parameter is an int.

13.

String x = "Intro to Java";

int i = x.indexOf("j");

What is the value of i?

a)

0

b)

-1

c)

9

d)

The code does not compile

14.

What is printed after the execution of the following code?

a)

A

b)

B

c)

C

d)

D

e)

F

15.

What is printed after this code execution?

a)

A

b)

B

c)

C

d)

D

e)

E

16.

What is printed after the code finishes executing?

a)

Compiler Error

b)

Nothing is printed

c)

14121086420

d)

02468101214

17.

What is the final value of i?

a)

14

b)

-1

c)

16

d)

15

18.

What is printed from the following code execution?

a)

16

b)

16.9

c)

0.9

d)

17

19.
a)

5!

b)

4!

c)

2.3!

d)

5.1!

20.
a)

Result: 1.43.7

b)

Result: 5.1

c)

Result: 5

21.

True or False?

This is the correct way to write the main method:

public static void main(String[] args)

a)

True

b)

False

22.

String[] arr = new String[3];

arr[0] = "Hello";

arr[1] = "World";

arr[2] = "!";

What is the value of the following variable?

String x = arr[0] + arr[1] + arr[2];

a)

Hello World!

b)

HelloWorld!

c)

Hello World

d)

HelloWorld

23.

What value is printed at the end of the following code execution?

a)

Hello!

b)

Nothing is printed

c)

The while statement causes a syntax error

d)

The code will continuously print Hello!

(Infinite Loop)

24.

What is printed when the following method is called?

a)

Hello

b)

Hello

there

c)

The code does not compile, because the method is returning nothing.

d)

The code does not compile because there is an unreachable statement, causing a compile error.

25.

The following method is supposed to take two int parameters and return x^y. For example, if the method call is square(5,3) it should return 5^3 or 125.

However, the program is not working as intended.

Why?

a)

The math class must be imported before any methods can be used from it.

b)

Math.pow() expects the base to come first, and then the exponent to come after.

Ex: Math.pow(x,y)

c)

Math.pow() returns an integer, and the method returns a double, causing a compile error.