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

Discussion

What will be the output of the program?

public class Probe34
{
    static void show()
    {
        Object o = new Object();
        String text = o.toString();
        System.out.println(text.startsWith("java.lang.Object@"));
    }

    public static void main(String[] args)
    {
        show();
    }
}

  • A.The program prints false
  • B.The program prints true
  • C.The program prints java.lang.Object
  • D.Compilation fails.

Answer: B

Object's default toString is the class name, an at sign, and the hash code in hexadecimal, so the text does begin with java.lang.Object@ and the test is true. The digits after it vary between runs, which is why the question asks about the prefix rather than the whole string.

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

Post your comment

Please log in or register to post a comment.