Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Generics Quiz 1 | DSA | Fall 2025

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

Which of the following is a correct way to define a generic method that prints elements of any type array?

a)

public static printArray(T[] inputArray)

b)

public static void printArray(T[] inputArray)

c)

public static T printArray(T[] inputArray)

d)

public static void printArray(E inputArray)

2.

What will be the output of the following code snippet? Assume Box is like variable.

Box integerBox = new Box<>();

integerBox.setValue(25);

System.out.println(integerBox.getValue());

a)

Compilation error

b)

25

c)

null

d)

"25"

3.

Why can't we use primitive types like int, double directly in generic classes or methods?

a)

Generics only support class types

b)

Java compiler doesn't allow it

c)

Generics were designed to avoid performance issues

d)

Primitive types don't have methods

4.

Which of the following generic method headers correctly restricts a type to subclasses of Double?

a)

public <T extends Number> void method(T number){}

b)

public void method(T number){}

c)

public <T extends Double> void method(T number){}

d)

public void method(T extends Number number)

5.

Consider this method:

public <T extends Number> boolean isEqual(T number1, T number2)

What does this method ensure?

a)

Accepts all object types

b)

Accepts only primitive data types

c)

Accepts only types that are subclasses of Number

d)

Accepts only Integer type

6.

Which feature of Java allows code reuse across different types without type casting?

a)

Inheritance

b)

Encapsulation

c)

Polymorphism

d)

Generics

7.

What is the output of the following?

GenericClass g = new GenericClass<>("One", "Two");

System.out.println(g);

a)

One Two

b)

[One, Two]

c)

obj1: One obj2: Two

d)

Compilation error

8.

Can You Store data of different Data Type in ArrayList?

a)

YES

b)

NO

9.

What is the key advantage of using ArrayList over arrays when working with generics in Java?

a)

Performance

b)

Fixed size

c)

Dynamic resizing

d)

Faster compilation

10.

What is the correct way to create an ArrayList of a custom generic type Maps?

Here T refers to some DataType...

a)

ArrayList<T> list = new ArrayList();

b)

ArrayList list = new ArrayList<>();

c)

ArrayList<T> mapList = new ArrayList<T>();

d)

GenericList list = new ArrayList<>();

11.

What does the following method do?

public static int search(T[] a, T key)

a)

Sorts the array

b)

Finds the index of a key in the array

c)

Filters nulls from array

d)

Adds the key to the array

12.

Which of the following would be valid class declaration for a generic class that stores two objects of the same type?

a)

class Data<T> {

int obj1,

String obj2;

}

b)

class Data<T> {

String obj1,

String obj2;

}

c)

class Data<T> {

String obj1;

String obj2;

}

d)

class Data<T> {

T obj1;

T obj2;

}

13.

Which interface or class is a better alternative to use for storing key-value pairs in a generic way?

a)

List

b)

Map

c)

Set

d)

Pair

14.

Given the following declaration, which method prints all entries correctly?

public static void printArray(Maps[] array)

a)

Use for-each loop inside the method

b)

Print array directly

c)

Use stream API only

d)

Not possible to print arrays of generic types

15.

What is the benefit of using bounded type parameters in generic methods?

a)

Increased runtime performance

b)

Enforces type safety and restricts input

c)

Makes code shorter

d)

Allows working with primitive types

16.

Which functional interface is used to define custom ordering for objects?

a)

Comparable

b)

Comparator

c)

Cloneable

d)

Serializable

17.

What is the correct method signature for the compare method in the Comparator interface?

a)

public int compare(Object o)

b)

public boolean compare(T o1, T o2)

c)

public int compare(T o1, T o2)

d)

public void compare(T o1, T o2)

18.

What value should compare(o1, o2) return when o1 should come before o2?

a)

Positive value

b)

Zero

c)

Negative value

d)

Any random number

19.

Which statement about Comparator is TRUE?

a)

A class can implement only one Comparator.

b)

Comparator imposes natural ordering.

c)

Comparator allows defining multiple ways to compare objects.

d)

Comparator cannot be used with Collections.sort()

20.

Which of the following correctly passes a Comparator to sort a list?

a)

Collections.sort(list);

b)

Collections.sort(list, new Comparator());

c)

Collections.sort(list, comparator);

d)

list.sort();