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

Discussion

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

public class Listing53
{
    public static void main(String[] args)
    {
        Object o = "text";
        assert o instanceof Integer;
        System.out.println("int");
    }
}

  • A.An AssertionError is thrown.
  • B.Compilation fails.
  • C.A ClassCastException is thrown.
  • D.The program prints int

Answer: A

instanceof asks a question rather than performing a conversion, so it never throws; it simply answers false for a String tested against Integer. The assertion condition is false and an AssertionError is thrown. A cast would have produced ClassCastException instead.

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

Post your comment

Please log in or register to post a comment.