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.
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?
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.
Which of the following correctly declares a generic method that returns the first element of any collection?
Apublic T getFirst(Collection collection)
Bpublic T getFirst(Collection collection)
Cpublic T getFirst(Collection collection)
Dpublic 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.
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());
Atrue
Bfalse
CCompilation error
DRuntime 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.
What is the relationship between generics and arrays in Java?
AGeneric arrays can be created freely like List[]
BGeneric arrays are not supported; you cannot create arrays of parameterized types
CGeneric arrays work only with unbounded wildcards
DArrays 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.
BAny type, but with restricted capabilities - can only read as Object
CUnknown type that extends Object
DA 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.
What is the result of type erasure in Java generics?
AGeneric type information is preserved at runtime
BAll generic type information is removed at compile time, replaced with raw types or bounds
CGeneric types are converted to Object only
DType 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.
Which of the following is a valid generic class declaration in Java?
Apublic class Box { }
Bpublic class Box { }
Cpublic class Box { }
Dpublic 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.