Java OCA OCP Practice Question 1563

Question

What is the output of the following application?

package mypkg;// w  w w.  j a va  2s. com
abstract interface Printable {
   public void print();
}
public class Main {
   public static void main(String[] seaweed) {
      int distance = 7;
      Printable p = {
         @Override
         public void print() {
            System.out.print(distance);
         }
      };
      p.print();
   }
}
  • A. The application completes without printing anything.
  • B. 7
  • C. One line of this application fails to compile.
  • D. Two lines of this application fail to compile.


C.

Note

The main() method attempts to define an anonymous inner class instance but fails to provide the class or interface name, or use the new keyword.

The right-hand side of the assignment to the p variable should start with new Printable().

For this reason, Option C is the correct answer.

If the code was corrected with the proper declaration, it would output 7, and Option B would be the correct answer.




PreviousNext

Related