Java Programming — Generics
Java OOP, collections, multithreading
25 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 25 questions in Generics
Q.1 Easy Generics
When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
A
B
C
D
Correct Answer:  A.
EXPLANATION

Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.

Take Test
Q.2 Easy Generics
In a Spring Boot application, you need to create a generic repository interface that works with any entity type. Which declaration correctly implements this pattern?
A public interface GenericRepository { T findById(Long id); void save(T entity); }
B public interface GenericRepository { Object findById(Long id); void save(Object entity); }
C public interface GenericRepository { T findById(Long id); void save(T entity); }
D public interface GenericRepository { T findById(K id); void save(T entity); }
Correct Answer:  A. public interface GenericRepository { T findById(Long id); void save(T entity); }
EXPLANATION

Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.

Take Test
Q.3 Easy Generics
Which declaration is valid in Java Generics?
A List list = new ArrayList();
B List list = new ArrayList();
C List list = new ArrayList();
D List
Correct Answer:  B. List list = new ArrayList();
EXPLANATION

Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.

Take Test
Q.4 Easy Generics
Which of the following correctly declares a generic method that returns the first element of any collection?
A public T getFirst(Collection collection)
B public T getFirst(Collection collection)
C public T getFirst(Collection collection)
D public Collection getFirst(T collection)
Correct Answer:  B. public T getFirst(Collection collection)
EXPLANATION

Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.

Take Test
Q.5 Easy Generics
What will be the output of the following code?
java
List list = new ArrayList();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
A true
B false
C Compilation error
D Runtime exception
Correct Answer:  A. true
EXPLANATION

Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.

Take Test
Advertisement
Q.6 Easy Generics
What is the relationship between generics and arrays in Java?
A Generic arrays can be created freely like List[]
B Generic arrays are not supported; you cannot create arrays of parameterized types
C Generic arrays work only with unbounded wildcards
D Arrays of generics work only for primitive types
Correct Answer:  B. Generic arrays are not supported; you cannot create arrays of parameterized types
EXPLANATION

Java does not allow creation of arrays with parameterized types due to type erasure. List<String>[] would be unsafe. You must use List<String>[] or List[] instead.

Take Test
Q.7 Easy Generics
What does the wildcard in List represent?
A Any type, with full read and write capabilities
B Any type, but with restricted capabilities - can only read as Object
C Unknown type that extends Object
D A placeholder for generic type parameter
Correct Answer:  B. Any type, but with restricted capabilities - can only read as Object
EXPLANATION

List<?> represents unknown type with read-only capabilities. Elements can only be read as Object. You cannot add elements (except null) due to type safety.

Take Test
Q.8 Easy Generics
What is the compiler-inferred type parameter in this call?
List list = Arrays.asList("a", "b", "c");
A T is inferred as Object
B T is inferred as String
C T is inferred as ? extends String
D T is inferred as ? super String
Correct Answer:  B. T is inferred as String
EXPLANATION

The compiler infers the most specific common type of the arguments. Since all arguments are String literals, T is inferred as String.

Take Test
Q.9 Easy Generics
What is the result of type erasure in Java generics?
A Generic type information is preserved at runtime
B All generic type information is removed at compile time, replaced with raw types or bounds
C Generic types are converted to Object only
D Type erasure only applies to wildcards
Correct Answer:  B. All generic type information is removed at compile time, replaced with raw types or bounds
EXPLANATION

Type erasure removes all generic type parameters during compilation. For unbounded types, they're replaced with Object; for bounded types, they're replaced with the upper bound.

Take Test
Q.10 Easy Generics
Which of the following is a valid generic class declaration in Java?
A public class Box { }
B public class Box { }
C public class Box { }
D public class Box { }
Correct Answer:  A. public class Box { }
EXPLANATION

Valid generic class declarations allow multiple type parameters with upper bounds. Option A is syntactically correct. Option C uses invalid 'super' keyword in class declaration. Option D has malformed syntax.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips