Multiple choice technology web technology

Given: 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?

  1. -124

  2. -134

  3. -424

  4. -434

  5. -444

  6. Compilation fails

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

aa (A[]) matches sifter(A[]...) via varargs, resolving to "1" if no other method matches, but varargs is lowest priority. ba (B[]) matches sifter(B[]...) and sifter(B[] b1). sifter(B[] b1) is a direct match, so it's chosen, yielding "3". sifter(7) (int) boxes to Integer and matches sifter(Object o), yielding "4". Total is s += "1" (for aa?), wait. sifter(aa): is there a better match? A[] matches Object o directly (non-varargs) or A[]... (varargs). Since A[] is an object, sifter(Object) (non-varargs) is chosen over sifter(A[]...) (varargs). So sifter(aa) prints "4". sifter(ba) matches sifter(B[] b1) (exact non-varargs match), printing "3". sifter(7) matches sifter(Object), printing "4". Output is -434.