Which interface in JDBC is used to execute SQL queries and obtain results?
Answer: A
Statement interface is used to execute SQL queries. Connection is used to establish database connection, ResultSet holds query results, and Driver manages database connections.
Q.2Easy
What is the correct order of JDBC operations?
Answer: A
The correct JDBC sequence is: 1) Load the JDBC driver, 2) Create connection, 3) Create statement, 4) Execute query, 5) Process results, 6) Close resources.
Q.3Easy
Which JDBC driver type is platform-independent and does not require native code?
Answer: D
Type 4 (Thin) drivers are pure Java drivers, platform-independent, and don't require native code. They communicate directly with database using Java sockets.
Q.4Easy
What exception is thrown when a database connection cannot be established?
Answer: A
SQLException is thrown for database-related errors including connection failures. ClassNotFoundException occurs when JDBC driver class is not found.
Q.5Easy
Which method is used to retrieve a String value from a ResultSet object?
Answer: B
getString() method retrieves String values from ResultSet. getInt() gets integer values, and fetchString/readString are not valid JDBC methods.
Advertisement
Q.6Medium
What does the executeUpdate() method return in JDBC?
Answer: B
executeUpdate() returns an integer representing the number of rows affected by INSERT, UPDATE, or DELETE operations. executeQuery() returns ResultSet.
Q.7Medium
Which interface is used to execute precompiled SQL statements in JDBC?
Answer: B
PreparedStatement is used for precompiled SQL statements with parameters. It provides better performance and prevents SQL injection compared to Statement.
Q.8Medium
What is the primary advantage of using PreparedStatement over Statement?
Answer: A
PreparedStatement offers faster execution due to precompilation and prevents SQL injection through parameterized queries using placeholders (?).
Q.9Medium
Which method must be called to move cursor to the next row in ResultSet?
Answer: A
The next() method moves the ResultSet cursor to the next row and returns true if a row exists, false if no more rows. It's essential for iterating through results.
Q.10Medium
What is Connection pooling in JDBC?
Answer: A
Connection pooling maintains a pool of reusable database connections to improve performance and reduce overhead of creating new connections repeatedly.
Q.11Medium
Which DriverManager method is used to establish a database connection?
Answer: B
DriverManager.getConnection() is the standard method that takes URL, username, and password to establish a connection to the database.
Q.12Medium
What does the setAutoCommit(false) method do in JDBC?
Answer: A
setAutoCommit(false) disables automatic commit, allowing multiple statements to be part of single transaction. Manual commit() or rollback() must be called.
Q.13Hard
Consider code: String query = "SELECT * FROM users WHERE id=" + userId; stmt.executeQuery(query); What security issue exists?
Answer: B
This is vulnerable to SQL injection as user input is directly concatenated. Using PreparedStatement with parameters prevents this: "SELECT * FROM users WHERE id=?"
Q.14Hard
How would you implement batch processing in JDBC for multiple INSERT operations?
Answer: A
addBatch() adds SQL command to batch, executeBatch() executes all batched commands at once, improving performance significantly for multiple operations.
Q.15Hard
What is the difference between execute(), executeQuery(), and executeUpdate()?
Answer: A
execute() returns boolean (true if ResultSet available), executeQuery() returns ResultSet for SELECT, executeUpdate() returns row count for INSERT/UPDATE/DELETE.
Q.16Hard
Which JDBC feature allows you to call stored procedures?
Answer: C
CallableStatement is used to invoke stored procedures and functions. It extends PreparedStatement and supports IN, OUT, and INOUT parameters.
Q.17Hard
In JDBC, what does the ResultSet.TYPE_SCROLL_INSENSITIVE constant represent?
Answer: B
TYPE_SCROLL_INSENSITIVE creates scrollable ResultSet (can move forward/backward) but changes made to database after ResultSet creation are not reflected.
Q.18Medium
Which of the following is the correct way to close JDBC resources to prevent resource leaks?
Answer: D
Resources should be closed in reverse order of creation (ResultSet → Statement → Connection), or preferably using try-with-resources which auto-closes resources.
Q.19Easy
Which JDBC driver type is platform-independent and does not require native code installation?
Answer: D
Type 4 drivers (thin drivers) are pure Java drivers that communicate directly with the database using the database native protocol. They are platform-independent and do not require native code.
Q.20Easy
What is the correct syntax to retrieve an integer value from a ResultSet object at column index 2?
Answer: A
The correct method is getInt(int columnIndex) which retrieves an integer value from the ResultSet at the specified column index (1-based indexing).