Java OCA OCP Practice Question 1799

Question

What is the output of the following application?

package db;//from   ww  w .  j a  v  a2 s.co m
import java.io.*;
import java.sql.*;
public class Main {
   static class Login implements Closeable {
      public void close() throws SQLException {
         System.out.print("2");
      }
      public void write(String data) {}
      public String read() {return null;}
   }
   public static void main(String... files) throws Exception {
      try (Login l = new Login()) {
         // TODO: Decide what to read/rite
      } finally {
         System.out.print("1");
      }
   }
}
  • A. 12
  • B. 21
  • C. The code does not compile because of the Login class.
  • D. The code does not compile because of the try-with-resources statement.


C.

Note

The Closeable interface defines a close() method that throws IOException.

The overridden implementation of Login, which implements Closeable, declares a SQLException.

This is a new checked exception not found in the inherited method signature.

Therefore, the method override is invalid and the close() method in Login does not compile, making Option C the correct answer.




PreviousNext

Related