Java Programming
Java OOP, collections, multithreading
270 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 270 questions
Q.61 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
Q.62 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.

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

Take Test
Q.64 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.65 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
Advertisement
Q.66 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.67 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.68 Easy Generics
What is the output of this generic code?
class Pair {
public void display(K key, V value) {
System.out.println(key + ": " + value);
}
}
Pair p = new Pair();
p.display("Age", 25);
A Age: 25
B Compilation error - type mismatch
C Age: 25.0
D Runtime exception
Correct Answer:  A. Age: 25
EXPLANATION

The generic class Pair with two type parameters K and V correctly accepts String and Integer. The display method prints the key-value pair.

Take Test
Q.69 Easy Generics
Which of the following is a valid generic method declaration?
A public T getValue(T value) { return value; }
B public T getValue(T value) { return value; }
C public getValue(T value) { return value; }
D public getValue(T value) { return value; }
Correct Answer:  A. public T getValue(T value) { return value; }
EXPLANATION

The correct syntax for a generic method is: access_modifier <T> returnType methodName(T parameter). The type parameter <T> must come before the return type.

Take Test
Q.70 Easy Generics
What will be the output of the following code?
List list = new ArrayList();
list.add("Java");
System.out.println(list.get(0).length());
A 4
B 5
C Compilation error
D Runtime exception
Correct Answer:  A. 4
EXPLANATION

"Java" has 4 characters. The generic type ensures the element is a String, so .length() method is available without casting.

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