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

Discussion

Which line contains the error?

public class Sample34
{
    public static void main(String[] args)
    {
        char c = 'a';
        assert c + 1 == 'b' : "not next";
        System.out.println((char)(c + 1));
    }
}

  • A.assert c + 1 == 'b' : "not next";
  • B.No line here is in error.
  • C.char c = 'a';
  • D.System.out.println((char)(c + 1));

Answer: B

This one is fine. Arithmetic promotes the char to int, and the literal 'b' promotes too, so 98 == 98 holds and the assertion passes. The cast on the print is what turns the int back into a char. Nothing here depends on assertions being enabled.

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

Post your comment

Please log in or register to post a comment.