WorksheetsJava Generics Quiz 1 | DSA | Fall 2025
Total questions: 20
Worksheet time: 10mins
Which of the following is a correct way to define a generic method that prints elements of any type array?
public static printArray(T[] inputArray)
public static void
public static T printArray(T[] inputArray)
public static void printArray
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());
Compilation error
25
null
"25"
Why can't we use primitive types like int, double directly in generic classes or methods?
Generics only support class types
Java compiler doesn't allow it
Generics were designed to avoid performance issues
Primitive types don't have methods
Which of the following generic method headers correctly restricts a type to subclasses of Double?
public <T extends Number> void method(T number){}
public void method(T number){}
public <T extends Double> void method(T number){}
public void method(T extends Number number)
Consider this method:
public <T extends Number> boolean isEqual(T number1, T number2)
What does this method ensure?
Accepts all object types
Accepts only primitive data types
Accepts only types that are subclasses of Number
Accepts only Integer type
Which feature of Java allows code reuse across different types without type casting?
Inheritance
Encapsulation
Polymorphism
Generics
What is the output of the following?
GenericClass g = new GenericClass<>("One", "Two");
System.out.println(g);
One Two
[One, Two]
obj1: One obj2: Two
Compilation error
Can You Store data of different Data Type in ArrayList?
YES
NO
What is the key advantage of using ArrayList over arrays when working with generics in Java?
Performance
Fixed size
Dynamic resizing
Faster compilation
What is the correct way to create an ArrayList of a custom generic type Maps?
Here T refers to some DataType...
ArrayList<T> list = new ArrayList();
ArrayList
ArrayList<T> mapList = new ArrayList<T>();
GenericList list = new ArrayList<>();
What does the following method do?
public static int search(T[] a, T key)
Sorts the array
Finds the index of a key in the array
Filters nulls from array
Adds the key to the array
Which of the following would be valid class declaration for a generic class that stores two objects of the same type?
class Data<T> {
int obj1,
String obj2;
}
class Data<T> {
String obj1,
String obj2;
}
class Data<T> {
String obj1;
String obj2;
}
class Data<T> {
T obj1;
T obj2;
}
Which interface or class is a better alternative to use for storing key-value pairs in a generic way?
List
Map
Set
Pair
Given the following declaration, which method prints all entries correctly?
public static void printArray(Maps[] array)
Use for-each loop inside the method
Print array directly
Use stream API only
Not possible to print arrays of generic types
What is the benefit of using bounded type parameters in generic methods?
Increased runtime performance
Enforces type safety and restricts input
Makes code shorter
Allows working with primitive types
Which functional interface is used to define custom ordering for objects?
Comparable
Comparator
Cloneable
Serializable
What is the correct method signature for the compare method in the Comparator interface?
public int compare(Object o)
public boolean compare(T o1, T o2)
public int compare(T o1, T o2)
public void compare(T o1, T o2)
What value should compare(o1, o2) return when o1 should come before o2?
Positive value
Zero
Negative value
Any random number
Which statement about Comparator is TRUE?
A class can implement only one Comparator.
Comparator imposes natural ordering.
Comparator allows defining multiple ways to compare objects.
Comparator cannot be used with Collections.sort()
Which of the following correctly passes a Comparator to sort a list?
Collections.sort(list);
Collections.sort(list, new Comparator());
Collections.sort(list, comparator);
list.sort();
