Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan question

 
Steven Broadbent
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Broadbent
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I did not see that in S and B though.
 
Dan Gun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is one bit that I don't feel covered extensively. Never mind, there's always the JLS.....
 
Steven Broadbent
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven Broadbent
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, apparently this is for C compatability.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic