Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

Question 1:

Which of the following interfaces can be used to execute a stored procedure on the
database?

A. CallableStatement

B. PreparedStatement

C. ProceduralStatement

D. StoredStatement

Question 2:

Identify the correct statement regarding a JDBC Connection:

A. When a JDBC Connection is created, it is in auto-commit mode.

B. When a JDBC Connection is created, its commit mode depends on the parameters
used while creating the connection.

C. When a JDBC Connection is created, its auto-commit feature is disabled.

D. When a JDBC Connection is created, its commit mode is undetermined.

Question 2:

Identify the correct statement regarding a JDBC Connection:

A. When a JDBC Connection is created, it is in auto-commit mode.

B. When a JDBC Connection is created, its commit mode depends on the parameters
used while creating the connection.

C. When a JDBC Connection is created, its auto-commit feature is disabled.

D. When a JDBC Connection is created, its commit mode is undetermined.

Question 3:

Given:

String qr = "insert into STOCK ( ID, TICKER, LTP, EXCHANGE ) values( ?, ?, ?, ?)";
String[] tickers = {"AA", "BB", "CC", "DD" };

You are trying to initialize the STOCK table and for that you need to insert one
row for each of the ticker value in the tickers array. Each row has to be
initialized with the same values except the ID and TICKER columns, which are
different for each row. The ID column is defined as AUTO_INCREMENT and so you need
to pass only 0 for this column.

Which of the following code snippets would you use?

A.
for(String ticker : tickers)
try(PreparedStatement ps = c.prepareStatement(qr);) {
ps.setInt(1, 0);
ps.setString(2, ticker);
ps.setDouble(3, 0.0);
ps.setString(4, "NYSE");
ps.executeUpdate();
}

B.
try(PreparedStatement ps = c.prepareStatement(qr);) {
for(String ticker : tickers) {
ps.setInt(1, 0);
ps.setString(2, ticker);
ps.setDouble(3, 0.0);
ps.setString(4, "NYSE");
ps.executeUpdate();
}
}

C.
try(PreparedStatement ps = c.prepareStatement(qr);) {
ps.setInt(1, 0);
ps.setDouble(3, 0.0);
ps.setString(4, "NYSE");
for(String ticker : tickers) {
ps.setString(2, ticker);
ps.executeUpdate();
}
}

D.
for(String ticker : tickers)
try(Statement s = c.createStatement(qr);) {
s.executeUpdate("insert into STOCK ( ID, TICKER, LTP, EXCHANGE ) values( 0,
'"+ticker+"', 0.0, 'NYSE')");
}

Question 4:

A JDBC driver implementation must provide implementation classes for which of the
following interfaces?

A. java.sql.DriverManager

B. java.sql.Driver

C. java.sql.Connection

D. java.sql.Statement

E. java.sql.SQLException

F. java.sql.Date

Question 5:

Given:
String qr = "insert into USERINFO values( ?, ?, ?)";
try(PreparedStatement ps = c.prepareStatement(qr);)
{
ps.setObject(0, 1); //1
ps.setObject(1, "Ally A", JDBCType.VARCHAR); //2
ps.setObject(2, "101 main str"); //3
ps.executeUpdate(); //4
ps.setObject(1, "Bob B"); //5
ps.setNull(2, java.sql.Types.VARCHAR); //5
ps.executeUpdate(); //6
}

What will be the result?

A. Two rows with the following values will be inserted in the USERINFO table: 1,
Ally A, 101 main str null, Bob B, null

B. Two rows with the following values will be inserted in the USERINFO table: 1,
Ally A, 101 main str 1, Bob B, 101 main str

C. An exception will be thrown at //1.

D. An exception will be thrown at //2.

E. An exception will be thrown at //3.

F. An exception will be thrown at //4.

G. An exception will be thrown at //5.

H. An exception will be thrown at //6.

Question 6:

Given that a table named STOCK exists with nullable columns as follows:

ID(int, primary key) TICKER(varchar) LASTPRICE(int, nullable)


EXCHANGE(varchar, nullable)

and the following code:

Connection c = //get connection

String qr = "insert into STOCK ( ID, TICKER, LASTPRICE, EXCHANGE )


values( ?, ?, ?, ?)";

PreparedStatement ps = c.prepareStatement(qr);
ps.setInt(1, 1);
ps.setString(2, "APPL");

//INSERT CODE HERE

int i = ps.executeUpdate(); //1


System.out.println(i);

What can be inserted in the above code so that a row with values (1, "APPL", null,
null) will be inserted in the STOCK table?

