Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 231–240 of 958
Topics in Java Programming
Q.231 Easy Generics
What is the output of this code snippet?

ArrayList list = new ArrayList();
list.add("Java");
Object obj = list.get(0);
String str = (String) obj;
System.out.println(str.length());
A 4
B Compilation error
C Runtime ClassCastException
D null
Correct Answer:  A. 4
EXPLANATION

The code compiles and runs correctly. 'Java' has 4 characters. The explicit cast is necessary because get() returns Object type.

Take Test
Q.232 Medium Generics
In a generic class 'class Box', what constraint is placed on T?
A T must implement the Comparable interface with itself as the type parameter
B T must be a subclass of Comparable
C T must be Comparable with any type
D T is unrestricted and Comparable is optional
Correct Answer:  A. T must implement the Comparable interface with itself as the type parameter
EXPLANATION

The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.

Take Test
Q.233 Easy Generics
Which of the following will compile successfully?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  B. List list = new ArrayList();
EXPLANATION

Generics only work with reference types. 'int' is a primitive type, so 'Integer' wrapper class must be used instead.

Take Test
Q.234 Medium Generics
What does the PECS principle stand for in generics context?
A Producer Extends, Consumer Super
B Parameter Extends, Class Super
C Polymorphic Extends, Constructor Super
D Public Extends, Static Super
Correct Answer:  A. Producer Extends, Consumer Super
EXPLANATION

PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.

Take Test
Q.235 Medium Generics
Which generic declaration would allow storing both String and Integer in the same collection?
A List list = new ArrayList();
B List list = new ArrayList();
C List
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.

Take Test
Q.236 Medium Generics
Which statement correctly uses the lower-bounded wildcard in generics?
A List
B List
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List
EXPLANATION

Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.

Take Test
Q.237 Medium Generics
In the declaration 'List
A Any Number subclass
B Only Number or its exact subclasses
C Nothing can be added safely (except null)
D Only Integer and Double
Correct Answer:  C. Nothing can be added safely (except null)
EXPLANATION

Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.

Take Test
Q.238 Easy Generics
What is the primary advantage of using generics in Java?
A Type safety and elimination of explicit casting
B Increased execution speed
C Reduced memory consumption
D Automatic parallel processing
Correct Answer:  A. Type safety and elimination of explicit casting
EXPLANATION

Generics provide compile-time type checking, preventing ClassCastException and eliminating the need for explicit casting at runtime.

Take Test
Q.239 Easy Generics
In Java generics, what does the wildcard '?' represent?
A An unknown type that can be any class
B A mandatory type parameter
C A primitive data type only
D A deprecated feature in Java 8+
Correct Answer:  A. An unknown type that can be any class
EXPLANATION

The wildcard '?' in generics represents an unknown type, allowing flexibility when the exact type is not known or not important.

Take Test
Q.240 Medium Generics
Which of these declarations would NOT cause a compilation warning or error?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List list = new ArrayList();
Correct Answer:  A. List list = new ArrayList();
EXPLANATION

Option A uses proper diamond syntax with matching type parameters on both sides. Options B, C produce unchecked warnings, and Option D is invalid due to invariance.

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