Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 9)
9.
Which three are valid declarations of a char?
  1. char c1 = 064770;
  2. char c2 = 'face';
  3. char c3 = 0xbeef;
  4. char c4 = \u0022;
  5. char c5 = '\iface';
  6. char c6 = '\uface';
1, 2, 4
1, 3, 6
3, 5
5 only
Answer: Option
Explanation:

(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of a character.

char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always start with a '\u'.

char c4 = \u0022; is wrong because the single quotes are missing.

char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts with '\i' rather than '\u'.

Discussion:
24 comments Page 1 of 3.

Adarsh said:   1 decade ago
How can a char variable carry integer value?

Sai said:   1 decade ago
I too have the same doubt.. Can anyone say me the correct answer for it..

Anil said:   1 decade ago
I tried the above initializations 1,3,6.
But while displaying values, it displays only ? for statements..
Why it displaying like that......

Any one please post the answer for this...

Manju said:   1 decade ago
How char carry the int values?

Kedar said:   1 decade ago
In Java, lower priority data types are converted to its nearest higher priority data types implicitely so that the some of the values of char are also of the int.
(1)

Sandip said:   1 decade ago
What is character literal? and.

Why char c2='face'; is wrong answer?

Please justified it.

Basha said:   1 decade ago
@Sandip

c2='a' it is represntaion of character.

But, in the given question c2='face' trying to assign the string value to character variable.

Chandu said:   1 decade ago
Watz actually unicode means?can anyone help me out of this ?

Cybog said:   1 decade ago
Why c='\uface' is legal and c='face' is legal? please explain. Thanks is advance.

Harshit kumar said:   1 decade ago
What is actually unicode? please explain it.


Post your comments here:

Your comments will be displayed after verification.