A.
ps.setNull(3, JDBCType.INTEGER);
ps.setNull(4, JDBCType.STRING);

B.
ps.setNull(3, JDBCType.INTEGER);
ps.setNull(4, JDBCType.VARCHAR);

C.
ps.setNull(3, Types.INTEGER);
ps.setNull(4, Types.STRING);

D.
Integer ltp = null;
ps.setInt(3, ltp);
ps.setString(4, ltp);

E.
ps.setNull(3, Types.INTEGER);
ps.setString(4, null);

Question 7:

Given the following RDBMS table information :


STUDENT Table
SID INT Primary Key
NAME VARCHAR(50)
GPA INT

and the following code:

Statement stmt = connection.createStatement();


ResultSet rs = stmt.executeQuery("select SID, NAME, GPA from STUDENT");
while(rs.next()){
System.out.println( INSERT CODE HERE );
}
connection.close();

What can be inserted in the above code so that it will print the GPA value for each
student?

(Assume that items not specified such as import statements and try/catch block are
all valid.)

A. rs.getString(2)

B. rs.getString(3)

C. rs.getInt(2)

D. rs.getInteger(2)

E. rs.getInt("GPA")

Question 8:

Identify correct statement(s) regarding the benefit of using PreparedStatement over


Statement.

A. PreparedStatement offers better performance.

B. PreparedStatement offers better performance when the same query is to be run


multiple times with different parameter values.
C. PreparedStatement supports multiple transactions.

D. PreparedStatement allows several additional SQL types such as BLOB and CLOB.

Question 9:

Which of the following method would you use to get a JDBC connection while using
JDBC 4.0 but not while using a previous version of JDBC?

A.
public Connection getConnection1(String url, String userid, String pwd) throws
Exception{
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
return DriverManager.getConnection(url, p);
}

B.
public Connection getConnection2(String url, String userid, String pwd) throws
Exception{
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
DriverManager.registerDriver("com.xyz.Driver");
return DriverManager.getConnection(url, p);
}

C.
public Connection getConnection3(String url, String userid, String pwd) throws
Exception{
Properties p = new Properties();
p.setProperty("jdbc.driver", "com.xyz.Driver");
p.setProperty("jdbc.user", userid);
p.setProperty("jdbc.user.password", pwd);
return DriverManager.getConnection(url, p);
}

D.
public Connection getConnection4(String url, String userid, String pwd) throws
Exception{
Class.forName("com.xyz.Driver");
return DriverManager.getConnection(url, userid, pwd);
}

E.
public Connection getConnection5(String url, String userid, String pwd) throws
SQLException{
return DriverManager.getConnection(url, userid, pwd);
}

Question 10:

Identify the correct statements regarding JDBC.

A. The JDBC Driver must be loaded explicitly in the code before attempting to
create a connection to the database.
B. Starting JDBC 4.0, the JDBC Driver class is not required any more.

C. Starting JDBC 4.0, the JDBC Driver class is not required to be loaded explicitly
in the code any more.

D. JDBC 4.0 allows loading multiple JDBC Drivers while previous versions did not.

Question 11:

Given that a code fragment has just created a JDBC Connection and has executed an
update statement, which of the following statements is correct?

A. Changes to the database are pending a commit call on the connection.

B. Changes to the database will be rolled back if another update is executed


without commiting the previous update.

C. Changes to the database will be committed right after the update statement has
completed execution.

D. Changes to the database will be committed when another query (update or select)
is fired using the connection.

Question 12:

Which of the following are valid JDBC URLs?

A. jdbc:derby://localhost:1527/sample

B. //jdbc://derby://localhost:1527/sample

C. http://jdbc:mysql:localhost/sample

D. https://mysql.com:3306/sample

Question 13:

Which of the following code fragments can you use to get a JDBC connection?

A.
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
DriverManager.setClientInfo(p);
Connection c = DriverManager.getConnection(url);

B.
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
Connection c = DriverManager.getConnection(dburl);
c.setClientInfo(p);
c.connect();

C.
Properties p = new Properties();
p.setProperty("user", userid);
p.setProperty("password", pwd);
Connection c = DriverManager.getConnection(dburl, p);

D.
Connection c = DriverManager.getConnection(url);
c.connect(userid, pwd)

Question 14:

Consider the following code :

Connection connection = dataSource.getConnection();


PreparedStatement stmt = connection.prepareStatement("select * from
CUSTOMER where EMAILID=?");
stmt.setObject(1, "bob@gmail.com"); //LINE 10
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("EMAILID")); //LINE 12
}
connection.close();

