Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 241–250 of 958
Topics in Java Programming
Q.241 Hard Generics
What will be the result of this code?
List list = new ArrayList();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
A ClassCastException when accessing list.get(1)
B Code executes without error
C Compilation error
D StringIndexOutOfBoundsException
Correct Answer:  A. ClassCastException when accessing list.get(1)
EXPLANATION

Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.

Take Test
Q.242 Hard Generics
Which statement is true about generic inheritance?
A ArrayList is NOT a subtype of List due to invariance
B ArrayList IS a subtype of List
C ArrayList is a subtype of ArrayList
D Generic types support covariance by default
Correct Answer:  B. ArrayList IS a subtype of List
EXPLANATION

ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.

Take Test
Q.243 Easy Generics
What is the output of this generic code?
class Pair {
public void display(K key, V value) {
System.out.println(key + ": " + value);
}
}
Pair p = new Pair();
p.display("Age", 25);
A Age: 25
B Compilation error - type mismatch
C Age: 25.0
D Runtime exception
Correct Answer:  A. Age: 25
EXPLANATION

The generic class Pair with two type parameters K and V correctly accepts String and Integer. The display method prints the key-value pair.

Take Test
Q.244 Hard Generics
Consider this code:
public T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound ?
A Ensures type safety by guaranteeing T implements Comparable with itself
B Allows T to be compared with any type
C Improves runtime performance
D Eliminates the need for type erasure
Correct Answer:  A. Ensures type safety by guaranteeing T implements Comparable with itself
EXPLANATION

Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.

Take Test
Q.245 Hard Generics
What will happen with this code?
List list = new ArrayList();
list.add(123); // Adding Integer
String s = (String) list.get(0);
A ClassCastException at runtime
B Compilation error
C Code executes successfully
D Unchecked warning at compile time only
Correct Answer:  A. ClassCastException at runtime
EXPLANATION

Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.

Take Test
Q.246 Hard Generics
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
A public void process(List
B public void process(List
C public void process(List source, List dest) { }
D public void process(List source, List dest) { }
Correct Answer:  A. public void process(List
EXPLANATION

PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.

Take Test
Q.247 Medium Generics
What will be the output?
List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

The Integer value is stored in the list. At runtime, type erasure converts it to Object, but the actual stored value remains an Integer instance.

Take Test
Q.248 Medium Generics
Consider this generic interface:
interface Comparable { int compareTo(T o); }
Which class definition correctly implements this?
A class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
B class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
C class MyClass implements Comparable { public int compareTo(Object o) { return 0; } }
D class MyClass implements Comparable { public int compareTo(T o) { return 0; } }
Correct Answer:  A. class MyClass implements Comparable { public int compareTo(MyClass o) { return 0; } }
EXPLANATION

Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.

Take Test
Q.249 Medium Generics
What is the difference between List and List?
A List is read-only while List allows adding any object type
B List is read-only while List allows adding any type
C Both are equivalent
D List uses type erasure while List does not
Correct Answer:  A. List is read-only while List allows adding any object type
EXPLANATION

List<?> accepts any type but is read-only (can't add). List<Object> explicitly accepts Object type and allows adding objects.

Take Test
Q.250 Medium Generics
Which of the following represents a bounded type parameter?
A class Box { }
B class Box { }
C class Box
D class Box { }
Correct Answer:  A. class Box { }
EXPLANATION

Bounded type parameters restrict the types that can be used as arguments. T extends Number ensures T is Number or its subclass.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips