Machine-translated by Google. Questions are authored in English.
Top

Discussion

The program is run with java -ea Program104 - what will be the output?

public class Program104
{
    interface Limits { int MAX = 10; }

    public static void main(String[] args)
    {
        assert Limits.MAX == 10;
        System.out.println(Limits.MAX);
    }
}

  • A.The program prints 0
  • B.Compilation fails because interfaces cannot hold fields.
  • C.An AssertionError is thrown.
  • D.The program prints 10

Answer: D

A field declared in an interface is implicitly public, static and final, so Limits.MAX is a constant equal to 10. The assertion passes and 10 is printed. Interfaces may hold constants; what they cannot hold is mutable instance state.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.