Java Generics Practice
Generic classes and methods with type parameters.
1
Generic Print Method
Easy
Write a generic method static void printItem(T item) that prints the item. Read a string, call printItem with it.
Input Format
A single line containing a string.
Output Format
The same string.
Sample Test Cases
|
Sample Input 1
Hello |
Sample Output 1
Hello |
Constraints
Length
Explanation
Type parameter before return type.
2
Generic Box Class
Easy
Create a generic class Box with a field T value, a setter set(T) and a getter get(). Read a string, store it, and print box.get().
Input Format
A single line containing a string.
Output Format
The stored string.
Sample Test Cases
|
Sample Input 1
Apple |
Sample Output 1
Apple |
Constraints
Length
Explanation
T is a placeholder for the type.
1
Generic Method - Array Print
Medium
Write a generic method static void printArray(T[] arr) that prints each element on its own line. In main create a String[] of two values and call it.
Input Format
No input (uses a fixed array).
Output Format
Two lines.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Java Generics |
Constraints
—
Explanation
Arrays of reference types only (no primitives).
2
Two-Type Generic
Medium
Create a class Pair with fields key and value, constructor and getters getKey()/getValue(). Read a string key and an integer value, create a Pair, print "key -> value".
Input Format
Two lines: key string, then integer.
Output Format
key -> value
Sample Test Cases
|
Sample Input 1
rank 1 |
Sample Output 1
rank -> 1 |
Constraints
1
Explanation
Two type parameters.
3
Bounded Type Parameter
Medium
Write a generic method static double sum(T a, T b) returning a.doubleValue() + b.doubleValue(). Read two integers and print the double sum.
Input Format
A single line with two integers.
Output Format
The sum as a decimal (e.g. 15.0).
Sample Test Cases
|
Sample Input 1
7 8 |
Sample Output 1
15.0 |
Constraints
1
Explanation
The bound gives access to Number methods.
1
Generic Max Method
Hard
Write a generic method static T max(T a, T b) using compareTo. Read two strings and print the lexicographically larger.
Input Format
A single line with two strings.
Output Format
The larger string.
Sample Test Cases
|
Sample Input 1
apple banana |
Sample Output 1
banana |
|
Sample Input 2
zebra apple |
Sample Output 2
zebra |
Constraints
Both length
Explanation
Comparable bound enables compareTo.
2
Wildcard (? extends)
Hard
Write a method static double sumList(List
Input Format
First line: N. Second: N ints.
Output Format
The integer sum.
Sample Test Cases
|
Sample Input 1
4 1 2 3 4 |
Sample Output 1
10 |
Constraints
1
Explanation
Wildcard accepts Integer lists.
3
Generic Swap
Hard
Write a generic method static void swap(T[] arr, int i, int j). Read N and N strings, then two indices, swap and print the array space-separated.
Input Format
First line: N. Second: N strings. Third: i j.
Output Format
The array after swapping.
Sample Test Cases
|
Sample Input 1
3 a b c 0 2 |
Sample Output 1
c b a |
Constraints
1
Explanation
Use a T temp variable.
Competitive MCQs — Java Generics
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 30
Q1
What is the purpose of generics?
Correct!
Wrong — correct answer is .
Generics let collections/methods work with types safely.
Q2
Which of these declares a generic class?
Correct!
Wrong — correct answer is .
A type parameter in angle brackets makes Box generic.
Q3
What is the output of this code?
java
1
2
3
4
2
3
4
ArrayList<String> list = new ArrayList<>();
list.add("Hi");
String s = list.get(0);
System.out.println(s);Correct!
Wrong — correct answer is .
Generics remove the need to cast; get returns String.
Q4
What is the output of this code?
ArrayList list = new ArrayList<>();
list.add("Hi");
String s = list.get(0);
System.out.println(s);
ArrayList
list.add("Hi");
String s = list.get(0);
System.out.println(s);
Correct!
Wrong — correct answer is .
Generics remove the need to cast; get returns String.
Q5
Which code causes a compile error?
Correct!
Wrong — correct answer is .
Type mismatch is caught at compile time.
Q6
What is a type parameter bounded by Comparable used for?
Correct!
Wrong — correct answer is .
gives access to compareTo().
Q7
What does > mean in generics?
Correct!
Wrong — correct answer is .
The wildcard ? stands for an unknown type.
Q8
Which is a correct generic method declaration?
Correct!
Wrong — correct answer is .
The type parameter goes before the return type.
Q9
Why is primitive int not allowed as a type argument?
Correct!
Wrong — correct answer is .
Type arguments must be reference types (Integer, etc.).
Q10
What is type erasure?
Correct!
Wrong — correct answer is .
After compilation, generic types are erased to raw types.
Q11
Which is the recommended raw-type-free declaration?
Correct!
Wrong — correct answer is .
Diamond infers the type; raw List is discouraged.
Q12
What is the output of this code?
Box b = new Box<>();
b.set("hi");
System.out.println(b.get());
Box
b.set("hi");
System.out.println(b.get());
Correct!
Wrong — correct answer is .
The generic get() returns the stored String "hi".
Q13
What is a type parameter?
Correct!
Wrong — correct answer is .
T in Box is replaced with a concrete type at use.
Q14
What is type safety?
Correct!
Wrong — correct answer is .
Generics move many type errors to compile time.
Q15
What is the output of this code?
List n = new ArrayList<>();
n.add(10);
int x = n.get(0);
System.out.println(x);
List
n.add(10);
int x = n.get(0);
System.out.println(x);
Correct!
Wrong — correct answer is .
Generics allow direct assignment without casting.
Q16
Which is a bounded type parameter?
Correct!
Wrong — correct answer is .
extends bounds T to Number and its subclasses.
Q17
What is the output of this code?
static T first(List list) {
return list.get(0);
}
System.out.println(first(List.of("a", "b")));
static
return list.get(0);
}
System.out.println(first(List.of("a", "b")));
Correct!
Wrong — correct answer is .
The generic method returns the first element, "a".
Q18
Why can't primitives be type arguments?
Correct!
Wrong — correct answer is .
Use wrappers like Integer instead of int with generics.
Q19
What is a wildcard?
Correct!
Wrong — correct answer is .
? stands for some unknown type in generics.
Q20
What is the output of this code?
List> list = List.of(1, "two");
System.out.println(list.size());
List> list = List.of(1, "two");
System.out.println(list.size());
Correct!
Wrong — correct answer is .
A wildcard list still knows its size.
Q21
What does extends Number> mean?
Correct!
Wrong — correct answer is .
The upper bound restricts the wildcard to Number subtypes.
Q22
Which is TRUE about generic methods?
Correct!
Wrong — correct answer is .
Generic methods declare their own type parameter in the signature.
Q23
What is the output of this code?
class Pair {
K k;
V v;
Pair(K k, V v) {
this.k = k;
this.v = v;
}
}
Pair p = new Pair<>("age", 20);
System.out.println(p.k + p.v);
class Pair
K k;
V v;
Pair(K k, V v) {
this.k = k;
this.v = v;
}
}
Pair
System.out.println(p.k + p.v);
Correct!
Wrong — correct answer is .
String concatenation joins "age" with 20 → "age20".
Q24
Which is the diamond operator?
Correct!
Wrong — correct answer is .
The diamond lets the compiler infer the type argument.
Q25
What is the output of this code?
List list = new ArrayList<>();
list.add("x");
// list.add(5); // would fail because...
List
list.add("x");
// list.add(5); // would fail because...
Correct!
Wrong — correct answer is .
Generics reject mismatched types at compile time.
Q26
What is a generic interface?
Correct!
Wrong — correct answer is .
Interfaces like Comparable declare type parameters.
Q27
What is the output of this code?
static> T max(T a, T b) {
return a.compareTo(b) >= 0 ? a : b;
}
System.out.println(max(3, 7));
static
return a.compareTo(b) >= 0 ? a : b;
}
System.out.println(max(3, 7));
Correct!
Wrong — correct answer is .
The bound allows compareTo; 7 is larger.
Q28
Which of these is a raw type usage?
Correct!
Wrong — correct answer is .
Omitting the type argument produces a raw type.
Q29
Why prefer generics over casts?
Correct!
Wrong — correct answer is .
Generics avoid manual casts and catch errors earlier.
Q30
What is the output of this code?
class Box {
T val;
void set(T v) {
val = v;
}
}
Box b = new Box<>();
b.set(42);
System.out.println(b.val);
class Box
T val;
void set(T v) {
val = v;
}
}
Box
b.set(42);
System.out.println(b.val);
Correct!
Wrong — correct answer is .
Box stores the Integer 42.