Java Programming — JDBC
Java OOP, collections, multithreading
25 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 25 questions in JDBC
Q.1 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.2 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.3 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.4 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.5 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
Advertisement
Q.6 Hard JDBC
When using JDBC with Spring Framework in 2024-25, which approach is recommended for resource management?
A Manual try-catch blocks
B Try-with-resources statement
C JdbcTemplate with Spring
D DriverManager only
Correct Answer:  C. JdbcTemplate with Spring
EXPLANATION

JdbcTemplate in Spring Framework automatically manages JDBC resources and is the recommended approach in modern applications.

Take Test
Q.7 Hard JDBC
In JDBC, what is the significance of setting FetchSize on a Statement when dealing with large ResultSets?
A It determines the maximum number of rows to fetch from database in one round-trip
B It sets the total memory allocated for ResultSet
C It automatically closes the ResultSet
D It enables caching of all rows in memory
Correct Answer:  A. It determines the maximum number of rows to fetch from database in one round-trip
EXPLANATION

FetchSize controls how many rows are fetched from the database at once, optimizing performance for large datasets.

Take Test
Q.8 Hard JDBC
Which JDBC feature should be used for executing multiple SQL statements in a batch for optimal performance?
A executeUpdate() in a loop
B executeBatch() with addBatch()
C execute() method
D Multiple Statement objects
Correct Answer:  B. executeBatch() with addBatch()
EXPLANATION

executeBatch() with addBatch() reduces network round-trips and improves performance for bulk operations.

Take Test
Q.9 Hard JDBC
In a JDBC application, when using transactions with setAutoCommit(false), what happens to intermediate savepoints if rollback() is called?
A All changes are saved
B Only savepoint changes are rolled back
C All changes since last commit are rolled back
D The entire connection is closed
Correct Answer:  C. All changes since last commit are rolled back
EXPLANATION

When rollback() is called without savepoints, all changes made since the last commit are undone and rolled back.

Take Test
Q.10 Hard JDBC
Consider a scenario: A developer uses Statement with user input directly in SQL queries. What is the primary risk?
A Slower query execution
B SQL Injection attacks
C Memory leaks
D Connection timeout
Correct Answer:  B. SQL Injection attacks
EXPLANATION

Direct concatenation of user input in SQL queries exposes the application to SQL injection attacks. PreparedStatement should be used instead.

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