• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Numerice value for letter 'a'

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So does one have to remember the ascii value of each character
     
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic