MCQs on Java Database Connectivity (JDBC) | Java Database Integration

Discover Java Database Connectivity (JDBC) through this comprehensive set of 30 MCQs. Perfect for developers looking to enhance their database integration skills, this collection covers JDBC basics, drivers, connections, SQL queries, prepared statements, and batch processing techniques.


MCQs

1-10: Introduction to JDBC

  1. What does JDBC stand for in Java?
    • A) Java Database Communication
    • B) Java Data Connector
    • C) Java Database Connectivity
    • D) Java Database Class
  2. Which of the following is a major purpose of JDBC?
    • A) To connect Java applications to various databases
    • B) To enhance Java user interfaces
    • C) To speed up Java applications
    • D) To manage memory in Java applications
  3. What is the first step to establish a connection to a database in JDBC?
    • A) Load the JDBC driver
    • B) Execute an SQL query
    • C) Close the connection
    • D) Set the database credentials
  4. Which of the following methods is used to create a database connection in JDBC?
    • A) DriverManager.getConnection()
    • B) Connection.openConnection()
    • C) JDBC.connect()
    • D) Database.connect()
  5. Which class is used to manage the database connection in JDBC?
    • A) Statement
    • B) Connection
    • C) Driver
    • D) ResultSet
  6. Which interface provides methods to establish a connection to a database in JDBC?
    • A) Connection
    • B) DriverManager
    • C) Statement
    • D) PreparedStatement
  7. Which method is used to load a JDBC driver class in Java?
    • A) Class.forName()
    • B) Driver.load()
    • C) getClass()
    • D) JDBC.loadDriver()
  8. Which of the following can JDBC be used for?
    • A) Managing database connections
    • B) Performing CRUD operations
    • C) Executing SQL queries
    • D) All of the above
  9. In JDBC, what type of object is used to execute SQL queries?
    • A) Statement
    • B) ResultSet
    • C) Connection
    • D) CallableStatement
  10. What type of JDBC object is used to execute stored procedures?
  • A) CallableStatement
  • B) PreparedStatement
  • C) Statement
  • D) ResultSet

11-15: JDBC Drivers and Connections

  1. What is the purpose of a JDBC driver?
  • A) To handle communication between Java and the database
  • B) To store database credentials
  • C) To execute SQL queries
  • D) To display results in the UI
  1. Which of the following is a type of JDBC driver?
  • A) Type 1: JDBC-ODBC Bridge
  • B) Type 2: Native-API Driver
  • C) Type 3: Network Protocol Driver
  • D) All of the above
  1. Which JDBC driver is suitable for direct connection to the database server using native API calls?
  • A) Type 1 Driver
  • B) Type 2 Driver
  • C) Type 3 Driver
  • D) Type 4 Driver
  1. Which method in the Connection interface is used to create a Statement object?
  • A) createStatement()
  • B) prepareStatement()
  • C) setStatement()
  • D) newStatement()
  1. What is the correct syntax to register a JDBC driver in Java?
  • A) DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
  • B) Driver.registerDriver(com.mysql.cj.jdbc.Driver);
  • C) Connection.registerDriver(com.mysql.cj.jdbc.Driver);
  • D) DriverManager.register(com.mysql.cj.jdbc.Driver);

16-20: Executing SQL Queries in Java

  1. Which method is used to execute an SQL query in JDBC?
  • A) executeQuery()
  • B) execute()
  • C) runQuery()
  • D) executeSQL()
  1. Which of the following methods is used to execute an SQL update query (INSERT, UPDATE, DELETE)?
  • A) executeUpdate()
  • B) executeQuery()
  • C) execute()
  • D) runUpdate()
  1. What does the executeQuery() method in JDBC return?
  • A) An integer indicating the number of rows affected
  • B) A ResultSet object containing the query results
  • C) A Connection object
  • D) A Statement object
  1. Which of the following is used to handle SQL exceptions in JDBC?
  • A) SQLException
  • B) SQLExceptionHandler
  • C) DatabaseException
  • D) JDBCException
  1. What type of result does executeUpdate() return in JDBC?
  • A) A ResultSet
  • B) A boolean
  • C) An integer indicating rows affected
  • D) A Connection object

21-30: Prepared Statements and Batch Processing

  1. What is the main advantage of using PreparedStatement over Statement in JDBC?
  • A) Prevents SQL injection
  • B) Increases query execution time
  • C) Automatically closes the connection
  • D) Reduces memory usage
  1. Which of the following methods is used to set parameters in a PreparedStatement?
  • A) setParameter()
  • B) setObject()
  • C) setInt(), setString(), etc.
  • D) bindParameter()
  1. What is the purpose of batch processing in JDBC?
  • A) To execute multiple SQL queries in one go
  • B) To increase memory usage
  • C) To execute one SQL query repeatedly
  • D) To handle SQL exceptions more effectively
  1. Which method is used to add a query to the batch in JDBC?
  • A) addBatch()
  • B) batchQuery()
  • C) addStatement()
  • D) addSQL()
  1. How do you execute a batch of SQL queries in JDBC?
  • A) executeBatch()
  • B) executeQuery()
  • C) runBatch()
  • D) execute()
  1. Which of the following methods is used to clear the batch of queries in JDBC?
  • A) clearBatch()
  • B) resetBatch()
  • C) clearQueries()
  • D) flushBatch()
  1. What does the executeBatch() method return?
  • A) A ResultSet object
  • B) An array of integers indicating the number of affected rows for each statement
  • C) A Connection object
  • D) A PreparedStatement object
  1. What is the best practice for executing batch updates with PreparedStatement in JDBC?
  • A) Always execute a single update at a time
  • B) Use executeUpdate() for batch processing
  • C) Use addBatch() to add multiple statements and execute them in bulk
  • D) Avoid using batch processing for large queries
  1. Which of the following is true about PreparedStatement in JDBC?
  • A) It supports SQL injection attacks
  • B) It is slower than Statement
  • C) It is precompiled, making it more efficient for repeated use
  • D) It cannot accept parameters
  1. What is the correct sequence of steps for batch processing in JDBC?
  • A) Create Statement, add queries using addBatch(), execute with executeBatch()
  • B) Create Connection, set parameters using setParameter(), execute with executeUpdate()
  • C) Create PreparedStatement, add queries with executeBatch(), execute with addBatch()
  • D) Create Statement, set parameters, and execute with execute()

Answer Key

QnoAnswer
1C) Java Database Connectivity
2A) To connect Java applications to various databases
3A) Load the JDBC driver
4A) DriverManager.getConnection()
5B) Connection
6A) Connection
7A) Class.forName()
8D) All of the above
9A) Statement
10A) CallableStatement
11A) To handle communication between Java and the database
12D) All of the above
13B) Type 2 Driver
14A) createStatement()
15A) DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
16A) executeQuery()
17A) executeUpdate()
18B) A ResultSet object containing the query results
19A) SQLException
20C) An integer indicating rows affected
21A) Prevents SQL injection
22C) setInt(), setString(), etc.
23A) To execute multiple SQL queries in one go
24A) addBatch()
25A) executeBatch()
26A) clearBatch()
27B) An array of integers indicating the number of affected rows for each statement
28C) Use addBatch() to add multiple statements and execute them in bulk
29C) It is precompiled, making it more efficient for repeated use
30A) Create Statement, add queries using addBatch(), execute with executeBatch()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top