Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 171–180 of 958
Topics in Java Programming
Q.171 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.

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

Take Test
Q.173 Medium Generics
Which of these is a valid generic interface implementation?
A class MyClass implements Comparable
B class MyClass implements Comparable
C class MyClass extends Comparable
D class MyClass implements Comparable
Correct Answer:  A. class MyClass implements Comparable
EXPLANATION

Implementing generic interfaces requires specifying the type parameter. 'implements Comparable<MyClass>' is the proper way to implement.

Take Test
Q.174 Medium Generics
What is the PECS principle in Java Generics?
A Public Extends Collections Structure
B Producer Extends, Consumer Super
C Parameter Extends Comparable Supports
D Polymorphic Extends Class Super
Correct Answer:  B. Producer Extends, Consumer Super
EXPLANATION

PECS (Producer Extends, Consumer Super) is a guideline: use 'extends' for producers (reading) and 'super' for consumers (writing).

Take Test
Q.175 Medium Generics
What does 'covariance' mean in the context of Java Generics?
A Types vary together in the same direction
B Types vary in opposite directions
C Types do not vary at all
D Types are interchangeable
Correct Answer:  A. Types vary together in the same direction
EXPLANATION

Covariance means if Dog is a subtype of Animal, then Producer<Dog> is a subtype of Producer<Animal>. Achieved using upper-bounded wildcards.

Take Test
Q.176 Medium Generics
Which wildcard usage follows the Producer pattern?
A List
B List
C List
D List
Correct Answer:  B. List
EXPLANATION

Producer (Get-Only pattern) uses upper-bounded wildcard (? extends T). It's used when you want to read/produce elements of specific types.

Take Test
Q.177 Medium Generics
What is the correct output of this code?
java
List
A Compilation successful
B Compilation error
C Runtime exception
D Adds 5 to the list
Correct Answer:  B. Compilation error
EXPLANATION

With upper-bounded wildcard (? extends Number), you can read but cannot add elements (except null) because the compiler doesn't know the exact type.

Take Test
Q.178 Medium Generics
Consider the code:
java
List list = new ArrayList();

Will this compile?
A Yes, Integer is a subtype of Number
B No, ArrayList is not compatible with List
C Yes, with warning
D Depends on compiler version
Correct Answer:  B. No, ArrayList is not compatible with List
EXPLANATION

Generics are invariant. ArrayList<Integer> cannot be assigned to List<Number> even though Integer is a subtype of Number. This prevents type safety issues.

Take Test
Q.179 Medium Generics
What does the following generic declaration mean?
public T findMax(T a, T b)
A T must be Number or Comparable
B T must extend Number and implement Comparable
C T must be Number and a subclass of Comparable
D T can be either Number or Comparable
Correct Answer:  B. T must extend Number and implement Comparable
EXPLANATION

Multiple bounds in generics use '&' to specify that the type must satisfy all constraints. T must extend Number AND implement Comparable<T>.

Take Test
Q.180 Easy Generics
Which declaration is valid in Java Generics?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List
Correct Answer:  B. List list = new ArrayList();
EXPLANATION

Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.

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