2(vii)
Predict the output of the following code snippet:
char ch='B', char chr=Character.toLowerCase(ch); int n=(int)chr-10; System.out.println((char)n+"\t"+chr);
Show SolutionHide Solution↓
X b
Step-by-Step Execution:
char ch = 'B';
chis assigned the character'B'.
char chr = Character.toLowerCase(ch);
Character.toLowerCase(ch)converts'B'to its lowercase equivalent'b'.chr = 'b'.
int n = (int) chr - 10;
(int) chrconverts the character'b'to its ASCII value.- ASCII value of
'b'is 98. n = 98 - 10 = 88.
System.out.println((char) n + "\t" + chr);
(char) n: Converts the integer88back to a character.- ASCII value
88corresponds to the character'X'.
- ASCII value
+ "\t" +adds a tab space between the characters.chr: The value ofchris'b'.