File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mock exam question" Watch "mock exam question" New topic

mock exam question

bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
I came across this question in one of the mock exams.
I marked 2 as my ans, can anyone pls explain why the ans is 3 and not 2.
question from mock exam
public class AQuestion
public void method(Object o)
System.out.println("Object Verion");
public void method(String s)
System.out.println("String Version");
public static void main(String args[])
AQuestion question = new AQuestion();
1.The code does not compile.
2.The code compiles cleanly and shows "Object
3.The code compiles cleanly and shows "String
4.The code throws an Exception at Runtime.
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
I checked the null instnaceof with String and Object . It says it is neither String nor Object; what is the difference between the null passed to a method call and when checking directly for its runtime type ? This code prints "String version null Not String, Not Object" ?? why ?
class AQuestion{
public void method(Object o){
System.out.println("Object Verion"+o);
public void method(String s){
System.out.println("String Version"+s);
public static void main(String args[]){
AQuestion question = new AQuestion();
if( null instanceof String ){
System.out.println("String Version");
if( null instanceof Object){
System.out.println("Object Version");
System.out.println("Not String, Not Object");
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
hi Dan,
if there were a String conversion from every other type to String type, even a null... then how could this code below prints.. "int version"
it should print "string version" right,... pls explain... am i missing something..

please use the [code][/code] tags when showing code. visit <a href="" target="_blank" rel="nofollow"></a> ,for more details
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
Dan, I think you�re wrong. Just because there is a conversion from any other type to String, it doesn�t mean that it will be used all the time. What happens in bani�s problem is that the compiler chooses the most specific method to run, like this:

The precise definition is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:
  • T can be converted to U by method invocation conversion.
  • Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.

  • here�s an example from the same section of the jls that shows this exact thing:
    Consider the example:

    This example produces an error at compile time. The problem is that there are two declarations of test that are applicable and accessible, and neither is more specific than the other. Therefore, the method invocation is ambiguous.
    If a third definition of test were added:
    static void test(ColoredPoint p, ColoredPoint q) {
    System.out.println("(ColoredPoint, ColoredPoint)");
    then it would be more specific than the other two, and the method invocation would no longer be ambiguous.

    So since String is more specific than Object, because String is a sub-class of Object, the compiler will choose the String version method.
    In srinivas� example, since the argument is an int it chooses the int version, following the same logic.
    Hope that helps, if you have anymore doubts please let me know.
    [ July 16, 2002: Message edited by: Francisco A Guimaraes ]

    Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="" target="_blank" rel="nofollow">here</a> to see an example.
    bani kaali
    Ranch Hand

    Joined: May 05, 2002
    Posts: 42
    Thank you Francisco and Dan,for the detailed explainations.
    Dan Chisholm
    Ranch Hand

    Joined: Jul 02, 2002
    Posts: 1865
    Yes, my previous two posts were definitly wrong. I think the easiest way to verify that java just picks the lowest subclass is with the following program.

    The output is B.
    The following code illustrates that Java has no preference for promoting null to a String over any other type.

    The above code will not compile due to the ambiguity.
    Of course, if you remove the ambiguity then it compiles and prints String.

    Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
    zarina mohammad
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 104
    i found another example in which the most specific method is called.

    in this case " version" is printed since FileNotFoundException class is subclass of IOException which in turn is subclass of Object. hence the method with FileNotFoundException is called which the more specific.
    I agree. Here's the link:
    subject: mock exam question
    It's not a secret anymore!