Java Generics Test Questions

Java Generics Quiz Part – 3 | 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 – 2

Q1) Which of the following is invalid?

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

View Answer Answer:- c) ArrayList<? extends Number> al3 = new ArrayList<String>();

Integer is child class of Number therefore (b) is valid. But String is not child class of Number therefore (c) is invalid.

Q2) Which of the following is invalid?

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

View Answer Answer:- c) ArrayList<? super Number> al3 = new ArrayList<Integer>();

Object is super class of Number therefore (b) option is valid. But Integer class is not the super class of Number therefore (c) option is invalid.

Q3) Which of the following is invalid?

a) ArrayList<?> al1 = new ArrayList<?>();
b) ArrayList<?> al2 = new ArrayList<? extends Number>();
c) Both (a) and (b) are valid.
d) Both (a) and (b) are invalid.

View Answer Answer:- d) Both (a) and (b) are invalid.

Wild character (?) can be used only on the declaration part (i.e. left side of the =). Otherwise, we will get a compile-time error:- class or interface without bounds.

Q4) Which of the following syntax is valid?

a) public <T>void m1(T ob) { }
b) public void<T> m1(T ob) { }
c) public <T>void<T> m1(T ob) { }
d) Either all are valid or all are invalid.

View Answer Answer:- a) public <T>void m1(T ob) { }

We can define bounded type even at the method level also. We have to declare the type parameter just before the return type.

Q5) Which of the following syntax is valid?

a) public <T extends Number & Comparable & Runnable>void m1() { }
b) public <T extends Runnable & Number>void m1() { }
c) public <T extends Number & Thread>void m1() { }
d) Either all are valid or all are invalid.

View Answer Answer:- a) public <T extends Number & Comparable & Runnable>void m1() { }

In (b) option class should be taken first before the interface therefore this option is invalid. We can’t extend more than one class therefore (c) option is invalid.

Q6) Find the output of the below program?

import java.util.ArrayList;
class Test {
   public static void main(String[] args) {
      ArrayList<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      m1(al);
      System.out.println(al);
   }

   public static void m1(ArrayList al) {
      al.add(10);
      al.add(10.5);
      al.add(true);
   }
}

a) [A, B]
b) [A, B, 10, 10.5, true]
c) Compile time error
d) Compile successfully, but Exception raised at Runtime

View Answer Answer:- b) [A, B, 10, 10.5, true]

Generic code can communicate with non-generic code. In the main method “al” is a generic type and only String values are allowed to insert into the ArrayList object. But in the m1() method “al” is the non-generic type and we can add any type of value.

Q7) Find the output of the below program?

import java.util.*;
class Test {
   public static void main(String[] args) {
      ArrayList<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      m1(al);
      Iterator<String> itr = al.iterator();
      while(itr.hasNext()) {
         String str = itr.next();
         System.out.print(str + " ");
      }
   }

   public static void m1(ArrayList al) {
      al.add(10);
      al.add(10.5);
      al.add(true);
   }
}

a) [A, B]
b) [A, B, 10, 10.5, true]
c) Compile time error
d) Compile successfully, but Exception raised at Runtime

View Answer Answer:- d) Compile successfully, but Exception raised at Runtime

There are no syntax mistakes, therefore, we won’t get a compile-time error. Before iterating the list, “al” contains [A, B, 10, 10.5, true]. While iterating every element is fetched and converted to String type. When it comes to 10, then Integer can’t be converted to String type. Hence exception is raised.

The output of this program is:-
A B Exception in thread “main” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader ‘bootstrap’) at Test.main(Test.java:10)

Q8) Which option is exactly the same as the below statement?

ArrayList al = new ArrayList();

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

View Answer Answer:- d) All are the same.

Q9) What will be the output of the below program?

import java.util.*;
class Test {
   public static void main(String[] args) {
      ArrayList al = new ArrayList<String>();
      al.add("A");
      al.add(10);
      al.add(10.5);
      al.add(true);
      System.out.println(al);
   }
}

a) [A]
b) [A, 10, 10.5, true]
c) Compile time error
d) Compile successfully, but Exception raised at Runtime

View Answer Answer:- b) [A, 10, 10.5, true]

Here, “al” is a non-generic version hence we can add any type of element.

Q10) Find the output of the below program?

import java.util.*;
class Test {

   public static void main(String[] args) {
      ArrayList<String> al = new ArrayList<String>();
      m1(al);
   }

   public static void m1(ArrayList<String> al) { 
      System.out.println("String");
   }

   public static void m1(ArrayList<Integer> al) {
      System.out.println("Integer");
   }
}

a) Compile time error
b) String
c) Integer
d) None of these

View Answer Answer:- a) Compile time error

Method overloading can’t be done through Generics. Here we will get a compile-time error:- name clash: m1(ArrayList) and m1(ArrayList) have the same erasure

public static void m1(ArrayList al) {
^
1 error

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do 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 *