Showing 71–80 of 270 questions
Which keyword is used to declare a generic class in Java?
A
B
generic
C
template
D
type
EXPLANATION
<T> is the standard syntax for declaring a generic type parameter in Java classes.
What is the primary purpose of Generics in Java?
A
To provide type safety and eliminate type casting
B
To increase memory allocation
C
To reduce code compilation time
D
To automatically manage garbage collection
Correct Answer:
A. To provide type safety and eliminate type casting
EXPLANATION
Generics enable type safety at compile time and eliminate the need for explicit type casting, reducing runtime errors.
In a JDBC application, which interface is responsible for executing SQL queries and returning ResultSet objects?
A
Statement
B
Connection
C
Driver
D
DataSource
Correct Answer:
A. Statement
EXPLANATION
The Statement interface is used to execute SQL queries. Connection creates the statement, Driver manages connections, and DataSource provides connection pooling, but Statement is the actual executor.
What is the difference between commit() and rollback() in JDBC transactions?
A
commit() saves changes, rollback() undoes them
B
Both perform the same function
C
rollback() is faster than commit()
D
commit() is used for SELECT queries, rollback() for UPDATE
Correct Answer:
A. commit() saves changes, rollback() undoes them
EXPLANATION
commit() permanently saves all changes made during the transaction, while rollback() discards all changes and reverts to the previous state.
Which exception is thrown when a JDBC driver is not found in the classpath?
A
SQLException
B
ClassNotFoundException
C
DriverNotFoundException
D
JDBCException
Correct Answer:
B. ClassNotFoundException
EXPLANATION
When Class.forName() cannot locate the driver class, it throws ClassNotFoundException, indicating the driver JAR is not in the classpath.
In JDBC, what does the BatchUpdate feature allow developers to do?
A
Execute single queries faster
B
Execute multiple queries in a single batch for improved performance
C
Create backup of database
D
Monitor database connections
Correct Answer:
B. Execute multiple queries in a single batch for improved performance
EXPLANATION
addBatch() and executeBatch() methods allow multiple SQL statements to be grouped and executed together, reducing network traffic and improving performance.
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.
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.
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.
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.