This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E" Watch "SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E" New topic
Author

SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Hi all ,
In SCJP 1.6 K &B book chapter 2 ,Self Test Question 15----
I think the answer should be E -444 but it says D ie -434.

Please somebody suggest me on this.




Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I don't have the book at hand. Could you post the question and possible answers ?


JDBCSupport - An easy to use, light-weight JDBC framework -
Dj Lalotra
Greenhorn

Joined: Sep 14, 2009
Posts: 8
Hi Lucky,

First of all ,could you please make it a habit of typing or doing a copy+paste of you code in the post itself ,so that it would become easier for others to answer your queries(and everyone might not be having a K&B).

Anyways back to the point.
The answer given in the book is perfectly alright.
The following methods will never get called due to mismatching argument types:
static void d(A[]... a2); and
static void sifter(B[]... b1);

But when you perform :
sifter(aa);
it will invoke static void sifter(Object o) as there is no exact match for the type aa(reference of type A[]) .The polymorphism will come into picture and the type aa will automatically cast into supreme superclass Object's object and static void sifter(Object o) will get called.So it will pick -4.

When you do:
sifter(ba);
it will invoke static void sifter(B[] b1) ,this is a clear type match.So the value 3 is picked and the value of s will become -43 at this level.

and when you do:
sifter(7);
it will call static void sifter(Object o).The reason for this method being called is also polymorphism.The primitive int value of 7 will automatically convert into object of type INTEGER and then to Object finally.Autoboxing & Unboxing feature added from JAVA 5.0(Tiger).
The value 3 is picked and the final value of s will become -434 ,and hence the answer.

Hope this would help.

cheers!
DJ.





Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Code from example is :

class A {}
class B extends A{}

public class ComingThru{

static String s="-";
public static void main(String[] args)
{

A[] aa=new A[2];
B[] ba=new B[2];
sifter(aa);

sifter(ba);
sifter(7);
System.out.println(s);


}

static void sifter(A[]... a2){s+="1";}
static void sifter(B[]... b1){s+="2";}
static void sifter(B[] b1){s+="3";}
static void sifter(Object o ){s+="4";}
}

What is the result ;

A -124
B -134
C -424
D -434 //Answer is this D
E -444 //my guess is E
F Compilation fails
Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Hi Lucky,



Dj has very well explained the concept behind the code. I would like to add a little more to it.

The sifter methods with the var-args are perfectly alright however they would be the last methods to be called in any code.

When you invoke sifter with bb parameter it will be matched with the method on Line 4 and not Line 5 as Line 4 is the exact match for sifter(bb) as it takes a parameter of an array of B type however for sifter(aa) and sifter(7) invocations the method that would be matched would be the one on Line 5 as both pass the IS-A test for Object.

sifter(aa) -> Here aa is an Array which is an Object.
sifter(7) -> Here int is Boxed to Integer and Integer is an Object.

Suppose Line 1, Line 4 and Line 5 were commented in that case the methods invoked for
sifter(aa) and sifter(bb) would be the ones with the var-args parameters.

The only thing you have to remember is that the var-args methods would be the last ones to be invoked as they could handle any number of invocations of A[] or B[].


Thanks,
Usha Atwal


_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Java Certification Exam Mock Tests

SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5

With 100% Money Back Guarantee!




Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
Dhirendra Ku
Greenhorn

Joined: Aug 29, 2009
Posts: 9
Hi Lucky DJ and Atwal has explained very well and I want to give you one suggestion whenever you want to post any doubt regarding code examples then at least once you should run/compile that code.


Thanks & Regards,

Dhirendra Kumar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SCJP 1.6 Chapter2 Question 15- Answer -> is D ,I think it should be E