File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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???
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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:


Java Resources at www.wbrogden.com
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
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

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
 
Consider Paul's rocket mass heater.
 
subject: char literal
 
Similar Threads
Java char data type problem
Compile time constant
char literal
char question from khalid mughal exam
KHALID >> MOCK