Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Quiz

Total questions: 20

Worksheet time: 12mins

Name
Class
Date
1.

Which Java collection does not allow duplicate elements?

a)

HashSet

b)

ArrayList

c)

LinkedList

d)

TreeMap

2.

Which Java collection allows access to elements using index-based methods?

a)

Set

b)

Queue

c)

Map

d)

List

3.

Which Java collection ensures that elements are stored in ascending order based on their natural ordering or a custom comparator?

a)

HashMap

b)

TreeSet

c)

LinkedHashMap

d)

HashSet

4.

What interface in Java Collection Framework allows you to store key-value pairs?

a)

Set

b)

List

c)

Map

d)

Collection

5.

Which is the root interface used for storing elements and provides methods to manipulate them?

a)

List

b)

Map

c)

Collection

d)

Set

6.

Which Java collection guarantees insertion order?

a)

HashSet

b)

TreeMap

c)

LinkedHashMap

d)

HashMap

7.

Which Java collection class provides a resizable array implementation?

a)

HashSet

b)

ArrayList

c)

HashMap

d)

LinkedList

8.

Which of the following is NOT a primitive data type in Java?

a)

int

b)

float

c)

double

d)

String

9.

What happens when you concatenate a String with a primitive data type in Java?

a)

The primitive data type is automatically converted to a String before concatenation.

b)

The primitive data type remains unchanged.

c)


An error occurs because primitive data types cannot be concatenated with Strings.

d)

The primitive data type is converted to its corresponding object wrapper class before concatenation.

10.

What does the == operator compare when used with reference types in Java?

a)

It compares the contents of the objects.

b)

It compares the memory addresses of the objects.

c)

it compares the sizes of the objects.

d)

It compares the types of the objects.

11.

When should you use the equals method instead of the == operator to compare objects in Java?

a)

When comparing primitive data types.

b)

When comparing the contents of objects.

c)

When comparing memory addresses of objects.

d)

When comparing the types of objects.

12.

What is the purpose of the pom.xml file in Maven projects?

a)

It contains the source code of the project.

b)

It defines project metadata, dependencies, and build settings.

c)

It specifies the main class of the project.

d)

It is used for unit testing configurations.

13.

Which method in the String class is used to concatenate two strings?

a)

concat()

b)

append()

c)

concatenate()

d)

add()

14.

What does the trim() method do in the String class?

a)

It removes leading and trailing whitespace from the string.

b)

It splits the string into an array of substrings based on a delimiter.

c)

It converts the string to uppercase.

d)

It replaces all occurrences of a specified character with another character.

15.

Which method in the String class is used to obtain the length of a string?

a)

size()

b)

length()

c)

getSize()

d)

count()

16.

Which statement in Java is used to declare an array?

a)

int array;

b)

array<int>;

c)

int[] array;

d)

array = new int[];

17.

Which loop structure in Java is best suited for iterating through an array?

a)

for loop

b)

while loop

c)

do-while loop

d)

enhanced for loop (for-each loop)

18.

What is the correct way to access the third element of an array named arr in Java?

a)

arr.get(2)

b)

arr[2]

c)

arr.get(3)

d)

arr[3]

19.

What does the following for loop do?

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

a)

It iterates 5 times.

b)

It iterates 4 times.

c)

It iterates indefinitely.

d)

It iterates once.

20.

Which loop structure in Java is best suited for situations where the loop body needs to be executed at least once?

a)

for loop

b)

while loop

c)

do-while loop

d)

enhanced for loop (for-each loop)