MCQs on Java Generics | Java Type Safety

Explore key concepts of Java Generics and type safety with this comprehensive set of 30 MCQs. Designed for Java developers, this collection covers Generics, Wildcards, Bounds, Generic Classes, Interfaces, and Type Parameters in Methods to ensure strong knowledge in Java type safety principles.


MCQs

1-10: Introduction to Generics

  1. What is the primary benefit of using Generics in Java?
    • A) Code reusability
    • B) Increased memory consumption
    • C) Faster execution speed
    • D) Support for multiple inheritance
  2. In Java, which of the following represents a generic class?
    • A) public class Box<T> {}
    • B) public class Box() {}
    • C) public class Box[] {}
    • D) public class Box{}
  3. Which statement is true about Java Generics?
    • A) Generics enforce type safety at compile-time
    • B) Generics enforce type safety at runtime
    • C) Generics do not provide type safety
    • D) Generics are not supported in Java
  4. In which Java version were Generics introduced?
    • A) Java 5
    • B) Java 6
    • C) Java 7
    • D) Java 8
  5. Which of the following is a valid declaration of a generic method?
    • A) public <T> void print(T obj) {}
    • B) public <void> print(T obj) {}
    • C) public void <T> print(T obj) {}
    • D) public void print<T>(T obj) {}
  6. What does the <T> signify in a generic class or method?
    • A) A specific type
    • B) A wildcard
    • C) A placeholder for a type
    • D) A specific value
  7. What is the default type of a generic type parameter in Java?
    • A) Object
    • B) Integer
    • C) String
    • D) Void
  8. Which of the following is a benefit of type-safe collections in Java Generics?
    • A) Reduces runtime errors
    • B) Increases runtime overhead
    • C) Provides backward compatibility
    • D) Allows inheritance
  9. Can you use primitive types like int, char, etc., with Java Generics?
    • A) Yes, directly
    • B) No, only wrappers like Integer, Character, etc.
    • C) Yes, but only in methods
    • D) No, Generics do not support primitives
  10. What is the correct syntax for creating a generic list in Java?
  • A) List<int> list = new ArrayList<>();
  • B) List<Integer> list = new ArrayList<>();
  • C) List[] list = new ArrayList<>();
  • D) ArrayList<List> list = new List<>();

11-15: Wildcards and Bounds

  1. What does the wildcard ? extends T mean in Java Generics?
  • A) It matches any class that is a subclass of T
  • B) It can hold any type of object
  • C) It matches any object type of T
  • D) It only accepts objects of type T
  1. What is the purpose of the wildcard ? super T in Java Generics?
  • A) It can hold any object that is a supertype of T
  • B) It can hold only T or subclasses of T
  • C) It can hold any object that is a subclass of T
  • D) It only accepts objects of type T
  1. Which of the following correctly demonstrates the use of wildcards in Generics?
  • A) public <T> void printList(List<? super T> list) {}
  • B) public <T> void printList(List<? extends T> list) {}
  • C) Both A and B
  • D) None of the above
  1. When should you use ? extends T wildcards in Generics?
  • A) When you want to add elements to a collection
  • B) When you want to ensure that elements are of type T or any subclass
  • C) When you want to restrict the type to only T
  • D) When you don’t want any constraints on the type
  1. What happens if you attempt to add an element to a collection with ? extends T wildcard?
  • A) Compilation error occurs
  • B) The element is added successfully
  • C) The element is not added, but it is accepted
  • D) The element is automatically type-cast

