File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloaded methods - Boone's Mock Question

 
Monisha Talwar
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 29: What is the result of executing the following code:

Select the one right answer.
a. 1
b. 2
c. 3
d. 4
e. 5
Ans: C
Explination:
Question 29: c. The method with all double parameters is actually the only version of test() that the Java Virtual Machine can legally coerce the numbers to. The reason the other versions of test() are not invoked is that at least one of the parameters would have to be automatically coerced from a type with greater accuracy to a type with less accuracy, which is illegal. (See chapter 9.)
I still did not understand...Infact I thought the ans would be (a).
 
Monisha Talwar
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should have thought of this earlier.
The 3rd arg is by default an int and so it can be passed into the 3rd method which takes all 3 doubles. Can't go to the 1st method which has the 3rd arg as short.
So the double, long and int are passed to the the double-double-double method...Right?
 
Carl Sampson
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think C is correct because it is the only method where the parameters could be widened in all cases. In all of the other cases the parameter would have to be narrowed which the compiler won't do for you without an explicit cast.
 
preeti khane
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah the main thing to remeber here is that a number no matter if it is the range of short/ byte/ char will still be taken as an int
unless specifically stated
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic