JDBC in Java Multiple Choice Questions
JDBC in Java Multiple Choice Questions
JDBC is an API (Application Programming Interface) that helps a programmer to write a Java program to connect to a database, retrieve the data from the database, and perform various operations on the data in a Java program. As it is an important topic, the questions related to JDBC frequently asked in Java interviews and competitive exams. So, in this section, we have collected some multiple-choice questions based on JDBC that are important from the perspective of different competitive exams and interviews.
1) What are the major components of the JDBC?
DriverManager, Driver, Connection, Statement, and ResultSet
DriverManager, Driver, Connection, and Statement
DriverManager, Statement, and ResultSet
DriverManager, Connection, Statement, and ResultSet
Answer: a
Explanation:
DriverManager: Manages a list of database drivers.
Driver: The database communications link, handling all communication with the database.
Connection: Interface with all methods for contacting a database.
Statement: Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned, and executed.
ResultSet: The ResultSet represents a set of rows retrieved due to query execution.
2) Select the packages in which JDBC classes are defined?
jdbc and javax.jdbc
rdb and javax.rdb
jdbc and java.jdbc.sql
sql and javax.sql
Answer: d
Explanation: JDBC API is divided into two packages i.e. java.sql and javax.sql. We have to import these packages to use classes and interfaces in our application.
3) Thin driver is also known as?
Type 3 Driver
Type-2 Driver
Type-4 Driver
Type-1 Driver
Answer: c
Explanation: The JDBC thin driver is a pure Java driver. It is also known as Type-4 Driver. It is platform-independent so it does not require any additional Oracle software on the client-side. It communicates with the server using SQL *Net to access Oracle Database.
4) What is the correct sequence to create a database connection?
i. Import JDBC packages.
ii. Open a connection to the database.
iii. Load and register the JDBC driver.
iv. Execute the statement object and return a query resultset.
v. Create a statement object to perform a query.
vi. Close the resultset and statement objects.
vii. Process the resultset.
viii. Close the connection.
i, ii, iii, v, iv, vii, viii, vi
i, iii, ii, v, iv, vii, vi, viii
ii, i, iii, iv, viii, vii, v, vi
i, iii, ii, iv, v, vi, vii, viii
Answer: b
Explanation: To create a database connection in Java, we must follow the sequence given below:
Import JDBC packages.
Load and register the JDBC driver.
Open a connection to the database.
Create a statement object to perform a query.
Execute the statement object and return a query resultset.
Process the resultset.
Close the resultset and statement objects.
Close the connection.
5) Which of the following method is used to perform DML statements in JDBC?
executeResult()
executeQuery()
executeUpdate()
execute()
Answer: c
Explanation: We use the executeUpdate() method for DML SQL queries that change data in the database, such as INSERT, UPDATE, and DELETE which do not return a resultset.
6) How many transaction isolation levels provide the JDBC through the Connection interface?
3
4
7
2
Answer: b
Explanation: The following table defines transaction isolation levels.
Transaction Isolation Level Description
TRANSACTION_READ_UNCOMMITTED Dirty reads, non-repeatable reads, and phantom reads can occur.
TRANSACTION_READ_COMMITTED Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
TRANSACTION_REPEATABLE_READ Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
TRANSACTION_SERIALIZABLE Dirty reads, non-repeatable reads, and phantom reads are prevented.
7) Which of the following method is static and synchronized in JDBC API?
getConnection()
prepareCall()
executeUpdate()
executeQuery()
Answer: A
Explanation: A Java application using the JDBC API establishes a connection to a database by obtaining a Connection object. The standard way to obtain a Connection object is to call the method DriverManager.getConnection() method that accepts a String contains the database connection URL. It is a static and synchronized method.
8) Which methods are required to load a database driver in JDBC?
getConnection()
registerDriver()
forName()
Both b and c
Answer: d
Explanation: There are two ways to load a database driver in JDBC:
By using the registerDriver() Method: To access the database through a Java application, we must register the installed driver in our program. We can do this with the registerDriver() method that belongs to the DriverManager class. The registerDriver() method takes as input a driver class, that is, a class that implements the java.sql.Driver interface, as is the case with OracleDriver.
By using the Class.forName() Method: Alternatively, we can also use the forName() method of the java.lang.Class to load the JDBC drivers directly. However, this method is valid only for JDK-compliant Java virtual machines. It is invalid for Microsoft JVMs.
9) Parameterized queries can be executed by?
ParameterizedStatement
PreparedStatement
CallableStatement and Parameterized Statement
All kinds of Statements
Answer: b
Explanation: The PreparedStatement interface extends the Statement interface. It represents a precompiled SQL statement that can be executed multiple times. It accepts parameterized SQL quires. We can pass 0 or more parameters to this query.
10) Which of the following is not a valid statement in JDBC?
Statement
PreparedStatement
QueryStatement
CallableStatement
Answer: c
Explanation:
Statement: Use this for general-purpose access to your database. It is useful when we are using static SQL statements at runtime. The Statement interface cannot accept parameters.
PreparedStatement: It represents the pre-compiled SQL statements that can be executed multiple times.
CallableStatement: It is used to execute SQL stored procedures.
QueryStatement: It is not supported by JDBC.
11) Identify the isolation level that prevents the dirty in the JDBC Connection class?
TRANSACTION_READABLE_READ
TRANSACTION_READ_COMMITTED
TRANSACTION_READ_UNCOMMITTED
TRANSACTION_NONE
Answer: b
Explanation: The isolation level TRANSACTION_READ_COMMITTED prevents the dirty read but non-repeatable reads and phantom reads can occur.
12) What does setAutoCommit(false) do?
It will not commit transactions automatically after each query.
It explicitly commits the transaction.
It never commits the transactions.
It does not commit transaction automatically after each query.
Answer: b
Explanation: The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. After the auto-commit mode is disabled, no SQL statements are committed until we call the commit() method explicitly.
13) Stored procedure can be called by using the ????..?
CallableStatement
Statement
CalledStatement
PreparedStatement
Answer: b
Explanation: The stored procedure is a database program that can be utilized to perform CRUD tasks with the table. We can call these procedures by using the Statement Interface. It provides methods to execute queries with the database.
14) What should be the correct order to close the database resource?What should be the correct order to close the database resource?
Connection, Statements, and then ResultSet
ResultSet, Connection, and then Statements
Statements, ResultSet, and then Connection
ResultSet, Statements, and then Connection
Answer: d
Explanation: The golden rule to JDBC connections and statements is to close in the reverse order of initiation or opening. In addition, the ResultSet is dependant on the execution of the Statement and the Statement is dependant on the Connection instance. Hence, the closing should occur in that order (ResultSet, Statement, and then Connection).
15) A good way to debug JDBC-related problems is to enable???..?
JDBC tracing
Exception handling
Both a and b
Only b
Answer: a
Explanation: The JDBC Driver supports both DriverManager and DataSource tracing as documented in the JDBC 3.0 API specification. Trace information consists of JDBC API method entry and exit points with the corresponding parameter and returns values. DriverManager.setLogWriter method to send trace messages to a PrintWriter. The trace output contains a detailed listing of the JDBC activity.
16) Which JDBC driver can be used in servlet and applet both?
Type 3
Type 4
Type 3 and Type 2
Type 3 and Type 4
Answer: d
Explanation: Type 3 driver follows the three-tier approach which is used to access the databases. The JDBC clients use standard network sockets to communicate with a middleware application server. In a Type 4 driver, a pure Java-based driver that communicates directly with the vendor's database through a socket connection.
17) JDBC-ODBC driver is also known as?
Type 4
Type 3
Type 1
Type 2
Answer: c
Explanation: Type 1 driver is also known as the JDBC-ODBC bridge driver. It is a database driver implementation that employs the ODBC driver to connect to the database. The driver converts JDBC method calls into ODBC function calls.
18) Which of the following driver is the fastest one?
JDBC-ODBC Bridge Driver
Native API Partly Java Driver
Network Protocol Driver
JDBC Net Pure Java Driver
Answer: d
Explanation: JDBC Net pure Java driver (Type 4) is the fastest driver because it converts the JDBC calls into vendor-specific protocol calls and it directly interacts with the database.
19) Which of the following is not a type of ResultSet object?
TYPE_FORWARD_ONLY
CONCUR_WRITE_ONLY
TYPE_SCROLL_INSENSITIVE
TYPE_SCROLL_SENSITIVE
Answer: b
Explanation: There are three types of ResultSet object:
TYPE_FORWARD_ONLY: This is the default type and the cursor can only move forward in the result set.
TYPE_SCROLL_INSENSITIVE: The cursor can move forward and backward, and the result set is not sensitive to changes made by others to the database after the result set was created.
TYPE_SCROLL_SENSITIVE: The cursor can move forward and backward, and the result set is sensitive to changes made by others to the database after the result set was created.
Based on the concurrency there are two types of ResultSet object.
CONCUR_READ_ONLY: The ResultSet is read-only, this is the default concurrency type.
CONCUR_UPDATABLE: We can use the ResultSet update method to update the rows data.
20) What is JDBC Savepoint?
An intermediate or checkpoint in a transaction
A point where we can store queries
A point where the JDBC application starts execution
A memory where we can store transaction
Answer: a
Explanation: JDBC Savepoint helps us in creating checkpoints in a transaction and we can rollback to that particular checkpoint.
21) How many stages are used by Java programmers while using JDBC in their programs?
3
2
5
6
Answer: d
Explanation: There are following stages in a JDBC program:
Register the driver
Connecting to a database
Preparing SQL statements in Java
Executing the SQL statements on the database
Retrieving the results
Closing the connection
22) Which of the following is the correct to register a JdbcOdbcDriver?
jdbc.odbc.JdbcOdbcDriver obj = new sun.jdbc.odbc.JdbcOdbcDriver();
odbc.JdbcOdbcDriver obj = new sun.odbc.JdbcOdbcDriver();
jdbc.JdbcOdbcDriver obj = new sun.jdbc.JdbcOdbcDriver();
jdbc.odbc.JdbcOdbc obj = new sun.jdbc.odbc.JdbcOdbc();
Answer: a
Explanation: By creating an object to the driver class of the driver software, we can register the driver. To register the JdbcOdbcDriver of the sun microsystems, we can create an object to the driver class JdbcOdbcDriver, as follows: sun.jdbc.odbc.JdbcOdbcDriver obj = new sun.jdbc.odbc.JdbcOdbcDriver();
23) How many ways to register a driver?
2
3
4
5
Answer: c
Explanation: There are four ways to register a driver:
1. By creating an object of the Driver For example: sun.jdbc.odbc.JdbcOdbcDriver obj = new sun.jdbc.odbc.JdbcOdbcDriver();
2. By sending the driver class object to the registerDriver() method of the DriverManager class. For example: DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
3. By sending the driver class name directly to the forName() For example: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
4. By using the getProperty() method of the System class. For example: String dname = System.getProperty("driver"); Class.forName(dname);
If you are using the above method to register the driver, we should have to specify the driver's name at the time of running the program. The getProperty() method receives the driver name and stores the driver name in dname.
We use the following command to provide the driver name at run time: c:\> java -Ddriver = driverclassname Programname
For example: c:\> java -Ddriver = sun.jdbc.odbc.JdbcOdbcDriver MyProgram
24) Identify the DSN in the following statement:
DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger") DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger")
jdbc
odbc
scott
oradsn
Answer: d
Explanation: Data Source Name (DSN) is a name given to the database to identify it in the Java program. The DSN is linked with the actual location of the database.
25) Which statement is correct if we want to connect the Oracle database using the thin driver provided by Oracle Corp.?
getConnection("jdbc::thin@localhost:1521:oracle", "scott", "tiger");
getConnection("jdbc:thin@localhost:1521:oracle", "scott", "tiger");
getConnection("jdbc::thin@localhost:1522:oracle", "scott", "tiger");
getConnection("jdbc::oracle@localhost:1521:thin", "scott", "tiger");
Answer: a
Explanation: We use the following statement to connect Oracle database using the thin driver. DriverManager.getConnection("jdbc::thin@localhost:1521:oracle", "scott", "tiger");
26) What are the types of ResultSet in JDBC?
Forward ResultSet
Scrollable ResultSet
Only a
Both a and b
Answer: d
Explanation: JDBC provides only two types of ResultSets: Forward and Scrollable ResultSet.
27) What is blob in the following statement?
create table profilepic(photo blob); create table profilepic(photo blob);
Variable
Object
Data type
Keyword
Answer: c
Explanation: SQL offers BLOB (Binary Large OBject) data type to store image files like .gif or .jpg or jpeg into the database table.
28) Which data type is used to store files in the database table?
BLOB
CLOB
File
Both a and b
Answer: b
Explanation: To store a large volume of data such as text file into a table, we use CLOB (Character Large OBject) data type of SQL.
29) DatabaseMetaData interface is used to get?????..?
Comprehensive information about the database as a whole.
Comprehensive information about the table as a whole.
Comprehensive information about the column as a whole.
Both b and c
Answer: a
Explanation: DatabaseMetaData is an interface that is used to get Comprehensive information about the database as a whole. It is implemented by driver vendors to let users know the capabilities of a DBMS in combination with the JDBC driver that is used with it.
30) Which of the following driver converts the JDBC calls into database-specific calls?
JDBC-ODBC Bridge Driver (Type 1)
Native API-partly Java Driver (Type 2)
Net Protocol-pure Java Driver (Type 3)
Native Protocol-pure Java Driver (Type 4)
Answer: b
Explanation: Type 2 driver converts JDBC calls into database-specific calls with the help of vendor database library. It directly communicates with the database server.
31) Are ResultSets updateable?
Yes, but only if we call the method openCursor() on the ResultSet and if the driver and database support this option.
Yes, but only if we indicate a concurrency strategy when executing the statement, and if the driver and database support this option.
Yes, but only if the ResultSet is an object of class UpdateableResultSet and if the driver and database support this option.
No, ResultSets are never updateable. We must explicitly execute a DML statement to change the data in the underlying database.
Answer: b
Explanation: By default, a ResultSet object is not updatable and its cursor moves only in the forward direction. If we want to create an updateable ResultSet object, either we can use ResultSet.TYPE_SCROLL_INSENSITIVE or the ResultSet.TYPE_SCROLL_SENSITIVE type, which moves the cursor forward and backward relative to the current position.
32) Which of the following interface provides the commit() and rollback() methods?
Statement Interface
ResultSet Interface
Connection Interface
RowSet Interface
Answer: c
Explanation: The connection interface provides the commit() and rollback() method. The commit() method makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by this Connection object. The rollback() method undoes all changes made in the current transaction and releases any database locks currently held by this Connection object.
33) How many statement objects can be created using a Connection?
2
1
3
Multiple
Answer: d
Explanation: Multiple statements can be created and used on the same connection, but only one resultset at once can be created and used on the same statement.
34) JDBC API supports____________ and __________ architecture model for accessing the database.
Two-tier
Three-tier
Both a and b
Only b
Answer: c
Explanation: The JDBC API supports both two-tier and three-tier processing models for database access. In the two-tier model, a Java application talks directly to the data source. In the three-tier model, commands are sent to a "middle tier" of services, which then sends the commands to the data source.
35) Which statement(s) is/ are true about transactions?
i. A transaction is a set of one or more SQL statements that make up a logical unit of work.
ii. A transaction ends with either a commit or a rollback, depending on whether there are any problems with data consistency or data concurrency.
iii. A lock is a mechanism that allows two transactions from manipulating the same data at the same time.
iv. To avoid conflicts during a transaction, a DBMS uses locks, mechanisms for blocking access by others to the data that is being accessed by the transaction.
Only i and ii
Only i and iii
Only i, ii, and iv
All the above
Answer: c
Explanation: The statements i, ii, and iv are true about transactions but iii is not because the lock mechanism prohibits two transactions from manipulating the same data at the same time.
Comments
Post a Comment