Java Generics Test Questions

Java Generics Quiz Part – 2 | In Java Generics feature was introduced in Java 5 version to provide the type for method’s parameter and return type dynamically by achieving compile-time type checking for solving ClassCastException. Here we will see different Java generics test Questions.

Also See:- Java Generics Quiz Part – 1, Java Generics Quiz Part – 3

Q1) We can call m1() method by passing?

m1(ArrayList<String> al) { }

a) ArrayList object of only String type.
b) ArrayList object of any type.
c) Only null value.
d) None of these

View Answer Answer:- a) ArrayList object of only String type.

We can call m1() method by passing the ArrayList object of the String type, because the parameter of m1() is the ArrayList of the String type.

Q2) Which of the following option can’t be used at PLACE-1? Using that option may lead to compile-time error or Exception?

import java.util.*;
public class Test {
   public static void main(String[] args) {
      ArrayList<String> al = new ArrayList<String>();
      m1(al);
      System.out.println(al);
   }
   public static void m1(ArrayList<String> al)  {
      // PLACE-1
   }
}

a) al.add(“A”);
b) al.add(null);
c) al.add(10);
b) None of these

View Answer Answer:- c) al.add(10);

The parameter of the m1() method is ArrayList of String type hence it can be called only by passing the ArrayList object of String type. Hence in the m1() method except for String, we can’t add any other value to the ArrayList.

Q3) We can call m1() method by passing?

m1(ArrayList<?> al) { }

a) ArrayList object of only String type.
b) ArrayList object of any type.
c) ArrayList object of only Number and its child class type.
d) Invalid Syntax; Compile-time error

View Answer Answer:- b) ArrayList object of any type.

We can call m1() method by passing the ArrayList object of any type, because the parameter of m1() is ArrayList. Here “?” is a wild character.

Q4) Which of the following option can be used at PLACE-1, it won’t raise compile-time error or Exception?

import java.util.*;
public class Test {
   public static void m1(ArrayList<?> al)  {
      // PLACE-1
   }
}

a) al.add(“A”);
b) al.add(null);
c) al.add(10);
b) None of these

View Answer Answer:- b) al.add(null);

The parameter of m1() method is ArrayList hence it can be called by passing ArrayList object of any type. The option (a) al.add(“A”); is valid only when m1(ArrayList al), the option (c) al.add(10); is valid for m1(ArrayList al). But (b) al.add(null); is valid for any type. The “null” is the default value of all objects.

Q5) Which of the following options are valid for the m1() method?

m1(ArrayList<? extends X> al)

a) X can be a class or interface.
b) If X is a class then we can call m1() by passing the ArrayList object of X or its child class type.
c) If X is an interface then we can call m1() by passing the ArrayList object of X or its implementation class type.
d) All of these are valid.

View Answer Answer:- d) All of these are valid.

Q6) Which of the following option can be used at PLACE-1, it won’t raise compile-time error or Exception?

import java.util.*;
public class Test {
   public static void m1(ArrayList<? extends Number> al)  {
      // PLACE-1
   }
}

a) al.add(“A”);
b) al.add(null);
c) al.add(10);
b) None of these

View Answer Answer:- b) al.add(null);

Within this method, we can’t add anything except “null” because we don’t know the type exactly.

Q7) We can call m1() method by passing?

public static void m1(ArrayList<? super Number> al)

a) ArrayList object of only Number type.
b) ArrayList object of any type.
c) ArrayList object of Number, Integer, or Double class type.
d) ArrayList object of Number or Object class type.

View Answer
Answer:- d) ArrayList object of Number or Object class type.

In the syntax:- m1(ArrayList al), X can be either class or interface. If X is a class then we can call this method by passing the ArrayList object of either X type or its superclass type.

ArrayList<Number> al1 = new ArrayList<Number>();
m1(al1); // valid

ArrayList<Object> al2 = new ArrayList<Object>();
m1(al2); // valid

ArrayList<Integer> al = new ArrayList<Integer>();
m1(al); /* invalid; incompatible types: ArrayList<Integer>
* cannot be converted to ArrayList<? super Number> */


The java.lang.Object is the superclass of Number therefore we can call m1() method by passing ArrayList of the Object type.

Q8) Which option is invalid?

public static void m1(ArrayList<? super Runnable> al)

a) m1(new ArrayList<Runnable>());
b) m1(new ArrayList<Thread>());
c) m1(new ArrayList<Object>());
d) All are valid

View Answer Answer:- b) m1(new ArrayList<Thread>());

In the syntax:- m1(ArrayList al), If X is an interface then we can call this method by passing the ArrayList object of either X type or superclass of the implementation class of X.

Option (a) is valid because we are passing the ArrayList object of Runnable type. Option (c) is valid because the Object class is the super of the Thread class, and we can pass the superclass of the implementation class of X. Option (b) is invalid because we can’t pass the implementation class of X.

Q9) Which of the following option can’t be used at PLACE-1, because it may lead to compile-time error or Exception?

class Animal { }
class Dog extends Animal { }
import java.util.*;
public class Test {
   public static void main(String[] args) {
      ArrayList<Animal> al = new ArrayList<Animal>();
      m1(al);
   }
   public static void m1(ArrayList<? super Animal> al)  {
      // PLACE-1
   }
}

a) al.add(new Animal());
b) al.add(new Dog());
c) al.add(null);
d) al.add(new Object());

View Answer Answer:- d) al.add(new Object());

Assume we have a method:- m1(ArrayList al), Now, within this method can add either X type of object and “null” to the list. But we can’t add the ArrayList object of its child/implementation class type.

Q10) Which of the following is invalid?

a) ArrayList<String> al1 = new ArrayList<String>();
b) ArrayList<?> al2 = new ArrayList<String>();
c) ArrayList<?> al3 = new ArrayList<Integer>();
d) All are valid.

View Answer Answer:- d) All are valid.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *