Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan question 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 "Dan question" Watch "Dan question" New topic
Author

Dan question

Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
Question 14
class MCZ24 {
public static void main (String[] args) {
char a = 061; // 1
char b = '\61'; // 2
char c = '\061'; // 3
char d = 0x0031; // 4
char e = '\u0031'; // 5
System.out.print(""+a+b+c+d+e);
}}
A compile-time error is generated at which line?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Answer given is :- f none of the above
All of the declarations are legal. The first three ( 061, '\61', '\061' ) are declared in octal format. The fourth (0x0031) is declared as a hexadecimal literal. The fifth ('\u0031') is a Unicode escape sequence.
I don't see how the first three are octal declarations - don't they all
have form '\uXXXX'


"....bigmouth strikes again, and I've got no right to take my place with the human race...."<p>SCJP 1.4
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
char a = 061; // 1
char b = '\61'; // 2
char c = '\061'; // 3
1) It is valid octal no.
2 & 3) We can use the escape sequence \ddd to specify a character literal by octal value, where each digit d can be any octal digit (0-7).
4 & 5) are hexadecimal.
Hope i made it clear.


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
Thanks, I did not see that in S and B though.
Dan Gun
Greenhorn

Joined: Aug 06, 2003
Posts: 26
Yes, this is one bit that I don't feel covered extensively. Never mind, there's always the JLS.....


SCJP 1.4
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
Just looked through M and R and didn't see it, though
the form '\ddd' looks familiar.
I'm sure I read that the maximum allowed is 377 in octal, and this seems to be so - for instance char c = '\477'
gives compiler error : unclosed character literal
377 octal is 7+ 56 + 192 = 255 decimal.
anyone shed any light on this? Maybe it is worth adding to the list of exam gotchas.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Octal escapes are covered in
section 3.10.6 of the Java Language Specification.
The following indicates that the largest octal escape is \377.

OctalEscape:
\ OctalDigit
\ OctalDigit OctalDigit
\ ZeroToThree OctalDigit OctalDigit
OctalDigit: one of
0 1 2 3 4 5 6 7
ZeroToThree: one of
0 1 2 3


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
Thanks all, apparently this is for C compatability.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dan question
 
Similar Threads
Dan's question:Topic. Literals
char literals
From Dan's exam
dan exam dought 2
char declaration