Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 10
Given structure of EMPLOYEE table:
EMPLOYEE (ID integer, FIRSTNAME varchar(100), LASTNAME varchar(100), SALARY real, PRIMARY KEY (ID))
EMPLOYEE table contains below records:
- 101 John Smith 12000
- 102 Sean Smith 15000
- 103 Regina Williams 15500
- 104 Natasha George 14600
Given code of Test.java file:
- package com.udayan.ocp;
- import java.sql.*;
- public class Test {
- public static void main(String[] args) throws Exception {
- String url = "jdbc:mysql://localhost:3306/ocp";
- String user = "root";
- String password = "password";
- String query = "Select ID, FIRSTNAME, LASTNAME, SALARY FROM EMPLOYEE ORDER BY ID";
- try (Connection con = DriverManager.getConnection(url, user, password);
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ) {
- ResultSet rs = stmt.executeQuery(query);
- rs.afterLast();
- while (rs.previous()) {
- rs.updateDouble(4, rs.getDouble(4) + 1000);
- rs.updateRow();
- }
- rs = stmt.executeQuery(query);
- while(rs.next()) {
- System.out.println(rs.getDouble(4));
- }
- }
- }
- }
Also assume:
URL, username and password are correct.
SQL query is correct and valid.
The JDBC 4.2 driver jar is configured in the classpath.
What will be the result of compiling and executing Test class?
-
A
13000.0
16000.0
16500.0
15600.0 -
B
12000.0
15000.0
15500.0
15600.0 -
C
12000.0
15000.0
15500.0
14600.0 -
D
15600.0
16500.0
16000.0
13000.0 -
E
13000.0
15000.0
15500.0
14600.0
Reveal correct answer
Correct answer: A
Explanation
Given query returns below records:
101 John Smith 12000
102 Sean Smith 15000
103 Regina Williams 15500
104 Natasha George 14600
'rs.afterLast();' moves the cursor just after the last record.
'rs.previous()' inside while loop moves the cursor from last to first and the codes inside while loop increment the salary of each record by 1000.
'rs.updateRow();' makes sure that salary is updated in the database.
Next while loop simply prints the updated salaries on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
