Java Generics Quiz - MCQ - Multiple Choice Questions

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);
    }
}
A. [20, 20]

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

You can modify a list typed as List<?>, but when you call m2(), inside that method, the list has a different type, List<T>, which allows you to change the value of the second element.

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);
    }
}
A. 1.0

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


<T extends Number> means that you can assign a Number or one of its subclasses to the generic type. This applies to both, the type argument of the class Question and to the variable t.

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

Option C doesn't compile because you can only assign a list of type Object to l3, the lower-bounded wildcard doesn't allow you to assign a subclass (like String).

4. Given

List<? super Number> list = new ArrayList<Object>(); // 1
list.add(new Integer(2)); // 2
list.add(new Object()); // 3
Which line will generate a compile-time error?

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

<? super Number> allows you to assign a List of type Number or its superclass, which is done in line // 1.

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 needs to be changed to List<Integer>.


Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. Java Multithreading Quiz
  11. JDBC Quiz
  12. Java Lambda Expressions Quiz
  13. Java Functional Interfaces Quiz
  14. Java Streams API Quiz
  15. Java Date Time Quiz
  16. Java 8 Quiz

Comments