Assuming that the query returns exactly 1 row, what will be printed when this code
is run?
(Assume that items not specified such as import statements, DB url, and try/catch
block are all valid.)

A. It will throw an exception at //LINE 12.

B. Compilation will fail due to //Line 12

C. It will print bob@gmail.com

D. It will print bob@gmail.com and then throw an exception.

Question 15:

You want to use a third party JDBC driver for a database. Which of the following
actions must you take to retrieve a Connection using that driver in your JDBC
program?

A. Put the driver jar in the class path.

B. Load the driver class using Class.forName

C. Create an instance of the Driver class using the new keyword.

D. Retrieve a connection using DriverManager.getConnection.

E. Load the driver class using Class.forName and then retrieve a connection using
DriverManager.getConnection.

Question 16:

Given:

Connection con = DriverManager.getConnection(dbURL);


con.setAutoCommit(false);
String updateString =
"update SALES " +
"set T_AMOUNT = 100 where T_NAME = 'BOB'";
Statement stmt = con.createStatement();
stmt.executeUpdate(updateString);
//INSERT CODE HERE

What statement can be added to the above code so that the update is committed to
the database?

A. con.setAutoCommit(true);

B. con.commit(true);

C. stmt.commit();

D. con.setRollbackOnly(false);

E. No code is necessary.

Question 17:

Identify correct statement(s) regarding the benefit of using PreparedStatement over


Statement.

A. PreparedStatement offers protection against SQL injection attacks.

B. PreparedStatement allows transactions to span across multiple databases.

C. PreparedStatement allows easier customization of joins at run time.

D. PreparedStatement allows BLOB and CLOB on all databases.

Question 18:

Given :
TEACHER table in the database with the following data:

TID(int) NAME(varchar) SUBJECT(varchar)


--------------------
1 Ally MATH
2 Bob MATH
1 Ally SCIENCE

and the following code:

try(
Connection c = ds.getConnection(); //assume that ds refers to a DataSource
PreparedStatement selectPS = c.prepareStatement("select TID, SUBJECT from
TEACHER where NAME = ?");
PreparedStatement insertPS = c.prepareStatement("insert into SUBJECT (NAME,
TID) VALUES (?, ?)");
)
{
selectPS.setString(1, "Ally");
ResultSet rs = selectPS.executeQuery();
while(rs.next()){
insertPS.setObject(1, rs.getObject(2), JDBCType.VARCHAR);
insertPS.setObject(2, rs.getObject(1), JDBCType.INTEGER);
insertPS.execute(); //1
}
}

Assuming that a SUBJECT table with no rows also exists, identify correct
statements.

A. Two PreparedStatement objects are created.

B. Three PreparedStatement objects are created.

C. Two SQL statements are executed.

D. Three SQL statements are executed.

E. An SQLException will be thrown because the ResultSet is not closed.

F. It will cause memory leaks because Connection, PreparedStatement, and ResultSet


objects are not closed.

Question 19:

Given:

Connection con = DriverManager.getConnection(dbURL);


con.setAutoCommit(false);
String updateString =
"update SALES " +
"set T_AMOUNT = 100 where T_NAME = 'BOB'";
Statement stmt = con.createStatement();
stmt.executeUpdate(updateString);
//INSERT CODE HERE

What statement should be added to the above code so that the update is committed to
the database?

A. stmt.commit();

B. con.commit();

C. stmt.commit(true);

D. con.commit(true);

E. No code is necessary.

Question 20:

Identify the correct statements:

A. A CallableStatement is used to call a stored procedure in the database while a


PreparedStatement is used to call a stored function.

B. A CallableStatement is easier to build and call from JDBC code than a


PreparedStatement.

C. A CallableStatement is preferred over PreparedStatement because it supports long


running transactions.

D. A CallableStatement is preferred over Statement because it supports long running


transactions.

Question 21:

Given:

Connection con = DriverManager.getConnection(dbURL);


String updateString =
"update SALES " +
"set T_AMOUNT = 100 where T_NAME = 'BOB'";
Statement stmt = con.createStatement();
stmt.executeUpdate(updateString);
//INSERT CODE HERE

What statement should be added to the above code so that the update is committed to
the database?

A. stmt.commit();

B. con.commit();

C. stmt.commit(true);

D. No code is necessary.

Question 22:

Which of the following are valid JDBC URLs?

A. jdbc.derby.localhost/sample

B. jdbc://mysql.com/sample

C. http://jdbc.mysql/sample

D. jdbc:oracle:thin:@localhost:1521:mydb

E. jdbc:mysql://192.168.1.10:3306/sample

F. jdbc:xderby:1106:sample

You might also like