Java OCA OCP Practice Question 1777

Question

What is the output of the following application?

package mypkg;/*from   www. jav  a2 s  .c  o  m*/

import java.io.Closeable;

class Login implements Closeable {
   public void close() {}
   public String get() {return "Hello";}
}
class Logout implements AutoCloseable {
   public void close() {}
   public void send(String message) {
      System.out.print(message);
   }
}
public class Main {
   public static void main(String... hands) {
      try (Login r = new Login();
         Logout w = new Logout();) {
         w.send(r.get());
      }
   }
}
  • A. Hello
  • B. The code does not compile because of the Login class.
  • C. The code does not compile because of the try-with-resources statement.
  • D. None of the above


A.

Note

The application compiles without issue and prints Hello, making Option A the correct answer.

The Login and Logout classes are both correctly implemented, with both overridden versions of close() dropping the checked exception.

The try-with-resources statement is also correctly implemented for two resources and does not cause any compilation errors or runtime exceptions.

Note that the semicolon (;) after the second resource declaration is optional.




PreviousNext

Related