What is the result of compiling and executing this code?

ITEM Table

* ID, INTEGER: PK

* DESCRIP, VARCHAR(100)

* PRICE, REAL

* QUALITY, INTEGER

And given the code fragment (assuming that the SQL query is valid):

try {

String query = “SELECT * FROM Item WHERE ID=110”;

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next ()) {

System.out.println(“ID: ” + rs.getInt(“Id”));

System.out.println(“Description: ” + rs.getString(“Descrip”));

System.out.println(“Price: ” + rs.getDouble(“Price”));

System.out.println(“Quantity: ” + rs.getInt(“Quantity”));

}

} catch (SQLException se) {

System.out.println(“Error”);

}

What is the result of compiling and executing this code?

ITEM Table

* ID, INTEGER: PK

* DESCRIP, VARCHAR(100)

* PRICE, REAL

* QUALITY, INTEGER

And given the code fragment (assuming that the SQL query is valid):

try {

String query = “SELECT * FROM Item WHERE ID=110”;

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next ()) {

System.out.println(“ID: ” + rs.getInt(“Id”));

System.out.println(“Description: ” + rs.getString(“Descrip”));

System.out.println(“Price: ” + rs.getDouble(“Price”));

System.out.println(“Quantity: ” + rs.getInt(“Quantity”));

}

} catch (SQLException se) {

System.out.println(“Error”);

}

What is the result of compiling and executing this code?

A.
An exception is thrown at runtime

B.
Compile fails

C.
The code prints Error

D.
The code prints information about Item 110

Explanation:
The connection conn is not defined. The code will not compile.



Leave a Reply 10

Your email address will not be published. Required fields are marked *


Francesco

Francesco

The explanation is ok but the correct answer is C,not B

admin

admin

Yes, it should be C.

yakov

yakov

Why the correct answer is C, not B ?

Charlie

Charlie

The code looks good, not considering the initialization of conn and the close of the resources.

Serg

Serg

+1 (The code looks good, not considering the initialization of conn and the close of the resources.)

With out connect initialization answer “B”.

hieu

hieu

assume conn intialization => D true;

Jav

Jav

Tricky question, the field on the database definition is quaLity, on the code the field is quaNtity, I think it generates an error on runtime based on that fact

Tony

Tony

If conn is not innitialization: Compile fails
Assume yes: Rum time error because of this line: System.out.println(“Quantity: ” + rs.getInt(“Quantity”)); Quantity QUALITY

Tony

Tony

but “assuming that the SQL query is valid” => conn is ok. Correct answer is A

Michael

Michael

not a good question~