Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 101–110 of 476
Topics in Java Programming
Q.101 Medium Generics
What does the following generic method signature indicate?
public static T findMax(T[] array)
A T must implement Comparable interface parameterized with itself
B T must be a Comparable type but not necessarily of itself
C Comparable is optional for T
D T is comparable only with Object types
Correct Answer:  A. T must implement Comparable interface parameterized with itself
EXPLANATION

The bounded type parameter 'T extends Comparable<T>' enforces that T must implement Comparable and be comparable with its own type. This is the F-bounded polymorphism pattern.

Take Test
Q.102 Medium Generics
Which of the following will cause a compile-time error?
A List list = new ArrayList();
B List list = new ArrayList();
C List
D List list = new ArrayList();
Correct Answer:  B. List list = new ArrayList();
EXPLANATION

Java does not allow covariance with generics. List<Integer> cannot be assigned to List<Number> because you could add a Double to the latter, breaking type safety.

Take Test
Q.103 Medium Generics
Consider the following code snippet:
public T processData(List
A T must be a subtype of the elements in the list
B The list can contain elements that are subtypes of T
C T and the wildcard bound are independent
D The list must contain exactly type T
Correct Answer:  B. The list can contain elements that are subtypes of T
EXPLANATION

The wildcard '? extends T' means the list can contain any type that is a subtype of T. This allows reading from the list safely but restricts writing.

Take Test
Q.104 Medium Generics
How would you define a generic method that works with a pair of objects and returns the first one?
A T getFirst(T first, U second) { return first; }
B T getFirst(T first, T second) { return first; }
C public T getFirst(T first, U second) { return first; }
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.

Take Test
Q.105 Medium Generics
What is the output?

List strings = new ArrayList();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);
A 123
B Compilation error
C Runtime ClassCastException
D null
Correct Answer:  C. Runtime ClassCastException
EXPLANATION

Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.

Take Test
Q.106 Medium Generics
What happens when you use raw type List instead of List?
A Type safety is lost and unchecked warnings appear
B All elements are treated as Object
C Runtime ClassCastException may occur
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.

Take Test
Q.107 Medium Generics
Consider a generic interface: interface Comparable { int compareTo(T obj); }. How should a class implement this for String comparison?
A class MyClass implements Comparable { }
B class MyClass implements Comparable { }
C class MyClass implements Comparable { }
D Both B and C are valid
Correct Answer:  D. Both B and C are valid
EXPLANATION

Option B directly specifies String, while Option C makes the class generic. Both are valid approaches to implement a generic interface.

Take Test
Q.108 Medium Generics
Which of the following will NOT compile?
A List numbers = new ArrayList();
B List
C List
D List objects = new ArrayList();
Correct Answer:  D. List objects = new ArrayList();
EXPLANATION

Option D fails because List<Object> cannot reference ArrayList<String>. Generics are invariant. Options A, B, C are valid (covariance with extends, contravariance with super).

Take Test
Q.109 Medium Generics
What does List represent in Java generics?
A List of unknown type
B List of any type
C List that can hold any single unknown type
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

List<?> is an unbounded wildcard that represents a list of unknown type. You can read from it (get Object), but cannot write to it (except null). Both A, B, and C are correct interpretations.

Take Test
Q.110 Medium Generics
Consider: public static T findMax(T a, T b) { return a; }. What is the issue with calling findMax(5, 3.5)?
A T cannot be inferred because int and double are different
B The method will compile and run without issues
C T will be Object type
D Runtime exception will occur
Correct Answer:  A. T cannot be inferred because int and double are different
EXPLANATION

The type parameter T cannot simultaneously be int and double. The compiler cannot infer a single type T that satisfies both arguments, resulting in a compilation error.

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