Generics

1.What is the main advantage of using generics in Java?
  • A. Faster execution
  • B. Smaller bytecode
  • C. Compile-time type safety
  • D. Less memory usage

Compile-time type safety

Compile-time type safety

2.What will happen if you use a raw type with a generic class?
  • A. Compile-time error
  • B. Runtime error
  • C. Warning but compiles
  • D. JVM crash

Warning but compiles

Warning but compiles

3.What does <T> signify in a class or method declaration?
  • A. T is a temporary variable
  • B. T is a fixed type
  • C. T is a type parameter
  • D. T is a constructor

T is a type parameter

T is a type parameter

4.Which of the following is a correct generic method declaration?
  • A. public void <T> print(T data)
  • B. public <T> void print(data T)
  • C. public <T> print(T data)
  • D. public void print<T>(T data)

public void <T> print(T data)

public void <T> print(T data)

5.Which is a valid generic type usage?
  • A. List<int> list = new ArrayList<>();
  • B. List<String> list = new ArrayList<String>();
  • C. List<> list = new ArrayList<String>();
  • D. List list = new List<String>();

List<String> list = new ArrayList<String>();

List<String> list = new ArrayList<String>();