Java OCA OCP Practice Question 1761

Question

What is the output of the following application?

package mypkg;//  ww  w  .  ja va2s .  co  m
import java.io.Closeable;
public class Main {
  class Printer implements Closeable {  // r1
     public void print() {
        System.out.println("This just in!");
     }
     public void close() {}
  }
  public void printHeadlines() {
     try {Printer p = new Printer()} {  // r2
        p.print();
     }
  }
  public static void main(String[] headlines) {
     new Main().printHeadlines();  // r3
  }
}
  • A. This just in!
  • B. The code does not compile because of line r1.
  • C. The code does not compile because of line r2.
  • D. The code does not compile because of line r3.


C.

Note

The class does not compile because in line r2, brackets {} are used instead of parentheses () in the try-with-resources statement, making Option C the correct answer.

If this line was fixed to use parentheses(), then the rest of the class would compile without issue and print This just in! at runtime, making Option A the correct answer.




PreviousNext

Related