Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 301–310 of 958
Topics in Java Programming
Q.301 Easy JDBC
Which JDBC API is used to execute parameterized queries safely?
A Statement
B PreparedStatement
C ResultSet
D MetaData
Correct Answer:  B. PreparedStatement
EXPLANATION

PreparedStatement allows parameterized queries using placeholders (?) and prevents SQL injection attacks.

Take Test
Q.302 Easy JDBC
Which interface in JDBC is used to execute SQL queries and retrieve results?
A Statement
B Connection
C ResultSet
D Driver
Correct Answer:  A. Statement
EXPLANATION

Statement interface is used to execute SQL queries. Connection creates statements, ResultSet holds results, and Driver manages connections.

Take Test
Q.303 Hard JDBC
In a multi-threaded JDBC application, why should each thread have its own Connection object?
A To improve database performance
B Connections are not thread-safe and sharing causes race conditions
C To reduce memory usage
D It is just a best practice with no technical requirement
Correct Answer:  B. Connections are not thread-safe and sharing causes race conditions
EXPLANATION

JDBC Connections are not thread-safe. Each thread must have its own Connection to prevent concurrent access issues and data corruption.

Take Test
Q.304 Hard JDBC
What is the optimal approach to handle JDBC resources to prevent memory leaks in a production application?
A Use try-catch blocks and manually close resources
B Rely on garbage collection to close resources
C Use try-with-resources statement to auto-close resources
D Create resources globally and close them at application shutdown
Correct Answer:  C. Use try-with-resources statement to auto-close resources
EXPLANATION

Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, preventing memory leaks.

Take Test
Q.305 Hard JDBC
Consider a scenario where a developer retrieves a ResultSet and then closes the Statement object. What happens to the ResultSet?
A The ResultSet remains valid and can still be accessed
B The ResultSet becomes invalid in most drivers (implementation-dependent)
C An immediate SQLException is thrown
D The database automatically commits pending changes
Correct Answer:  B. The ResultSet becomes invalid in most drivers (implementation-dependent)
EXPLANATION

Closing a Statement typically invalidates its associated ResultSet in most JDBC drivers, though behavior may vary.

Take Test
Q.306 Hard JDBC
A developer creates a JDBC connection but forgets to close it. What might be the consequence?
A Automatic garbage collection will close it immediately
B Resource leak leading to exhaustion of database connections
C The program will automatically rollback all transactions
D No impact as JDBC handles it automatically
Correct Answer:  B. Resource leak leading to exhaustion of database connections
EXPLANATION

Unclosed connections remain allocated, eventually exhausting the connection pool and causing application failures.

Take Test
Q.307 Medium JDBC
In JDBC, what is the purpose of CallableStatement?
A To execute simple SELECT queries
B To execute stored procedures and functions
C To batch multiple SQL statements
D To manage transaction isolation levels
Correct Answer:  B. To execute stored procedures and functions
EXPLANATION

CallableStatement is used to call stored procedures and stored functions in the database.

Take Test
Q.308 Medium JDBC
What is Connection pooling in JDBC and why is it used?
A A method to execute multiple SQL statements in parallel
B Reusing database connections to improve performance and reduce overhead
C A feature to automatically backup database connections
D A mechanism to encrypt database connections
Correct Answer:  B. Reusing database connections to improve performance and reduce overhead
EXPLANATION

Connection pooling maintains a pool of reusable connections, reducing the cost of creating new connections repeatedly.

Take Test
Q.309 Medium JDBC
Which JDBC constant specifies read-only access for a ResultSet?
A ResultSet.CONCUR_READ_ONLY
B ResultSet.TYPE_READ_ONLY
C ResultSet.MODE_READ
D ResultSet.ACCESS_READ_ONLY
Correct Answer:  A. ResultSet.CONCUR_READ_ONLY
EXPLANATION

CONCUR_READ_ONLY is the concurrency constant that specifies a read-only ResultSet.

Take Test
Q.310 Medium JDBC
In JDBC, what is the purpose of the ResultSetMetaData interface?
A To execute SQL queries
B To retrieve information about the structure of a ResultSet
C To manage database connections
D To handle transaction management
Correct Answer:  B. To retrieve information about the structure of a ResultSet
EXPLANATION

ResultSetMetaData provides information about columns, data types, and column properties of a ResultSet.

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