Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 82
Question ID: UKOCP47710
Given structure of LOG table:
LOG (ID integer, MESSAGE varchar(1000), PRIMARY KEY (ID))
LOG table contains below records:
- 1001 Login Successful
- 1002 Login Failure
- 1003 Not Authorized
Given code of Test.java file:
- package com.udayankhattry.ocp;
- import java.sql.*;
- import java.util.*;
- public class Test {
- public static void main(String[] args) {
- var url = "jdbc:mysql://localhost:3306/ocp";
- var user = "root";
- var password = "password";
- var list = new ArrayList<Integer>();
- try (var con = DriverManager.getConnection(url, user, password);
- var stmt = con.createStatement()) {
- ResultSet rs = stmt.executeQuery("Select ID from LOG");
- while(rs.next()) {
- list.add(rs.getInt("ID"));
- }
- }
- System.out.println(list.size());
- }
- }
What is the result?
- A 0
- B 1
- C 2
- D 3
- E An exception is thrown at runtime
- F Compilation error
Reveal correct answer
Correct answer: F
Explanation
UKOCP47710:
DriverManager.getConnection(...), con.createStatement(), stmt.executeQuery(...), rs.next() and rs.getInt(...) all declare to throw SQLException, which is a checked exception and hence handle or declare rule applies in this case.
As main(String []) method neither declares to throw IOException nor a catch block is available for IOException, hence code doesn't compile.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
