Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

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.
A single line containing a string.
The same string.
Sample Input 1
Hello
Sample Output 1
Hello
Length
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().
A single line containing a string.
The stored string.
Sample Input 1
Apple
Sample Output 1
Apple
Length
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.
No input (uses a fixed array).
Two lines.
Sample Input 1

                      
Sample Output 1
Java
Generics
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".
Two lines: key string, then integer.
key -> value
Sample Input 1
rank
1
Sample Output 1
rank -> 1
1
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.
A single line with two integers.
The sum as a decimal (e.g. 15.0).
Sample Input 1
7 8
Sample Output 1
15.0
1
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.
A single line with two strings.
The larger string.
Sample Input 1
apple banana
Sample Output 1
banana
Sample Input 2
zebra apple
Sample Output 2
zebra
Both length
Comparable bound enables compareTo.
2
Wildcard (? extends)
Hard
Write a method static double sumList(List
First line: N. Second: N ints.
The integer sum.
Sample Input 1
4
1 2 3 4
Sample Output 1
10
1
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.
First line: N. Second: N strings. Third: i j.
The array after swapping.
Sample Input 1
3
a b c
0 2
Sample Output 1
c b a
1
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
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);
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());
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);
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")));
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());
Correct!
Wrong — correct answer is .
A wildcard list still knows its size.
Q21
What does 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);
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...
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));
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);
Correct!
Wrong — correct answer is .
Box stores the Integer 42.