Choose the correct answers to the questions from the given options. (Do not copy the questions, write only the correct answers.)
The output of the statement: System.out.println(Character.toUpperCase('b') + 2); is:
Topic: Character methods and type promotion
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
(c) 68.
Work outward, one step at a time. Character.toUpperCase('b') returns the char 'B'. The + here is arithmetic, not string concatenation, because neither operand is a String — so 'B' is promoted to its ASCII code, 66. Then 66 + 2 = 68, and println prints the int 68, giving option (c).
The trap is option (d) 98, the code for lowercase 'b': it is there for anyone who forgets the toUpperCase call. Option (a) 66 is there for anyone who forgets the + 2. Option (b) 100 is 98 + 2 — both mistakes at once.
Remember the rule this tests: char + int gives an int in Java, not a char. If you want the letter 'D' printed instead of 68 you must cast it back — System.out.println((char)(Character.toUpperCase('b') + 2)).
68.