Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 51–60 of 212 questions
Q.51 Hard Generics
What will be the result of this code?
List list = new ArrayList();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
A ClassCastException when accessing list.get(1)
B Code executes without error
C Compilation error
D StringIndexOutOfBoundsException
Correct Answer:  A. ClassCastException when accessing list.get(1)
EXPLANATION

Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.

Take Test
Q.52 Hard Generics
Which statement is true about generic inheritance?
A ArrayList is NOT a subtype of List due to invariance
B ArrayList IS a subtype of List
C ArrayList is a subtype of ArrayList
D Generic types support covariance by default
Correct Answer:  B. ArrayList IS a subtype of List
EXPLANATION

ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.

Take Test
Q.53 Hard Generics
Consider this code:
public T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound ?
A Ensures type safety by guaranteeing T implements Comparable with itself
B Allows T to be compared with any type
C Improves runtime performance
D Eliminates the need for type erasure
Correct Answer:  A. Ensures type safety by guaranteeing T implements Comparable with itself
EXPLANATION

Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.

Take Test
Q.54 Hard Generics
What will happen with this code?
List list = new ArrayList();
list.add(123); // Adding Integer
String s = (String) list.get(0);
A ClassCastException at runtime
B Compilation error
C Code executes successfully
D Unchecked warning at compile time only
Correct Answer:  A. ClassCastException at runtime
EXPLANATION

Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.

Take Test
Q.55 Hard Generics
Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
A public void process(List
B public void process(List
C public void process(List source, List dest) { }
D public void process(List source, List dest) { }
Correct Answer:  A. public void process(List
EXPLANATION

PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.

Take Test
Advertisement
Q.56 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.57 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
Q.58 Hard JDBC
In a JDBC application using batch updates, what happens if one statement in the batch fails?
A All statements are executed, and failure is reported
B All statements are rolled back automatically
C Only failed statement is skipped
D Behavior depends on database configuration
Correct Answer:  D. Behavior depends on database configuration
EXPLANATION

executeBatch() behavior on failure varies by database and driver. BatchUpdateException is thrown, containing update counts for each statement. Explicit transaction control is recommended.

Take Test
Q.59 Hard JDBC
A query execution takes 5 seconds consistently. Which JDBC optimization technique should NOT be used for this scenario?
A Connection pooling
B Batch processing for multiple inserts
C Using indexes on frequently queried columns
D Increasing setFetchSize() value
Correct Answer:  D. Increasing setFetchSize() value
EXPLANATION

If query execution itself is slow (5 seconds), the bottleneck is in database operations, not data transfer. Increasing setFetchSize() only optimizes data retrieval, not query execution.

Take Test
Q.60 Hard JDBC
A developer wants to retrieve data from a stored procedure that returns multiple result sets and output parameters. Which approach is correct?
A Use Statement with executeQuery()
B Use PreparedStatement with execute()
C Use CallableStatement with registerOutParameter() and execute()
D Use PreparedStatement with setFetchSize()
Correct Answer:  C. Use CallableStatement with registerOutParameter() and execute()
EXPLANATION

CallableStatement is specifically designed for stored procedures, supporting registerOutParameter() for output parameters and getMoreResults() for multiple result sets.

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