Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 281–290 of 958
Topics in Java Programming
Q.281 Easy JDBC
Which method of Connection interface is used to start a transaction in JDBC?
A setAutoCommit(false)
B beginTransaction()
C startTransaction()
D createTransaction()
Correct Answer:  A. setAutoCommit(false)
EXPLANATION

setAutoCommit(false) disables auto-commit mode, allowing manual control over transactions through commit() and rollback() methods.

Take Test
Q.282 Easy JDBC
What is the correct sequence to establish a JDBC connection in Java?
A Load Driver → Create Connection → Execute Query → Close Connection
B Create Connection → Load Driver → Execute Query → Close Connection
C Load Driver → Create Statement → Execute Query → Close Connection
D Create Statement → Load Driver → Create Connection → Execute Query
Correct Answer:  A. Load Driver → Create Connection → Execute Query → Close Connection
EXPLANATION

The correct JDBC connection sequence is: Class.forName() → DriverManager.getConnection() → Create Statement → Execute Query → Close resources.

Take Test
Q.283 Easy JDBC
Which interface in JDBC is used to execute parameterized queries and prevent SQL injection attacks?
A Statement
B PreparedStatement
C CallableStatement
D Connection
Correct Answer:  B. PreparedStatement
EXPLANATION

PreparedStatement is used for parameterized queries with placeholders (?), which prevents SQL injection by separating SQL logic from data.

Take Test
Q.284 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.285 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.286 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.287 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.288 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
Q.289 Medium JDBC
What happens if you call next() on a ResultSet after the last row?
A Throws SQLException
B Returns false
C Returns true
D Throws NoSuchElementException
Correct Answer:  B. Returns false
EXPLANATION

next() returns false when the cursor is positioned beyond the last row, indicating no more rows available.

Take Test
Q.290 Medium JDBC
Which statement should be used when you need to retrieve column information without executing multiple queries?
A Statement with multiple executeQuery()
B PreparedStatement
C ResultSetMetaData
D DatabaseMetaData
Correct Answer:  C. ResultSetMetaData
EXPLANATION

ResultSetMetaData provides column information directly from the ResultSet without additional queries.

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