Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 251–260 of 958
Topics in Java Programming
Q.251 Medium Generics
Consider the code:
List
A No, compilation error - cannot add to ? extends
B Yes, it compiles successfully
C Only if Integer is used
D Only at runtime
Correct Answer:  A. No, compilation error - cannot add to ? extends
EXPLANATION

Upper bounded wildcards (? extends Number) are read-only. You cannot add elements because the compiler doesn't know the exact subtype.

Take Test
Q.252 Medium Generics
What will happen when you try to create an array of generics?
List[] array = new List[10];
A Compilation error
B Runtime exception
C Code executes successfully
D Creates an unchecked warning only
Correct Answer:  A. Compilation error
EXPLANATION

You cannot create arrays of generic types because of type erasure. The compiler prevents this with a compilation error.

Take Test
Q.253 Medium Generics
Which statement about wildcard generics (?) is correct?
A ? represents an unknown type and can be used with upper bounds (? extends T) or lower bounds (? super T)
B ? can only be used with extends keyword
C ? is used to create generic classes
D ? eliminates the need for type parameters
Correct Answer:  A. ? represents an unknown type and can be used with upper bounds (? extends T) or lower bounds (? super T)
EXPLANATION

Wildcards (?) represent an unknown type. Upper bound (? extends T) accepts T or its subtypes. Lower bound (? super T) accepts T or its supertypes.

Take Test
Q.254 Medium Generics
What is type erasure in Java Generics?
A The process by which generic type information is removed at runtime
B Automatic deletion of generic collections
C Compiler optimization of generic code
D Runtime type checking for generics
Correct Answer:  A. The process by which generic type information is removed at runtime
EXPLANATION

Type erasure is a mechanism where the Java compiler removes all generic type information during compilation, converting generic code to its raw type equivalent.

Take Test
Q.255 Easy Generics
Which of the following is a valid generic method declaration?
A public T getValue(T value) { return value; }
B public T getValue(T value) { return value; }
C public getValue(T value) { return value; }
D public getValue(T value) { return value; }
Correct Answer:  A. public T getValue(T value) { return value; }
EXPLANATION

The correct syntax for a generic method is: access_modifier <T> returnType methodName(T parameter). The type parameter <T> must come before the return type.

Take Test
Q.256 Easy Generics
What will be the output of the following code?
List list = new ArrayList();
list.add("Java");
System.out.println(list.get(0).length());
A 4
B 5
C Compilation error
D Runtime exception
Correct Answer:  A. 4
EXPLANATION

"Java" has 4 characters. The generic type ensures the element is a String, so .length() method is available without casting.

Take Test
Q.257 Easy Generics
Which keyword is used to declare a generic class in Java?
A
B generic
C template
D type
Correct Answer:  A.
EXPLANATION

<T> is the standard syntax for declaring a generic type parameter in Java classes.

Take Test
Q.258 Easy Generics
What is the primary purpose of Generics in Java?
A To provide type safety and eliminate type casting
B To increase memory allocation
C To reduce code compilation time
D To automatically manage garbage collection
Correct Answer:  A. To provide type safety and eliminate type casting
EXPLANATION

Generics enable type safety at compile time and eliminate the need for explicit type casting, reducing runtime errors.

Take Test
Q.259 Hard JDBC
In JDBC metadata operations, which method is used to retrieve information about table structure, column names, and their data types?
A getDatabaseMetaData()
B getMetaData() on ResultSet
C getColumnCount() and getColumnName()
D Both B and C together
Correct Answer:  D. Both B and C together
EXPLANATION

getMetaData() on ResultSet returns ResultSetMetaData which provides column information. getColumnCount() and getColumnName() are methods of ResultSetMetaData used to retrieve structure details.

Take Test
Q.260 Hard JDBC
A Java application requires connection pooling to handle 1000+ concurrent database requests efficiently. Which JDBC component should be implemented?
A Direct DriverManager.getConnection() calls
B DataSource interface with connection pool implementation like HikariCP or C3P0
C Creating new Connection for each request manually
D Using Statement caching only
Correct Answer:  B. DataSource interface with connection pool implementation like HikariCP or C3P0
EXPLANATION

DataSource with connection pooling frameworks (HikariCP, C3P0) efficiently manages connections by reusing them, reducing overhead. Direct getConnection() calls create new connections each time, which is inefficient for high concurrency.

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