This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Mock Exam Errata and the fly likes char literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "char literal" Watch "char literal" New topic
Author

char literal

ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
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

Joined: Jan 06, 2001
Posts: 60
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).]


<B>~James Baud</B><P>He who asks, is a fool for five minutes;<BR>but, he who does not ask, remains a fool forever. (Chinese proverb)
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
Saw it. Thank you.
Sarang Gurao
Greenhorn

Joined: Mar 14, 2001
Posts: 13
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

Joined: Jan 08, 2001
Posts: 2164
so c) is also right???
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
No.
C) is \0041 but not \u0041
 
Consider Paul's rocket mass heater.
 
subject: char literal
 
Similar Threads
char question from khalid mughal exam
KHALID >> MOCK
Java char data type problem
char literal
Compile time constant