Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 211–220 of 958
Topics in Java Programming
Q.211 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.212 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.213 Easy Generics
What is the output of the following code?

List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj.getClass().getName());
A java.lang.Integer
B java.lang.Object
C java.lang.Number
D Compilation error
Correct Answer:  A. java.lang.Integer
EXPLANATION

Although obj is declared as Object, it holds an Integer instance. getClass() returns the actual runtime type, which is Integer.

Take Test
Q.214 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.215 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.216 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
Q.217 Medium Generics
Which statement correctly defines a generic class with multiple type parameters and bounds?
A class Container { }
B class Container extends Number { }
C class Container { }
D class Container extends { }
Correct Answer:  C. class Container { }
EXPLANATION

Multiple type parameters with bounds must be comma-separated and each can have its own upper bound. Option C shows correct syntax with recursive bound on T and bound on K.

Take Test
Q.218 Easy Generics
What will be the result of executing: List list = new ArrayList();
A Compilation error
B Runtime error
C Compiles and runs successfully
D Warning at compile time only
Correct Answer:  A. Compilation error
EXPLANATION

This causes a compilation error due to type mismatch. List<Integer> cannot hold ArrayList<String>. Generics do not support implicit type conversion.

Take Test
Q.219 Medium Generics
Consider the declaration: List
A List of Integer and its subclasses
B List of Integer and its superclasses
C List of any type
D List of Number and Integer only
Correct Answer:  B. List of Integer and its superclasses
EXPLANATION

'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.

Take Test
Q.220 Easy Generics
What is the primary purpose of bounded type parameters in generics?
A To restrict type arguments to specific types or their subtypes
B To improve code readability only
C To increase runtime performance
D To create multiple inheritance in Java
Correct Answer:  A. To restrict type arguments to specific types or their subtypes
EXPLANATION

Bounded type parameters like <T extends Number> restrict the types that can be passed as type arguments, ensuring type safety and enabling access to specific methods.

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