This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Mock Question 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Mock Question 5" Watch "K&B Mock Question 5" New topic
Author

K&B Mock Question 5

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,

Can anyone explain why is the result of the following code is H) 4, 3


What is the result?

A). 1 1
B). 2 1
C). 3 1
D). 4 1
E). 1 3
F). 2 3
G). 3 3
H). 4 3

Why is Number preferred instead of Integer or Long in the case of System.out.print(doX(s,s) + " ");

Thanks


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Interesting! !! but i would also like to be cleared on...... how the output differs both on SCJP1.4 and and scjp1.5...


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Akhil...it wont even compile in JDK 1.4 (not SCJP1.4).
Try it!!
Tushar Patel
Greenhorn

Joined: Sep 27, 2005
Posts: 4
Following rules apply (in order):
1) Look for the most closely matching method without VarArg, auto-boxing/unboxing, if not, then
2) Most closely Matching method without VarArg but with auto-boxing/unboxing, still not, then
3) Most closely Matching method with VarArg and with Auto-boxing/unboxing

For

No matching method is found for (1). So (2) applies. Following method is most closely matching method, so it selected

For
, same rule applies, (2). So following is selected

So output is

Oh forgot,
Why is Number preferred instead of Integer or Long in the case of System.out.print(doX(s,s) + " ");
Apply OOP concept and you will find the answer. Hint: Number is superclass of Integer, Long, etc..

For example,


Output:
Above rule applies along with OOP.
[ September 27, 2005: Message edited by: Tushar Patel ]
Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
3) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Can it be decoupled like this ?
3a) Most closely Matching method with VarArg and without Auto-boxing/unboxing
3b) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Because in your last example (display 2), I fail to see any autoboxing / unboxing
[ September 27, 2005: Message edited by: Cyrano DeBergerac ]
Tushar Patel
Greenhorn

Joined: Sep 27, 2005
Posts: 4
Originally posted by Cyrano DeBergerac:
3) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Can it be decoupled like this ?
3a) Most closely Matching method with VarArg and without Auto-boxing/unboxing
3b) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Because in your last example (display 2), I fail to see any autoboxing / unboxing



Kind of, but you have to be very careful if you have both else you will get ambiguity. For example,

I think, you can have most closely mactching method using 3a) OR 3b), but not both as shown in example. So I think, 3) can be like this:
3) Most closely Matching method with VarArg and/or with Auto-boxing/unboxing

Please correct me. It is kind of interesting!!

Thanks Cyrano for your input.
Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
By John Smith ( previously known as Cyrano DeBergerac )

After few testing, i found out that the only presence of ... change completely the way to found the corresponding method:

void doX(int i) { ... );
void doX(Integer i) { ... };

doX(5);
doX(new Integer(5)); evoke their corresponding methods ( best match )

BUT

void doX(int i, String... strings) { ... };
void doX(Integer i, String... strings) { ... };

doX(5);
doX(new Integer(5)); compile time error for both

conclusion:
3) Find the ONLY POSSIBLE matching method with VarArg and any possible Auto-boxing / Unboxing. Compile-time error if more than one method is possible.


So in case like:
doX(long... longs) { ... };
doX(Number... numbers) { ... };

any possible call of long is include in Number... numbers, so any call with a list of parameters which can be transform in long... via conversion and AutoBoxing/Unboxing will throw a compiletime error.

For me, Java should launch a compile time error in the creation of the methods when one args list with varargs is completely include in the other or change their way to found the corresponding method with varargs.

Tell me if I made any mistake
[ September 27, 2005: Message edited by: John Smith ]
Tushar Patel
Greenhorn

Joined: Sep 27, 2005
Posts: 4
That's right John Smith(at the time of posting)!!

Therefore you cannot have both matching methods for 3a) AND 3b). You can have only one, one or the other, 3a) OR 3b).

Take Care,
[ September 27, 2005: Message edited by: Tushar Patel ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8879
    
    5
Hey John -

I can't quite make out what your last post is saying If you give it another try I'll let you know what I think... there's something about what you're saying that seems wrong, but since I can't quite understand your examples I'm not sure...

- Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
Thx to take care of us. Here the complete codes:

normal case: ( nothing new here )



As soon as u introduce the varargs, u cannot have multiple matching:



this lead to the 3rd case where some method are completely useless when the signature is completely hide by an other method. This case should send some compile time error at the declaration of methods. ( in my opinion ) or the algo to find the correct overloading method should be change.


[ September 27, 2005: Message edited by: John Smith ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8879
    
    5
Hi John,

As soon as u introduce the varargs, u cannot have multiple matching


I'm not sure what you're trying to do here, you haven't defined any methods that take just an int or just an integer? your invocations of doX are wrong?

- Bert
Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
1) Look for the most closely matching method without VarArg, auto-boxing/unboxing, if not, then
2) Most closely Matching method without VarArg but with auto-boxing/unboxing, still not, then
3) Find the unique Matching method with VarArg with or without Auto-boxing/unboxing OR compiletime error

the first code was just to show how it works without varargs ( step1 )
the second and third piece of code was about the step3 and how the JVM find the proper method if it has failed to find it with the step1 and 2.

If I put the perfect match in the second and third piece of code, i ll not be able to demonstrate how the JVM find the method on step3.

I am not sure about the meaning of your last post. I have define a way to access with only one int or one Integer + any number of String including 0.
In fact, the main problem is that I had define 2 of them
[ September 27, 2005: Message edited by: John Smith ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8879
    
    5
A-ha John,

Got it!

You're right about how this works, the good news is that this subtlety isn't on the exam!

Good catch!

- Bert
Eric Janssens
Ranch Hand

Joined: Sep 26, 2005
Posts: 53
Originally posted by Bert Bates:
the good news is that this subtlety isn't on the exam!


Hopefully, or I dont know how many years it will take to me to be ready
[ September 28, 2005: Message edited by: John Smith ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8879
    
    5
I promise!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Mock Question 5