KnowledgeBoat Logo

Computer Applications

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);

Java

Java Library Classes

ICSE Sp 2025

1 Like

Answer

X    b

Working

Step-by-Step Execution:

char ch = 'B';

  • ch is 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) chr converts 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 integer 88 back to a character.
    • ASCII value 88 corresponds to the character 'X'.
  • + "\t" + adds a tab space between the characters.
  • chr: The value of chr is 'b'.

Answered By

1 Like


Related Questions