wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char literal" Watch "char literal" New topic

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.

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

Joined: Mar 22, 2000
Posts: 13036
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.

author of:
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
Thank you, Bill.
If it is so, I can only say that the question is unclearly
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

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link: http://aspose.com/file-tools
subject: char literal
It's not a secret anymore!