This post contains a few useful Java Generics multiple-choice questions to self-test your Java Generics concepts knowledge.
The answer and explanation have been given for each MCQ.
1. What is the output of the following program?
public class Question {
public static void main(String[] args) {
Question q = new Question();
List<Integer> l = new ArrayList<>();
l.add(20);
l.add(30);
q.m1(l);
}
private void m1(List<?> l) {
m2(l); // 1
}
private <T> void m2(List<T> l) {
l.set(1, l.get(0)); // 2
System.out.println(l);
}
}
B. Compilation fails on the line marked as // 1
C. Compilation fails on the line marked as // 2
D. An exception occurs at runtime
Answer
The correct answer is A.
Explanation
2. What is the output of the following program?
public class Question <T extends Number> {
T t;
public static void main(String[] args) {
Question q =
new Question<Integer>(); // 1
q.t = new Float(1); // 2
System.out.println(q.t);
}
}
B. Compilation fails on the line marked as // 1
C. Compilation fails on the line marked as // 2
D. An exception occurs at runtime
Answer
The correct answer is A.
Explanation
3. Which of the following declarations don't compile?
A.
List<?> l1 = new ArrayList<>();
B.
List<String> l2 = new ArrayList();
C.
List<? super Object> l3 = new ArrayList<String>();
D.
List<? extends Object> l4 = new ArrayList<String>();
Answer
The correct answer is C.
Explanation
4. Given
List<? super Number> list = new ArrayList<Object>(); // 1
list.add(new Integer(2)); // 2
list.add(new Object()); // 3
A. Line marked as // 1
B. Line marked as // 2
C. Line marked as // 3
D. No compile-time error is generated
Answer
The correct answer is C.
Explanation
The list can contain instances of Number or one of its subclasses. Since line // 3 tries to add an instance of the superclass of Number (Object), this line generates a compile-time error.
5. Determine the behavior of this program:
import java.io.*;
class LastError < T > {
private T lastError;
void setError(T t) {
lastError = t;
System.out.println("LastError: setError");
}
}
class StrLastError < S extends CharSequence > extends LastError < String > {
public StrLastError(S s) {}
void setError(S s) {
System.out.println("StrLastError: setError");
}
}
class Test {
public static void main(String[] args) {
StrLastError < String > err = new StrLastError < String > ("Error");
err.setError("Last error");
}
}
A. It prints the following: StrLastError: setError
B. It prints the following: LastError: setError
C. It results in a compilation error
D. It results in a runtime exception
Answer
C. It results in a compilation error
Explanation
It looks like the setError() method in StrLastError is overriding setError()
in the LastError class. However, it is not the case. At the time of compilation, the
knowledge of type S is not available. Therefore, the compiler records the signatures of
these two methods as setError(String) in superclass and setError(S_extends_
CharSequence) in subclass—treating them as overloaded methods (not overridden).
In
this case, when the call to setError() is found, the compiler finds both the overloaded
methods matching, resulting in the ambiguous method call error.
6. Choose the correct option based on this program:
import java.util.*;
class UtilitiesTest {
public static void main(String[] args) {
List < int > intList = new ArrayList < > ();
intList.add(10);
intList.add(20);
System.out.println("The list is: " + intList);
A. It prints the following: The list is: [10, 20]
B. It prints the following: The list is: [20, 10]
C. It results in a compiler error
D. It results in a runtime exception
Answer
C. It results in a compiler error
Explanation
You cannot specify primitive types along with generics, so List
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- Java Multithreading Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Comments
Post a Comment