Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char literal

 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the Mughal's mock exam:
The 8859-1 character code for the uppercase letter A is 65. Which of these code fragments declare and initialize a variable of type char with this value?
A. char ch=65;
B. char ch='\65';
C. char ch='\0041';
D. char ch='A';
E. char ch="A";
My answer is: A, D. But the correct answer is only D.
WHY???
I compiled and run the following code:
public class child1
{
public static void main(String[] args)
{
char c1=65;
char c2='A';
System.out.println(c1+" "+c2);
}
}
It prints "A A", as I expected.
Please explain me why the answer A is wrong???

 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ego,
You are correct, the answers are a) and d). Please find the correction in errata HTML page Do a: Find>728.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
[This message has been edited by James Baud (edited March 22, 2001).]
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saw it. Thank you.
 
Sarang Gurao
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friend,
Its a nice question. this is really a tricky to assign a value by giving a integer value to it. u know this can be done in three ways. following way i do the assignment and i got the following result.
char c1=65;
System.out.println(c1); //Give Value A
System.out.println(+ c1);//Give Value 65 (Ascii Value A)
char c2='A';
System.out.println(c2);//Give Value A
System.out.println(+ c2);//Give Value 65
char c3=65;
System.out.println(c3);//Give Value A
System.out.println(+ c3);//Give Value 65
char c4='\65';
System.out.println(c4);//Give Value 5 (65 is octal value of decimal 5 )
System.out.println(+ c4);//Give Value 53(Ascii Value of decimal 5)
char c5='\u0041';
System.out.println(c5);//Give Value A
System.out.println(+ c5);//Give Value 65
char c6='A';
System.out.println();
System.out.println(c6);//Give Value A
System.out.println(+ c6);//Give Value 65

// char c7="A";
Will not compile because String cannot assign to char.
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so c) is also right???
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
C) is \0041 but not \u0041
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic