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

kathy sierra self test question?

 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is it from?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseCodeTags for posting code. Don't use the quote tags.
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book? Kathy Sierra has written and co-authored several books.
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic