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

Discussion

What will be the output of the program?

public class Case78
{
    public static void main(String[] args) throws Exception
    {
        int octal = 070;
        int hex = 0x1A;
        int plain = 70;
        System.out.println(octal + ", " + hex + ", " + plain);
    }
}

  • A.The program prints 70, 26, 70
  • B.The program prints 56, 26, 70
  • C.The program prints 70, 1, 70
  • D.The program prints 56, 1, 70

Answer: B

A leading zero marks an octal literal, so 070 is 7 times 8, which is 56, not 70. The 0x prefix marks hexadecimal, so 1A is 26. Without a prefix, 70 is just seventy, which is what makes the padded form so easy to misread. Accidental octal is a classic source of quiet arithmetic bugs.

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

Post your comment

Please log in or register to post a comment.