Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 161–170 of 958
Topics in Java Programming
Q.161 Easy Generics
When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
A
B
C
D
Correct Answer:  A.
EXPLANATION

Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.

Take Test
Q.162 Medium Generics
A utility class needs a method that accepts a List containing elements that are instances of a specific class or its subclasses. Which wildcard notation should be used?
A public static void process(List
B public static void process(List list)
C public static void process(List
D public static void process(List list)
Correct Answer:  A. public static void process(List
EXPLANATION

'? extends TargetClass' creates a covariant wildcard allowing lists of TargetClass or any subclass. This is ideal for reading operations when you need polymorphic list handling.

Take Test
Q.163 Easy Generics
In a Spring Boot application, you need to create a generic repository interface that works with any entity type. Which declaration correctly implements this pattern?
A public interface GenericRepository { T findById(Long id); void save(T entity); }
B public interface GenericRepository { Object findById(Long id); void save(Object entity); }
C public interface GenericRepository { T findById(Long id); void save(T entity); }
D public interface GenericRepository { T findById(K id); void save(T entity); }
Correct Answer:  A. public interface GenericRepository { T findById(Long id); void save(T entity); }
EXPLANATION

Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.

Take Test
Q.164 Medium Generics
A developer creates a generic method that accepts a collection of numbers and calculates their sum. Which type parameter declaration would be most appropriate for this scenario?
A public static double calculateSum(List numbers)
B public static double calculateSum(List numbers)
C public static double calculateSum(List numbers)
D public static double calculateSum(List numbers)
Correct Answer:  A. public static double calculateSum(List numbers)
EXPLANATION

Using 'T extends Number' establishes an upper bound, allowing the method to work with any Number subclass (Integer, Double, Float, etc.) while providing type safety and access to Number methods.

Take Test
Q.165 Medium Generics
What is the runtime class object for this generic variable?
java
List list = new ArrayList();
Class c = list.getClass();
A java.util.List
B java.util.ArrayList
C java.util.ArrayList
D java.lang.Object
Correct Answer:  C. java.util.ArrayList
EXPLANATION

Due to type erasure, getClass() returns ArrayList without generic parameters. Generic information exists only at compile time.

Take Test
Q.166 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.

Take Test
Q.167 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.

Take Test
Q.168 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.

Take Test
Q.169 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).

Take Test
Q.170 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).

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