Home / Questions / State the values of n and ch. char c = 'A'; int n = c + 1; char ch = (char)n;
Explanatory Question

State the values of n and ch.

char c = 'A';
int n = c + 1;
char ch = (char)n;

👁 114 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Let's analyze the code step by step to determine the values of n and ch.


char c = 'A';
int n = c + 1;
char ch = (char)n;

  1. Value of c:

    • c is assigned the character 'A'.
    • The Unicode (or ASCII) value of 'A' is 65.
  2. Calculation of n:

    • n is calculated as c + 1.
    • Given that c is 65, the calculation will be 65 + 1, which results in 66.

    Thus, n will be 66.

  3. Value of ch:

    • ch is assigned (char)n.
    • The value of n is 66, so ch will be (char)66.
    • The character corresponding to the Unicode value 66 is 'B'.

Summary:

  • The value of n is 66.
  • The value of ch is 'B'.