Java Generics Interview Questions

    Question 1JAVA - Generics Basics

    What is the main purpose of Generics in Java?

    Question 2JAVA - Generic Class

    Which of the following correctly defines a generic class?

    Question 3JAVA - Generic Method

    Which syntax is correct for a generic method?

    Question 4JAVA - Type Safety Example

    What will happen in this code?

    List list = new ArrayList(); list.add("Hello"); list.add(10); String s = (String) list.get(1); System.out.println(s);

    Question 5JAVA - Wildcard ?

    What does the wildcard ? represent in generics?

    Question 6JAVA - Upper Bounded Wildcard

    Which declaration is correct for a list of numbers (Integer, Double, etc.)?

    Question 7JAVA - Lower Bounded Wildcard

    Which syntax is used for a generic type that can accept Integer or its supertypes?

    Question 8JAVA - Generic Example Code

    What will this code print?

    class Box<T> { T value; Box(T value) { this.value = value; } T get() { return value; } } public class Test { public static void main(String[] args) { Box<String> b = new Box<>("Java"); System.out.println(b.get()); } }

    Question 9JAVA - Type Erasure

    How does Java implement generics internally?

    Question 10JAVA - Multiple Type Parameters

    Which of the following correctly declares a generic class with two type parameters?