Home Subjects Java Programming Generics

Java Programming
Generics

Java OOP, collections, multithreading

25 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 25
Topics in Java Programming
Q.11 Easy Generics
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
A Compile-time type checking prevents ClassCastException
B Enables code reuse without sacrificing type safety
C Improves API clarity and documentation
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.

Test
Q.12 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.

Test
Q.13 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.

Test
Q.14 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.

Test
Q.15 Easy Generics
Which of the following is a valid generic method declaration in Java?
A public static void display(T data) { }
B public static void display(T data) { }
C public static void display(T data) { }
D public static void display(T data) { }
Correct Answer:  A. public static void display(T data) { }
EXPLANATION

Type parameters in Java must be declared before the return type using angle brackets. Option A has correct syntax: <T> comes before return type void.

Test
Q.16 Easy Generics
In the interface declaration 'interface Pair', how many type parameters does it have?
A One
B Two
C Variable depending on implementation
D None, they are method parameters
Correct Answer:  B. Two
EXPLANATION

The interface has two type parameters: K and V, making it a generic interface that can be implemented with different type combinations.

Test
Q.17 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.

Test
Q.18 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.

Test
Q.19 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.

Test
Q.20 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.

Test
IGET
IGET AI
Online · Exam prep assistant
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