16-20: Generic Classes and Interfaces

  1. How do you define a generic interface in Java?
  • A) public interface Comparable<T> {}
  • B) public interface Comparable() {}
  • C) public interface Comparable<T> extends T {}
  • D) public interface Comparable<T> implements T {}
  1. Which of the following is true about Java Generic classes?
  • A) Generic classes can only have one type parameter
  • B) Generic classes can have multiple type parameters
  • C) Type parameters must be primitive types
  • D) Generic classes do not support inheritance
  1. What is the correct syntax for creating a generic class with multiple type parameters?
  • A) public class Box<T, E> {}
  • B) public class Box<T, E> {}
  • C) public class Box<T> <E> {}
  • D) public class Box<T, E> implements T, E {}
  1. What is the advantage of using a generic class?
  • A) It allows you to define type-safe classes
  • B) It increases the complexity of the code
  • C) It improves performance at runtime
  • D) It makes your code more flexible without type constraints
  1. Which of the following is a limitation of Java Generics?
  • A) Cannot instantiate generic types directly
  • B) Only classes can be generic, not methods
  • C) Generics increase runtime overhead
  • D) Generics cannot be used with interfaces

21-30: Type Parameters in Methods

  1. What is the main role of type parameters in methods?
  • A) To allow a method to accept different types of arguments
  • B) To allow methods to return multiple values
  • C) To allow a method to perform operations on primitive types
  • D) To define the method return type
  1. Which of the following is a valid generic method declaration in Java?
  • A) public <T> T method(T param) {}
  • B) public <T> void method<T>(T param) {}
  • C) public void <T> method(T param) {}
  • D) public <T> T method(T param) {}
  1. What type of parameter does the method public <T> void print(T param) accept?
  • A) Only objects of type T
  • B) Any type of object
  • C) Only String objects
  • D) Primitive types like int and char
  1. Which of the following is a correct way to call a generic method in Java?
  • A) method<String>("hello");
  • B) method("hello");
  • C) method<String>("hello");
  • D) method<String>(new String("hello"));
  1. What is the use of the <T> before the method name in a generic method?
  • A) To define the method’s return type
  • B) To specify the type of parameters the method accepts
  • C) To specify the class type
  • D) To allow the method to return a generic class
  1. Which of the following is true about generic method calls in Java?
  • A) You need to specify the type explicitly in method calls
  • B) Type inference is used to determine the type of the argument
  • C) Only primitive types can be used
  • D) You can’t call a generic method without providing a type
  1. In a generic method, what happens if you pass an argument of the wrong type?
  • A) The program compiles successfully
  • B) The method throws a runtime exception
  • C) A compile-time error occurs
  • D) The argument is automatically type-cast
  1. Which method signature demonstrates the use of multiple type parameters in a generic method?
  • A) public <T, E> void print(T t, E e) {}
  • B) public <T> void print(T t, E e) {}
  • C) public void <T, E> print(T t, E e) {}
  • D) public <T> void print(T, E) {}
  1. Which of the following can you not do with Java Generics?
  • A) Use primitive types
  • B) Pass multiple parameters to a generic method
  • C) Use wildcards with generic classes
  • D) Use any class type as a parameter
  1. What is the best practice when using Generics in methods for maximum type safety?
  • A) Always use wildcards
  • B) Use bounded type parameters
  • C) Avoid using Generics altogether
  • D) Use only Object types

Answer Key

QnoAnswer
1A) Code reusability
2A) public class Box<T> {}
3A) Generics enforce type safety at compile-time
4A) Java 5
5A) public <T> void print(T obj) {}
6C) A placeholder for a type
7A) Object
8A) Reduces runtime errors
9B) No, only wrappers like Integer
10B) List<Integer> list = new ArrayList<>();
11A) It matches any class that is a subclass of T
12A) It can hold any object that is a supertype of T
13C) Both A and B
14B) When you want to ensure that elements are of type T or any subclass
15A) Compilation error occurs
16A) public interface Comparable<T> {}
17B) Generic classes can have multiple type parameters
18A) public class Box<T, E> {}
19A) It allows you to define type-safe classes
20A) Cannot instantiate generic types directly
21A) To allow a method to accept different types of arguments
22A) public <T> T method(T param) {}
23B) Any type of object
24A) method<String>("hello");
25B) To specify the type of parameters the method accepts
26B) Type inference is used to determine the type of the argument
27C) A compile-time error occurs
28A) public <T, E> void print(T t, E e) {}
29A) Use primitive types
30B) Use bounded type parameters

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top