wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Dan's Mock (Dec. 4) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Dan Watch "Question about Dan New topic

Question about Dan's Mock (Dec. 4)

Emiko Hori

Joined: Nov 06, 2002
Posts: 6
I've been working on Dan's Mock Exam Questions (excellent site, BTW) and came up with a quesiton.
On Comprehensive Exam 3 No. 3, the question was:
Which of the following statements will generate a compile-time error?
a. Short s1 = new Short(1);
b. Short s1 = new Short('1');
c. Short s1 = new Short(1L);
d. Short s1 = new Short('b' - 'a');
e. Short s1 = new Short(1.0);
f. Short s1 = new Short((short)1 - (short)2);
g. Short s1 = new Short((byte)1);
h. Short s1 = new Short((short)1);
i. None of the above.
Dan's answer was b, c, d, e, f. When I compiled it, answer "a" also got a compiler error because we are passing an int. According to Dan's explanation:
"The Short wrapper class has two constuctors. One accepts an argument of type short and the other accepts a String. There is no implicit widening conversion from the primitive types char, int, long, float or int to type short so those types can not be passed as an argument to the constructor without an explicit cast to the primitive short type. Please note that if both operands of a binary arithmetic expression are of type byte, char or short then both are implicitly widened to type int and the result of the expression is of type int."
In that case, answer "a" has to be a compiler error as well, right? Please correct me if I'm wrong. I'm new to Java! Thanks so much for help.
Keen Chen
Ranch Hand

Joined: Nov 12, 2002
Posts: 47
I think u are right.

SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
You are correct. Thank you for using my exams and for pointing out that error. I'll upload the correction in a few minutes.

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

Joined: Nov 17, 2002
Posts: 115
That was a very interesting question (John-speak for 'I would have got that one wrong')
Java will do an implicit narrowing conversion on assignment, but, to quote the JLS:
Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion (�5.2).

That means that some of the constant expressions in the constructor arguments could be assigned to a primitive short variable, even though they cannot be used as argument values for the Short constructor:

SCJP 1.4
Emiko Hori

Joined: Nov 06, 2002
Posts: 6
Dan, thanks for the reply as well as your exams!
I agree. Here's the link: http://aspose.com/file-tools
subject: Question about Dan's Mock (Dec. 4)