Showing 31–40 of 100 questions
in 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.
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.
What is the difference between execute() and executeUpdate() methods?
A
execute() returns boolean, executeUpdate() returns int
B
execute() can execute any SQL statement, executeUpdate() only for DML
C
Both A and B are correct
D
There is no difference
Correct Answer:
C. Both A and B are correct
EXPLANATION
execute() returns true/false and handles any SQL, while executeUpdate() returns affected rows count for DML statements only.
In JDBC, which class is used to retrieve database metadata information?
A
ResultSet
B
Statement
C
DatabaseMetaData
D
Connection
Correct Answer:
C. DatabaseMetaData
EXPLANATION
DatabaseMetaData provides information about the database, tables, columns, and other database properties.
What does ResultSetMetaData.getColumnCount() return?
A
Number of rows in ResultSet
B
Number of columns in ResultSet
C
Column data type
D
Column name
Correct Answer:
B. Number of columns in ResultSet
EXPLANATION
getColumnCount() returns the total number of columns present in the ResultSet.
What is the benefit of using PreparedStatement over Statement for repeated queries?
A
Faster execution due to pre-compilation
B
Automatic SQL injection prevention
C
Better readability of code
D
Both A and B
Correct Answer:
D. Both A and B
EXPLANATION
PreparedStatement offers both pre-compilation for performance and SQL injection prevention through parameterized queries.
Which JDBC method executes UPDATE, INSERT, or DELETE statements?
A
executeQuery()
B
execute()
C
executeUpdate()
D
executeBatch()
Correct Answer:
C. executeUpdate()
EXPLANATION
executeUpdate() executes DML statements and returns the number of rows affected.
What is the purpose of Connection.commit() in JDBC?
A
Close the connection
B
Save all changes made in the current transaction
C
Rollback transactions
D
Create a new statement
Correct Answer:
B. Save all changes made in the current transaction
EXPLANATION
commit() saves all changes made during the current transaction permanently to the database.
In JDBC, what does the executeQuery() method return?
A
int
B
boolean
C
ResultSet
D
void
Correct Answer:
C. ResultSet
EXPLANATION
executeQuery() returns a ResultSet containing the rows fetched from the database for SELECT queries.
What exception is thrown when a database connection fails in JDBC?
A
IOException
B
SQLException
C
ClassNotFoundException
D
NullPointerException
Correct Answer:
B. SQLException
EXPLANATION
SQLException is thrown when database-related errors occur, including connection failures and SQL execution errors.