aspose file tools*
The moose likes Beginning Java and the fly likes kathy sierra self test question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "kathy sierra self test question?" Watch "kathy sierra self test question?" New topic
Author

kathy sierra self test question?

Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
3. class A { }
4. class B extends A { }
5. public class ComingThru {
6. static String s = "-";
7. public static void main(String[] args) {
8. A[] aa = new A[2];
9. B[] ba = new B[2];
10. sifter(aa);
11. sifter(ba);
12. sifter(7);
13. System.out.println(s);
14. }
15. static void sifter(A[]... a2) { s += "1"; }
16. static void sifter(B[]... b1) { s += "2"; }
17. static void sifter(B[] b1) { s += "3"; }
18. static void sifter(Object o) { s += "4"; }
19. }




What is the result?
A. -124
B. -134
C. -424
D. -434
E. -444
F. Compilation fails


can someone tell me what is happening in this program.....i couldn't understand this program at all....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Where is it from?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And please UseCodeTags for posting code. Don't use the quote tags.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
Campbell Ritchie wrote:Where is it from?


its from kathy sierra book.....the question is from 2nd chapter(object orientation) self test.....question no 15 on page 169.....please explain the code as i couldn't understand it......
Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
Answer:
® ✓ D is correct.

In general, overloaded var-args methods are chosen last. Remember that arrays
are objects. Finally, an int can be boxed to an Integer and then "widened" to an Object.
A, B, C, E, and F are incorrect based on the above information.


Can someone please explain how i am confused..???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Which book? Kathy Sierra has written and co-authored several books.
Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
Campbell Ritchie wrote:Which book? Kathy Sierra has written and co-authored several books.



Sun Certified Programmer for Java 6 study Guide for exam 310 065

by Kathy sierra and Bert Bates
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

what is so confusing about it.
1.Remember one rule Array is an Object in Java(everything is an object).
2.widening bit boxing, and boxing bit vararg. So in general vararg is last option when no other option is present for compiler.
3.You can never widen and then box because of this IS-A test fail, But you can box and then widen it.

here in this example you have created 2 array as


Now it checks for method which takes array as an argument but for array of A it doesn't found it. so it use method which takes Object and hence execute method. (Here widening bit boxing and vararg)


If this method is not exist then it execute



In second method for sifter(ba) it found method as


so print as 3

In last method there is no matching parameter for int so it execute


Life is easy because we write the source code.....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Pramod P Deore wrote: . . . In last method there is no matching parameter for int . . .
So you get boxing to an int, followed by widening to an Object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: kathy sierra self test question?
 
Similar Threads
Study Guide 6 -- Chapter2 Self Test Question 15 on page 181.
kathy sierra self test question..???
Which overloaded method is called?
SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E
Var Args,Arrays and overloading