Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numerice value for letter 'a'

 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The numeric value for the letter 'a' is 97.
Which of these code fragments will successfully declare and initialize a variable of type 'char' with this value?




Select 2 correct options
a char ch = 'a';


b char ch = '\97';
It will give illegal escape sequence.

c char ch = '\u0097';
'\u' is used for Unicode values and '97' is not the unicode value of 'a'. ( Unicode value of 'a' is '61', which is actually '97' in decimal)

d char ch = 0x97;
0x means it is a hex number so It will make it 151.

e char ch = 97;



The answer is a and e. Can someone explain this?
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chiran Mathur:
The numeric value for the letter 'a' is 97.
Which of these code fragments will successfully declare and initialize a variable of type 'char' with this value?




Select 2 correct options
a char ch = 'a';


b char ch = '\97';
It will give illegal escape sequence.

c char ch = '\u0097';
'\u' is used for Unicode values and '97' is not the unicode value of 'a'. ( Unicode value of 'a' is '61', which is actually '97' in decimal)

d char ch = 0x97;
0x means it is a hex number so It will make it 151.

e char ch = 97;



The answer is a and e. Can someone explain this?

Chiran,
A char can broadly considered as a numeric type according to JLS
The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types (�4.2) are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double

SO a character can be declared in one of the following ways
  • char c = 'a' (if you are declaring the exact character , use single quotes)
  • char c = 97 (as char is a numeric type, you can also declare char c to be the ASCII value of 'a')
  • char c = '\uxxxx' where xxxx is the hexadecimal equivalent of the character's value , here it is 97...therefor its hex is 0x61 ...so the character can be '\u0061...the 0x part from hex is removed )

  • Hope you got my answer
    Sri
     
    Chiran Mathur
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So does one have to remember the ascii value of each character
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, they have told you it is 97 (implicitly decimal). You should however be able to convert that 97 into hexadecimal, octal, and binary.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic