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