Java Programming
Java OOP, collections, multithreading
270 Questions 10 Topics Take Test
Advertisement
Showing 51–60 of 270 questions
Q.51 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.52 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
Q.53 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.54 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.55 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
Advertisement
Q.56 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.57 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
Q.58 Easy Generics
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
A Compile-time type checking prevents ClassCastException
B Enables code reuse without sacrificing type safety
C Improves API clarity and documentation
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.

Take Test
Q.59 Easy Generics
What is the output of the following code?

List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj.getClass().getName());
A java.lang.Integer
B java.lang.Object
C java.lang.Number
D Compilation error
Correct Answer:  A. java.lang.Integer
EXPLANATION

Although obj is declared as Object, it holds an Integer instance. getClass() returns the actual runtime type, which is Integer.

Take Test
Q.60 Easy Generics
What will be the result of executing: List list = new ArrayList();
A Compilation error
B Runtime error
C Compiles and runs successfully
D Warning at compile time only
Correct Answer:  A. Compilation error
EXPLANATION

This causes a compilation error due to type mismatch. List<Integer> cannot hold ArrayList<String>. Generics do not support implicit type conversion.

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