• 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???
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be that the exact text of the question asked for the correct way to write a char literal - not exactly the same as the right way to initialize the char variable.
Option A uses an int literal - which the compiler recognizes as a legal value for a char and so does not object.
Bill

------------------
author of:
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Bill.
If it is so, I can only say that the question is unclearly
expressed.
It will be a disaster for the people (like me), who don't
speak English as their mother tongue, if such questions
appear on the real exam.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ego,
The real exam questions are straight-forward and concise. They do not contain trick wording or ambiguous phrases. You should be fine as long as you understand the fundamentals of the Java language.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic