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

Discussion

What will be the output of the program?

public class Item32
{
    void execute()
    {
        char letter = 'a';
        char upper = Character.toUpperCase(letter);
        System.out.println(upper + " " + (int) upper);
    }

    public static void main(String[] args)
    {
        Item32 demo = new Item32();
        demo.execute();
    }
}

  • A.a and its code 97
  • B.A but with the code 97
  • C.the code 65 printed twice
  • D.A and its code 65

Answer: D

toUpperCase returns the char A, which prints as the letter, while the explicit cast to int prints its code point 65. A char concatenated into a string shows the character; cast to int it shows the number. Lower case a would have been 97.

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

Post your comment

Please log in or register to post a comment.