Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

212 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 212
Topics in Java Programming
Q.31 Hard Generics
Which Java feature was introduced specifically to support generics compilation?
A Bridge methods
B Reflection API
C Annotations
D Lambda expressions
Correct Answer:  A. Bridge methods
EXPLANATION

Bridge methods are synthetic methods generated during compilation to maintain polymorphism with generic types and type erasure.

Test
Q.32 Hard Generics
What output will this produce?
java
List
A Compiles and runs successfully, prints 5
B Compilation error
C Runtime ClassCastException
D Returns null
Correct Answer:  C. Runtime ClassCastException
EXPLANATION

With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.

Test
Q.33 Hard Generics
Which statement about generic array creation is correct?
A new ArrayList[10] is valid
B new List[10] is valid but produces warning
C Generic arrays cannot be created directly; use new ArrayList() instead
D Generic arrays are valid in Java 8+
Correct Answer:  C. Generic arrays cannot be created directly; use new ArrayList() instead
EXPLANATION

You cannot create arrays of generic types due to type erasure and heap pollution risks. Use Collection framework instead of arrays.

Test
Q.34 Hard Generics
What is the compiled bytecode signature of this generic method?
public T findMin(T[] arr)
A public Comparable findMin(Comparable[] arr)
B public Object findMin(Object[] arr)
C public Comparable findMin(Comparable[] arr)
D public Comparable findMin(Comparable[] arr) with bridge method
Correct Answer:  A. public Comparable findMin(Comparable[] arr)
EXPLANATION

Type erasure replaces T with its bound Comparable. The method signature becomes public Comparable findMin(Comparable[] arr).

Test
Q.35 Hard Generics
What is the difference between List and List in practical usage?
A They are identical
B List can accept any type; List cannot accept anything
C List can add Objects; List can only add null
D List is deprecated in favor of List
Correct Answer:  C. List can add Objects; List can only add null
EXPLANATION

List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).

Test
Q.36 Hard Generics
Consider this declaration: public static List createList(). Which statement about type inference is correct?
A Compiler always knows the type T
B Type T is inferred from context where method is called
C Type T must always be explicitly specified
D Compiler defaults T to Object
Correct Answer:  B. Type T is inferred from context where method is called
EXPLANATION

Java's type inference deduces T from the assignment context. E.g., 'List<String> list = createList()' infers T as String.

Test
Q.37 Hard Generics
What will happen when you compile and run this code?
java
List strings = Arrays.asList("a", "b", "c");
List raw = strings;
raw.add(123);
String s = strings.get(3);
A Compiles successfully and runs without error
B Compilation error due to raw type usage
C Compiles with warning, throws ClassCastException at runtime
D Compiles and throws ArrayIndexOutOfBoundsException
Correct Answer:  C. Compiles with warning, throws ClassCastException at runtime
EXPLANATION

Assigning generic type to raw type suppresses type checking. The Integer added bypasses the type system, causing ClassCastException when retrieved as String.

Test
Q.38 Hard Generics
How would you correctly use generics in a recursive type bound scenario?
A class Node { Node next; }
B class Node { Node next; }
C class Node extends T { }
D class Node { }
Correct Answer:  A. class Node { Node next; }
EXPLANATION

Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.

Test
Q.39 Hard Generics
Which of the following represents valid bounded wildcard usage for a method that processes collections?
A public void process(Collection
B public void process(Collection
C public void process(Collection col) { col.add(5); }
D public void process(Collection col) { col.add(5); }
Correct Answer:  B. public void process(Collection
EXPLANATION

Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.

Test
Q.40 Hard Generics
What is the compiled signature of this generic method?
public void sort(T[] array)
A public void sort(Number[] array)
B public void sort(Comparable[] array)
C public void sort(Object[] array)
D Type erasure uses the first bound: public void sort(Number[] array)
Correct Answer:  D. Type erasure uses the first bound: public void sort(Number[] array)
EXPLANATION

With multiple bounds using '&', type erasure replaces T with the first bound. Here, Number is the first bound, so T is replaced with Number.

Test
IGET
IGET AI
Online · Exam prep assistant
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