Java Database Connectivity (JDBC) Practice Quiz
- What is JDBC in Java?
- A) A method to connect Java to web services
- B) A technology to connect Java applications to databases
- C) A programming language
- D) A framework for data visualization
Answer: B) A technology to connect Java applications to databases
- Which interface is used to establish a connection to a database in JDBC?
- A) Statement
- B) Connection
- C) ResultSet
- D) PreparedStatement
Answer: B) Connection
- Which JDBC class is used to execute SQL queries?
- A) Connection
- B) Statement
- C) ResultSet
- D) PreparedStatement
Answer: B) Statement
- Which method is used to close the JDBC connection?
- A) closeConnection()
- B) close()
- C) disconnect()
- D) shutdown()
Answer: B) close()
- Which of the following is the correct order for creating a JDBC connection?
- Load the driver
- Establish the connection
- Create statement
- Execute query
- Close the connection
- A) 1, 2, 3, 4, 5
- B) 1, 3, 2, 4, 5
- C) 3, 1, 2, 4, 5
- D) 1, 2, 4, 3, 5
Answer: A) 1, 2, 3, 4, 5
- Which method is used to execute an SQL query in JDBC?
- A) executeQuery()
- B) execute()
- C) executeUpdate()
- D) Both A and B
Answer: D) Both A and B
- Which method of the Statement interface is used to execute an update (like INSERT, UPDATE, DELETE)?
- A) execute()
- B) executeUpdate()
- C) executeQuery()
- D) executeBatch()
Answer: B) executeUpdate()
- What is the purpose of the ResultSet object in JDBC?
- A) To store SQL queries
- B) To store the result of a database query
- C) To update the database records
- D) To create a database connection
Answer: B) To store the result of a database query
- Which class is used to manage database connections in JDBC?
- A) DriverManager
- B) Statement
- C) Connection
- D) ResultSet
Answer: A) DriverManager
- What is the use of PreparedStatement in JDBC?
- A) To execute SQL queries
- B) To store SQL query results
- C) To execute SQL queries with parameters
- D) To close the database connection
Answer: C) To execute SQL queries with parameters
- What is the correct way to handle exceptions in JDBC?
- A) Using throws clause
- B) Using try-catch blocks
- C) Using exception handling classes only
- D) Not handling exceptions is fine
Answer: B) Using try-catch blocks
- Which of the following methods is used to get the value of a column in a ResultSet object?
- A) getValue()
- B) getString()
- C) getColumnValue()
- D) getColumn()
Answer: B) getString()
- Which JDBC driver type requires the database to be installed on the same machine as the Java program?
- A) Type 1
- B) Type 2
- C) Type 3
- D) Type 4
Answer: B) Type 2
- What is the default auto-commit mode in JDBC?
- A) True
- B) False
- C) Depends on database
- D) It is not set by default
Answer: A) True
- Which of the following is not a JDBC driver type?
- A) Type 1
- B) Type 2
- C) Type 3
- D) Type 5
Answer: D) Type 5
- Which of the following JDBC classes supports transactions?
- A) Statement
- B) Connection
- C) ResultSet
- D) PreparedStatement
Answer: B) Connection
- How can you set the auto-commit mode to false in JDBC?
- A) conn.setAutoCommit(false)
- B) conn.setTransactionMode(false)
- C) conn.setAutoTransaction(false)
- D) conn.setCommitMode(false)
Answer: A) conn.setAutoCommit(false)
- Which of the following methods is used to commit a transaction in JDBC?
- A) commitTransaction()
- B) commit()
- C) commitChanges()
- D) executeCommit()
Answer: B) commit()
- What is the purpose of calling the rollback() method in JDBC?
- A) To commit the transaction
- B) To cancel the transaction
- C) To execute the transaction again
- D) To log the transaction
Answer: B) To cancel the transaction
- Which type of ResultSet holds the data of a query that is scrollable?
- A) TYPE_FORWARD_ONLY
- B) TYPE_SCROLL_INSENSITIVE
- C) TYPE_SCROLL_SENSITIVE
- D) TYPE_READ_ONLY
Answer: C) TYPE_SCROLL_SENSITIVE
- What does the getGeneratedKeys() method return in JDBC?
- A) The metadata of the generated keys
- B) The primary key value generated by the database
- C) The SQL query result
- D) The connection status
Answer: B) The primary key value generated by the database
- Which of the following is used to load the JDBC driver?
- A) Class.forName(“com.jdbc.Driver”)
- B) Connection.load(“com.jdbc.Driver”)
- C) DriverManager.load(“com.jdbc.Driver”)
- D) Class.getInstance(“com.jdbc.Driver”)
Answer: A) Class.forName(“com.jdbc.Driver”)
- Which method is used to check if a connection is still valid in JDBC?
- A) isValid()
- B) checkConnection()
- C) validateConnection()
- D) connectionStatus()
Answer: A) isValid()
- Which type of statement is used for executing precompiled SQL queries with parameters in JDBC?
- A) Statement
- B) PreparedStatement
- C) CallableStatement
- D) ResultSet
Answer: B) PreparedStatement
- How can you execute a stored procedure using JDBC?
- A) Using PreparedStatement
- B) Using CallableStatement
- C) Using Statement
- D) Using ResultSet
Answer: B) Using CallableStatement
- Which method of ResultSet is used to retrieve the current row number?
- A) getRow()
- B) getCurrentRow()
- C) currentRowNumber()
- D) rowIndex()
Answer: A) getRow()
- Which of the following exceptions is thrown when a database access error occurs in JDBC?
- A) SQLException
- B) IOException
- C) ClassNotFoundException
- D) NullPointerException
Answer: A) SQLException
- Which method is used to move the cursor to the first row in the ResultSet?
- A) first()
- B) moveFirst()
- C) goFirst()
- D) next()
Answer: A) first()
- What is the correct way to handle multiple statements in JDBC?
- A) Use a Batch Statement
- B) Use multiple ResultSet objects
- C) Execute each statement separately
- D) Use a single PreparedStatement
Answer: A) Use a Batch Statement
- Which of the following is true regarding JDBC connections?
- A) You need to manually close connections after use.
- B) JDBC connections are automatically closed by the system.
- C) You don’t need to worry about closing connections.
- D) JDBC automatically establishes new connections when needed.
Answer: A) You need to manually close connections after use.
- Which of the following methods is used to retrieve a connection object in JDBC?
- A) DriverManager.getConnection()
- B) Connection.getConnection()
- C) Driver.getConnection()
- D) ConnectionManager.getConnection()
Answer: A) DriverManager.getConnection()
- What does the setAutoCommit(false) method do in JDBC?
- A) Enables automatic commits after each SQL statement
- B) Disables automatic commits and requires a manual commit
- C) Commits all statements in a batch
- D) Sets the isolation level for the connection
Answer: B) Disables automatic commits and requires a manual commit
- Which of the following is not a type of ResultSet?
- A) TYPE_SCROLL_INSENSITIVE
- B) TYPE_SCROLL_SENSITIVE
- C) TYPE_FORWARD_ONLY
- D) TYPE_BACKWARD_ONLY
Answer: D) TYPE_BACKWARD_ONLY
- Which class is used to execute SQL queries that return a result set in JDBC?
- A) Statement
- B) PreparedStatement
- C) CallableStatement
- D) Connection
Answer: A) Statement
- Which method in JDBC is used to execute a stored procedure?
- A) executeProcedure()
- B) executeQuery()
- C) executeUpdate()
- D) execute()
Answer: D) execute()
- Which type of ResultSet is read-only and only allows forward movement?
- A) TYPE_SCROLL_INSENSITIVE
- B) TYPE_SCROLL_SENSITIVE
- C) TYPE_FORWARD_ONLY
- D) TYPE_READ_ONLY
Answer: C) TYPE_FORWARD_ONLY
- Which of the following methods of Statement is used to execute a batch of SQL statements?
- A) executeBatch()
- B) execute()
- C) addBatch()
- D) executeQueryBatch()
Answer: A) executeBatch()
- What happens if you don’t close the JDBC connection?
- A) The connection is automatically closed by the database server.
- B) The connection is kept open indefinitely.
- C) It may cause memory leaks and unnecessary resource usage.
- D) It throws an exception.
Answer: C) It may cause memory leaks and unnecessary resource usage.
- What is the return type of the executeQuery() method in JDBC?
- A) ResultSet
- B) int
- C) boolean
- D) void
Answer: A) ResultSet
- What is the default value of the auto-commit mode in JDBC?
- A) True
- B) False
- C) Depends on the database
- D) It must be explicitly set
Answer: A) True
- What is the primary purpose of a JDBC Driver?
- A) To establish a connection to a database
- B) To execute SQL queries
- C) To store data in a database
- D) To log SQL query execution
Answer: A) To establish a connection to a database
- Which type of JDBC driver is implemented entirely in Java?
- A) Type 1
- B) Type 2
- C) Type 3
- D) Type 4
Answer: D) Type 4
- Which method is used to register the JDBC driver in the program?
- A) Driver.registerDriver()
- B) DriverManager.registerDriver()
- C) Class.forName()
- D) Connection.registerDriver()
Answer: C) Class.forName()
- Which of the following methods retrieves an integer value from a ResultSet?
- A) getInt()
- B) getInteger()
- C) getValue()
- D) getColumnValue()
Answer: A) getInt()
- Which class in JDBC is used to retrieve and update database records?
- A) Statement
- B) PreparedStatement
- C) ResultSet
- D) Connection
Answer: C) ResultSet
- How can you add a parameter to a PreparedStatement in JDBC?
- A) setInt()
- B) addParameter()
- C) setString()
- D) Both A and C
Answer: D) Both A and C
- Which of the following methods is used to set the connection’s transaction isolation level?
- A) setIsolationLevel()
- B) setTransactionMode()
- C) setTransactionIsolation()
- D) setConnectionLevel()
Answer: C) setTransactionIsolation()
- Which method is used to update the ResultSet when using a scrollable ResultSet?
- A) updateRow()
- B) update()
- C) setRow()
- D) moveTo()
Answer: A) updateRow()
- What does the commit() method do in JDBC?
- A) Executes the SQL statement
- B) Commits the current transaction
- C) Closes the current connection
- D) Resets the database connection
Answer: B) Commits the current transaction
- Which of the following is true about the Statement interface in JDBC?
- A) It is used to execute SQL queries without parameters
- B) It is used to execute SQL queries with parameters
- C) It is used to execute stored procedures
- D) It can only execute SELECT statements
Answer: A) It is used to execute SQL queries without parameters
- Which of the following is not a valid JDBC method for retrieving data from a ResultSet?
- A) getString()
- B) getInt()
- C) getDouble()
- D) setData()
Answer: D) setData()
- What is the purpose of the setAutoCommit(true) method in JDBC?
- A) It commits all changes after every SQL statement.
- B) It prevents committing any transaction until commit() is explicitly called.
- C) It allows you to execute SQL statements in batches.
- D) It automatically rolls back any changes made.
Answer: A) It commits all changes after every SQL statement.
- What does the executeUpdate() method return in JDBC when executing an INSERT statement?
- A) The number of affected rows
- B) A ResultSet object
- C) A boolean value
- D) The number of updated records
Answer: A) The number of affected rows
- Which of the following exceptions is thrown when a database connection cannot be established in JDBC?
- A) SQLException
- B) IOException
- C) ClassNotFoundException
- D) ConnectionException
Answer: A) SQLException
- Which of the following statements about JDBC batch processing is true?
- A) Batch processing is only available in Type 4 JDBC drivers.
- B) All SQL statements in a batch are executed one by one.
- C) Batch processing is more efficient than executing each statement individually.
- D) JDBC does not support batch processing.
Answer: C) Batch processing is more efficient than executing each statement individually.
- Which JDBC method is used to retrieve a prepared statement from the connection object?
- A) createStatement()
- B) prepareStatement()
- C) prepareQuery()
- D) executeStatement()
Answer: B) prepareStatement()
- Which of the following is true regarding CallableStatement in JDBC?
- A) It is used to execute regular SQL queries.
- B) It is used to execute stored procedures.
- C) It cannot accept parameters.
- D) It is not supported in JDBC 3.0 and higher.
Answer: B) It is used to execute stored procedures.
- What is the use of the setFetchSize() method in JDBC?
- A) It sets the maximum number of rows returned by a query.
- B) It sets the size of the ResultSet object.
- C) It sets the number of rows to fetch in each database round trip.
- D) It sets the size of the data buffer.
Answer: C) It sets the number of rows to fetch in each database round trip.
- What is the purpose of the setMaxRows() method in JDBC?
- A) To limit the number of rows that can be fetched by a ResultSet
- B) To set the maximum number of rows to be inserted in a batch
- C) To specify the maximum number of rows to be updated
- D) To limit the size of the SQL query result
Answer: A) To limit the number of rows that can be fetched by a ResultSet
- Which method of the Connection interface is used to create a PreparedStatement?
- A) createStatement()
- B) prepareStatement()
- C) prepareQuery()
- D) createPreparedStatement()
Answer: B) prepareStatement()
- Which of the following methods is used to close a JDBC connection?
- A) closeConnection()
- B) disconnect()
- C) close()
- D) terminate()
Answer: C) close()
- Which interface in JDBC is used to execute SQL queries that return multiple results?
- A) ResultSet
- B) Statement
- C) PreparedStatement
- D) CallableStatement
Answer: B) Statement
- Which type of ResultSet allows both forward and backward movement of the cursor?
- A) TYPE_FORWARD_ONLY
- B) TYPE_SCROLL_INSENSITIVE
- C) TYPE_SCROLL_SENSITIVE
- D) TYPE_SCROLLABLE
Answer: C) TYPE_SCROLL_SENSITIVE
- Which method is used to execute an SQL query in JDBC that returns no result?
- A) execute()
- B) executeUpdate()
- C) executeBatch()
- D) executeQuery()
Answer: B) executeUpdate()
- What is the purpose of the setMaxFieldSize() method in JDBC?
- A) To limit the maximum number of rows returned by a ResultSet
- B) To set the maximum length of a column in the result set
- C) To set the maximum number of fields that can be used in a SQL statement
- D) To set the maximum size of the database connection pool
Answer: B) To set the maximum length of a column in the result set
- Which method is used to retrieve a string from a ResultSet?
- A) getString()
- B) retrieveString()
- C) fetchString()
- D) getText()
Answer: A) getString()
- Which of the following JDBC classes supports batch processing?
- A) PreparedStatement
- B) Statement
- C) CallableStatement
- D) All of the above
Answer: D) All of the above
- Which exception is thrown when a database connection fails in JDBC?
- A) SQLException
- B) IOException
- C) SQLException
- D) DatabaseConnectionException
Answer: A) SQLException
- What is the function of the executeUpdate() method in JDBC?
- A) It executes SQL queries that return a ResultSet.
- B) It is used to execute DDL statements (like CREATE, DROP).
- C) It executes SQL queries that update the database and returns the count of affected rows.
- D) It executes SQL stored procedures.
Answer: C) It executes SQL queries that update the database and returns the count of affected rows.
- Which method is used to set a value to a parameter in a PreparedStatement?
- A) setParameter()
- B) set()
- C) setObject()
- D) setValue()
Answer: C) setObject()
- What does the setFetchDirection() method specify in JDBC?
- A) The direction of fetch operation
- B) The fetch size
- C) The fetch mode
- D) The order of rows in the ResultSet
Answer: A) The direction of fetch operation
- Which method is used to commit the current transaction in JDBC?
- A) commitTransaction()
- B) commit()
- C) executeCommit()
- D) endTransaction()
Answer: B) commit()
- Which method in the DriverManager class is used to deregister a JDBC driver?
- A) deregisterDriver()
- B) unregisterDriver()
- C) removeDriver()
- D) deregister()
Answer: A) deregisterDriver()
- Which type of JDBC driver communicates with the database via a middleware server?
- A) Type 1
- B) Type 2
- C) Type 3
- D) Type 4
Answer: C) Type 3
- Which method is used to execute a stored procedure with parameters using JDBC?
- A) executeQuery()
- B) executeProcedure()
- C) execute()
- D) prepareStatement()
Answer: C) execute()
- Which method is used to add a batch of SQL statements to a PreparedStatement?
- A) addBatch()
- B) executeBatch()
- C) addStatement()
- D) executeStatementBatch()
Answer: A) addBatch()
- Which JDBC class is used for handling result sets returned by queries in database operations?
- A) Statement
- B) ResultSet
- C) PreparedStatement
- D) Connection
Answer: B) ResultSet
- What does the setAutoCommit(false) method do in JDBC?
- A) It disables the automatic commit of SQL statements.
- B) It sets the database connection to read-only mode.
- C) It automatically commits every SQL statement.
- D) It sets the connection’s timeout value.
Answer: A) It disables the automatic commit of SQL statements.
- Which method of the Connection interface is used to create a Statement object in JDBC?
- A) createStatement()
- B) createPreparedStatement()
- C) createCallableStatement()
- D) createQuery()
Answer: A) createStatement()
- Which JDBC method retrieves an integer from the ResultSet?
- A) getInteger()
- B) getInt()
- C) retrieveInt()
- D) fetchInteger()
Answer: B) getInt()
- What is the purpose of the getGeneratedKeys() method in JDBC?
- A) It retrieves the SQL query that generated the keys.
- B) It returns the keys generated by a database operation (such as an INSERT statement).
- C) It gets all keys in the database.
- D) It generates primary keys for tables in the database.
Answer: B) It returns the keys generated by a database operation (such as an INSERT statement).
- Which JDBC method is used to execute an SQL statement that returns a single result?
- A) executeQuery()
- B) executeUpdate()
- C) execute()
- D) executeSingleResult()
Answer: A) executeQuery()
- Which of the following JDBC classes allows executing SQL queries and receiving results from the database?
- A) Connection
- B) Statement
- C) PreparedStatement
- D) CallableStatement
Answer: B) Statement
- What does the setTransactionIsolation() method do in JDBC?
- A) It sets the type of database to use for transactions.
- B) It sets the isolation level for database transactions.
- C) It specifies whether a transaction is read-only.
- D) It commits a transaction with the specified isolation level.
Answer: B) It sets the isolation level for database transactions.
- Which of the following is used to execute a stored procedure in JDBC?
- A) executeUpdate()
- B) executeQuery()
- C) execute()
- D) executeBatch()
Answer: C) execute()
- What is the purpose of the setEscapeProcessing() method in JDBC?
- A) It processes escape sequences in SQL statements.
- B) It escapes characters in SQL strings.
- C) It enables or disables automatic transaction commits.
- D) It disables the use of escape sequences in SQL queries.
Answer: A) It processes escape sequences in SQL statements.
- What does the isClosed() method of the Connection interface do in JDBC?
- A) Returns whether the connection is open or closed.
- B) Closes the connection to the database.
- C) Closes the ResultSet object.
- D) Returns whether the database is active.
Answer: A) Returns whether the connection is open or closed.
- What is the default isolation level for JDBC connections?
- A) READ_COMMITTED
- B) SERIALIZABLE
- C) READ_UNCOMMITTED
- D) TRANSACTION_REPEATABLE_READ
Answer: A) READ_COMMITTED
- Which JDBC method is used to execute a batch of SQL commands in a single database call?
- A) executeBatch()
- B) execute()
- C) executeUpdate()
- D) executeQueryBatch()
Answer: A) executeBatch()
- Which of the following classes are used for handling SQL exceptions in JDBC?
- A) SQLException
- B) SQLWarning
- C) DatabaseException
- D) Both A and B
Answer: D) Both A and B
- Which of the following methods is used to execute a SQL query that retrieves data from a database in JDBC?
- A) executeSelect()
- B) executeQuery()
- C) fetchResults()
- D) execute()
Answer: B) executeQuery()
- What type of JDBC statement is used to call a stored procedure?
- A) Statement
- B) PreparedStatement
- C) CallableStatement
- D) ExecutableStatement
Answer: C) CallableStatement
- Which of the following classes is used to represent a database connection in JDBC?
- A) Connection
- B) ResultSet
- C) Statement
- D) DataSource
Answer: A) Connection
- What does the setAutoCommit(true) method do in JDBC?
- A) Commits every transaction automatically after execution.
- B) Sets the isolation level to default.
- C) Prevents the automatic commit of SQL statements.
- D) Sets the connection to be read-only.
Answer: A) Commits every transaction automatically after execution.
- What is the default value of auto-commit in a JDBC connection?
- A) true
- B) false
- C) null
- D) depends on the database
Answer: A) true
- Which JDBC class is used to prepare a SQL query with placeholders (parametrized query)?
- A) Statement
- B) PreparedStatement
- C) CallableStatement
- D) QueryStatement
Answer: B) PreparedStatement
- What happens if you try to use a Statement object after calling its close() method?
- A) The object will be automatically reopened.
- B) The object will throw an exception.
- C) The object will execute the last query automatically.
- D) Nothing happens.
Answer: B) The object will throw an exception.
- Which of the following methods is used to execute an SQL query that updates data in the database?
- A) executeUpdate()
- B) executeBatch()
- C) executeQuery()
- D) executeInsert()
Answer: A) executeUpdate()
- Which JDBC interface is used to fetch a large number of rows from the database at once?
- A) ResultSet
- B) Statement
- C) PreparedStatement
- D) BatchResultSet
Answer: A) ResultSet
- Which JDBC method is used to get the last auto-generated keys after an INSERT operation?
- A) getGeneratedKeys()
- B) getLastInsertedId()
- C) getAutoGeneratedKey()
- D) retrieveLastKey()
Answer: A) getGeneratedKeys()
- Which of the following JDBC interfaces does not extend the Statement interface?
- A) PreparedStatement
- B) CallableStatement
- C) ResultSet
- D) All extend Statement
Answer: C) ResultSet
- What is the main purpose of JDBC drivers in Java?
- A) To communicate with the database.
- B) To execute SQL commands.
- C) To manage database transactions.
- D) To format SQL queries.
Answer: A) To communicate with the database.
- Which JDBC class is used to manage database transactions?
- A) TransactionManager
- B) Connection
- C) PreparedStatement
- D) DataSource
Answer: B) Connection
- Which of the following methods is used to set the fetch size for a ResultSet in JDBC?
- A) setFetchSize()
- B) setResultSize()
- C) setRowLimit()
- D) setMaxRows()
Answer: A) setFetchSize()
- What does the setQueryTimeout() method do in JDBC?
- A) Sets the timeout for preparing a statement.
- B) Sets the maximum number of rows to retrieve.
- C) Sets the time limit for executing a SQL query.
- D) Sets the database timeout.
Answer: C) Sets the time limit for executing a SQL query.
- Which method is used to fetch the value of a column in a ResultSet as an integer?
- A) getInt()
- B) getColumnValue()
- C) fetchInt()
- D) retrieveInteger()
Answer: A) getInt()
- Which method is used to execute a SQL query that returns multiple rows and columns?
- A) execute()
- B) executeQuery()
- C) executeUpdate()
- D) executeBatch()
Answer: B) executeQuery()
- What happens when the clearBatch() method is called in JDBC?
- A) It clears the batch of SQL statements for the current Statement object.
- B) It clears the ResultSet object.
- C) It clears all the SQL statements from the database.
- D) It commits the transaction.
Answer: A) It clears the batch of SQL statements for the current Statement object.
- Which of the following methods is used to retrieve a boolean value from a ResultSet?
- A) getBoolean()
- B) getBool()
- C) fetchBoolean()
- D) retrieveBoolean()
Answer: A) getBoolean()
- Which of the following is true about Connection in JDBC?
- A) It is used to execute SQL queries.
- B) It establishes a connection to the database.
- C) It stores the results of a SQL query.
- D) It processes the results returned by queries.
Answer: B) It establishes a connection to the database.
- Which method of PreparedStatement is used to set the value of a parameter in the SQL query?
- A) setValue()
- B) setParameter()
- C) set()
- D) setObject()
Answer: D) setObject()
- Which class implements the Driver interface in JDBC?
- A) DriverManager
- B) MySQLDriver
- C) Connection
- D) ResultSet
Answer: B) MySQLDriver
- What is the role of the ResultSetMetaData class in JDBC?
- A) To process ResultSet values.
- B) To retrieve metadata about the structure of a ResultSet.
- C) To set the fetch size of a ResultSet.
- D) To execute SQL queries.
Answer: B) To retrieve metadata about the structure of a ResultSet.
- Which method is used to execute a stored procedure that returns a single result set?
- A) execute()
- B) executeUpdate()
- C) executeQuery()
- D) executeBatch()
Answer: C) executeQuery()
- What type of database objects can be executed using a CallableStatement in JDBC?
- A) SQL queries
- B) SQL DDL statements
- C) Stored procedures and functions
- D) Both A and B
Answer: C) Stored procedures and functions
- Which of the following methods is used to begin a transaction in JDBC?
- A) beginTransaction()
- B) setAutoCommit(false)
- C) startTransaction()
- D) commitTransaction()
Answer: B) setAutoCommit(false)
- Which of the following JDBC classes does not deal directly with SQL queries?
- A) Statement
- B) Connection
- C) PreparedStatement
- D) ResultSet
Answer: B) Connection
- What does the setMaxRows() method do in JDBC?
- A) Limits the number of rows fetched by the ResultSet.
- B) Limits the number of rows updated by the statement.
- C) Limits the number of rows inserted into the database.
- D) Sets the number of columns in the ResultSet.
Answer: A) Limits the number of rows fetched by the ResultSet.
- Which method is used to get a string value from a ResultSet in JDBC?
- A) getStringValue()
- B) fetchString()
- C) getString()
- D) retrieveString()
Answer: C) getString()
- Which of the following is the correct order of steps for using JDBC?
- A) Establish a connection, Create statement, Execute query, Retrieve results, Close connection.
- B) Establish connection, Execute query, Create statement, Retrieve results, Close connection.
- C) Execute query, Create statement, Establish connection, Retrieve results, Close connection.
- D) Create statement, Retrieve results, Establish connection, Close connection.
Answer: A) Establish a connection, Create statement, Execute query, Retrieve results, Close connection.
- Which method in Statement is used to execute an SQL query that may return multiple results?
- A) executeQuery()
- B) executeUpdate()
- C) execute()
- D) executeBatch()
Answer: C) execute()
- What does executeUpdate() return in JDBC?
- A) A boolean indicating success
- B) The number of rows affected by the SQL statement
- C) A ResultSet object
- D) The transaction ID
Answer: B) The number of rows affected by the SQL statement
- Which JDBC driver type is platform-independent?
- A) Type 1
- B) Type 2
- C) Type 3
- D) Type 4
Answer: D) Type 4
- Which of the following methods of Connection is used to set the transaction isolation level?
- A) setAutoCommit()
- B) setTransactionIsolation()
- C) setIsolationLevel()
- D) setCommitLevel()
Answer: B) setTransactionIsolation()
- Which of the following is not a valid isolation level in JDBC?
- A) TRANSACTION_READ_UNCOMMITTED
- B) TRANSACTION_REPEATABLE_READ
- C) TRANSACTION_SERIALIZABLE
- D) TRANSACTION_UNIQUE_READ
Answer: D) TRANSACTION_UNIQUE_READ
- Which method is used to retrieve the database’s metadata in JDBC?
- A) getDatabaseMetaData()
- B) getMetaData()
- C) getDBInfo()
- D) fetchDatabaseMetaData()
Answer: A) getDatabaseMetaData()
- What does commit() do in JDBC?
- A) Starts a transaction
- B) Saves changes made to the database during a transaction
- C) Rolls back the current transaction
- D) Ends a session with the database
Answer: B) Saves changes made to the database during a transaction
- Which of the following classes is used to handle SQL exceptions in JDBC?
- A) SQLException
- B) DatabaseException
- C) SQLRuntimeException
- D) ConnectionException
Answer: A) SQLException
- What is the default fetch size of a ResultSet in JDBC?
- A) 10
- B) 100
- C) 50
- D) Depends on the database driver
Answer: D) Depends on the database driver
- Which statement is true about JDBC ResultSet?
- A) It can only hold the results of a single SQL query at a time.
- B) It is an object that represents a SQL statement.
- C) It can be used to insert records into a database.
- D) It is automatically closed when the connection is closed.
Answer: A) It can only hold the results of a single SQL query at a time.
- Which method is used to close a ResultSet object?
- A) closeResultSet()
- B) close()
- C) finalize()
- D) clear()
Answer: B) close()
- Which of the following methods can be used to execute a batch of SQL statements in JDBC?
- A) executeBatch()
- B) executeUpdate()
- C) execute()
- D) executeStatements()
Answer: A) executeBatch()
- Which interface is implemented by JDBC driver classes?
- A) Connection
- B) Statement
- C) Driver
- D) DataSource
Answer: C) Driver
- Which JDBC exception is thrown when a SQL query exceeds the execution time limit?
- A) SQLTimeoutException
- B) SQLException
- C) SQLExecutionException
- D) QueryTimeoutException
Answer: A) SQLTimeoutException
- Which method is used to retrieve the value of a column as a double from a ResultSet?
- A) getDouble()
- B) retrieveDouble()
- C) fetchDouble()
- D) getColumnValue()
Answer: A) getDouble()
- Which of the following methods is used to set the query timeout for a JDBC statement?
- A) setQueryTimeout()
- B) setTimeout()
- C) setExecutionTime()
- D) setTimeoutLimit()
Answer: A) setQueryTimeout()
- What does ResultSet.next() do in JDBC?
- A) Moves the cursor to the next row of the ResultSet.
- B) Retrieves the next value in the current row.
- C) Checks if the result set contains more rows.
- D) Closes the ResultSet.
Answer: A) Moves the cursor to the next row of the ResultSet.
- Which JDBC method is used to execute SQL statements for both DML and DDL operations?
- A) execute()
- B) executeUpdate()
- C) executeBatch()
- D) executeQuery()
Answer: B) executeUpdate()
- What is the role of the DriverManager class in JDBC?
- A) It manages database connections.
- B) It manages multiple SQL queries.
- C) It registers JDBC drivers.
- D) It manages SQL exceptions.
Answer: C) It registers JDBC drivers.
- What is the correct sequence of steps in JDBC?
- Create a Statement object.
- Execute the SQL query.
- Process the ResultSet object.
- Close the connection.
- A) 1, 2, 3, 4
- B) 1, 3, 2, 4
- C) 2, 1, 3, 4
- D) 3, 2, 1, 4
Answer: A) 1, 2, 3, 4
- Which JDBC interface is used to execute a SQL query and return a ResultSet object?
- A) Statement
- B) PreparedStatement
- C) CallableStatement
- D) ResultSet
Answer: A) Statement
- Which method in PreparedStatement is used to set an integer parameter?
- A) setInt()
- B) setInteger()
- C) setParameter()
- D) set()
Answer: A) setInt()
- Which method is used to retrieve the metadata about the columns in a ResultSet?
- A) getColumnMetaData()
- B) getMetaData()
- C) getResultSetMetaData()
- D) getColumnInfo()
Answer: C) getResultSetMetaData()
- Which of the following methods is used to set a String parameter in a PreparedStatement?
- A) setStringValue()
- B) setText()
- C) setString()
- D) setParameter()
Answer: C) setString()
- What is the function of the Connection object in JDBC?
- A) To manage transactions and database sessions.
- B) To execute SQL queries.
- C) To fetch and display query results.
- D) To handle SQL exceptions.
Answer: A) To manage transactions and database sessions.
- Which method is used to check if a ResultSet has more rows?
- A) hasMore()
- B) next()
- C) hasNext()
- D) nextRow()
Answer: C) hasNext()
- What is the effect of calling setAutoCommit(false) in JDBC?
- A) Transactions must be committed manually using commit().
- B) Auto-commit is enabled by default.
- C) All transactions are rolled back automatically.
- D) SQL queries are executed in batch.
Answer: A) Transactions must be committed manually using commit().
- Which JDBC interface is used to handle batch processing?
- A) BatchStatement
- B) Statement
- C) PreparedStatement
- D) CallableStatement
Answer: B) Statement
- What happens if you call close() on a Connection object in JDBC?
- A) It closes all associated Statement and ResultSet objects.
- B) It commits all pending transactions.
- C) It releases the connection back to the connection pool.
- D) It clears all database queries.
Answer: A) It closes all associated Statement and ResultSet objects.
- Which of the following is used to execute stored procedures in JDBC?
- A) CallableStatement
- B) PreparedStatement
- C) Statement
- D) SQLStatement
Answer: A) CallableStatement
- Which of the following methods in JDBC is used to execute a SELECT SQL query?
- A) executeUpdate()
- B) executeQuery()
- C) execute()
- D) executeBatch()
Answer: B) executeQuery()
- Which of the following is true about PreparedStatement in JDBC?
- A) It is used for executing static SQL queries.
- B) It allows the SQL query to be compiled each time it is executed.
- C) It supports parameterized queries, making it more efficient.
- D) It does not support batch execution.
Answer: C) It supports parameterized queries, making it more efficient.
- What is the default value of auto-commit in JDBC?
- A) true
- B) false
- C) depends on the database
- D) undefined
Answer: A) true
- What method do you use to begin a transaction in JDBC when auto-commit is disabled?
- A) startTransaction()
- B) beginTransaction()
- C) setAutoCommit(false)
- D) commit()
Answer: C) setAutoCommit(false)
- Which method is used to execute a stored procedure in JDBC?
- A) executeProcedure()
- B) callProcedure()
- C) execute()
- D) executeQuery()
Answer: C) execute()
- Which of the following exceptions is thrown when a database connection cannot be established?
- A) SQLException
- B) DatabaseNotFoundException
- C) SQLConnectionException
- D) ConnectionFailureException
Answer: A) SQLException
- Which of the following methods in Statement is used to execute a batch of SQL commands?
- A) executeBatch()
- B) executeUpdate()
- C) execute()
- D) executeMulti()
Answer: A) executeBatch()
- Which of the following is true about ResultSet in JDBC?
- A) It stores the result of an executed query.
- B) It is used for storing SQL statements.
- C) It only supports scrolling forwards.
- D) It is a permanent object stored in the database.
Answer: A) It stores the result of an executed query.
- Which of the following methods is used to retrieve the value of a column in a ResultSet as an integer?
- A) getInt()
- B) getInteger()
- C) fetchInt()
- D) retrieveInteger()
Answer: A) getInt()
- What is the role of DriverManager in JDBC?
- A) It manages multiple database connections.
- B) It registers available JDBC drivers.
- C) It connects to the database automatically.
- D) It manages the SQL query execution.
Answer: B) It registers available JDBC drivers.
- What happens when setAutoCommit(true) is called in JDBC?
- A) Changes to the database are automatically committed after each statement execution.
- B) The current transaction is rolled back.
- C) The session is closed automatically.
- D) The database connection is reset.
Answer: A) Changes to the database are automatically committed after each statement execution.
- Which JDBC method is used to execute SQL statements that update or modify the database (e.g., INSERT, UPDATE, DELETE)?
- A) executeQuery()
- B) executeUpdate()
- C) execute()
- D) executeBatch()
Answer: B) executeUpdate()
- What is a PreparedStatement used for in JDBC?
- A) It is used to execute SELECT queries.
- B) It allows parameterized queries and is more efficient than Statement.
- C) It is used to fetch metadata of a database.
- D) It is used to batch multiple SQL statements.
Answer: B) It allows parameterized queries and is more efficient than Statement.
- Which of the following is true about CallableStatement?
- A) It is used to execute stored procedures in a database.
- B) It is used to execute simple SELECT queries.
- C) It cannot accept parameters.
- D) It executes SQL queries as part of the batch process.
Answer: A) It is used to execute stored procedures in a database.
- Which of the following methods is used to set the SQL query timeout in JDBC?
- A) setTimeout()
- B) setQueryTimeout()
- C) setExecutionTimeout()
- D) setQueryLimit()
Answer: B) setQueryTimeout()
- What does setTransactionIsolation() method do in JDBC?
- A) Sets the isolation level of the database.
- B) Sets the database to auto-commit mode.
- C) Defines the maximum number of transactions allowed.
- D) Sets the timeout for a database query.
Answer: A) Sets the isolation level of the database.
- Which of the following methods is used to retrieve a string value from a ResultSet?
- A) getString()
- B) getValue()
- C) retrieveString()
- D) fetchString()
Answer: A) getString()
- What is the default transaction isolation level in JDBC?
- A) TRANSACTION_READ_UNCOMMITTED
- B) TRANSACTION_REPEATABLE_READ
- C) TRANSACTION_SERIALIZABLE
- D) TRANSACTION_READ_COMMITTED
Answer: D) TRANSACTION_READ_COMMITTED
- Which method is used to set a parameter in a PreparedStatement?
- A) setParameter()
- B) setValue()
- C) setObject()
- D) set()
Answer: D) set()
- What is the purpose of the ResultSetMetaData interface in JDBC?
- A) It provides information about the data types of columns in a ResultSet.
- B) It stores the query result.
- C) It updates the values in the ResultSet.
- D) It retrieves database connection details.
Answer: A) It provides information about the data types of columns in a ResultSet.
- Which method is used to retrieve the number of columns in a ResultSet?
- A) getColumnCount()
- B) getMetaData()
- C) getColumnSize()
- D) getNumberOfColumns()
Answer: B) getMetaData()
- Which of the following is used to handle SQL exceptions in JDBC?
- A) SQLException
- B) SQLConnectionException
- C) SQLRuntimeException
- D) SQLTransactionException
Answer: A) SQLException
- What is the purpose of the Connection interface in JDBC?
- A) It manages database connections.
- B) It provides methods to execute SQL queries.
- C) It processes the query result.
- D) It represents SQL statements.
Answer: A) It manages database connections.
- Which of the following JDBC methods is used to execute a stored procedure that has output parameters?
- A) executeQuery()
- B) executeUpdate()
- C) execute()
- D) executeBatch()
Answer: C) execute()
- Which method is used to close the connection to the database in JDBC?
- A) closeConnection()
- B) close()
- C) release()
- D) endConnection()
Answer: B) close()
- Which class in JDBC is used to establish a connection to the database?
- A) Statement
- B) ResultSet
- C) Connection
- D) DriverManager
Answer: C) Connection
- Which method retrieves the number of rows affected by a SQL query executed with Statement?
- A) getRowCount()
- B) getAffectedRows()
- C) getUpdateCount()
- D) getResultCount()
Answer: C) getUpdateCount()
- Which of the following statements is correct about the Statement object in JDBC?
- A) It is used to execute a query that returns a ResultSet.
- B) It is used to execute SQL queries that do not return any results.
- C) It is used to manage database connections.
- D) It cannot be used for executing SQL queries that modify the database.
Answer: A) It is used to execute a query that returns a ResultSet.
- What does the Connection.commit() method do in JDBC?
- A) It commits all changes made during the current transaction to the database.
- B) It starts a new transaction.
- C) It rolls back the current transaction.
- D) It terminates the database connection.
Answer: A) It commits all changes made during the current transaction to the database.
- Which of the following is the correct order of closing JDBC objects?
- A) ResultSet, Statement, Connection
- B) Statement, ResultSet, Connection
- C) Connection, Statement, ResultSet
- D) ResultSet, Connection, Statement
Answer: A) ResultSet, Statement, Connection