Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.205 :

Posted: Mon Jul 25, 2011 2:00 pm
by ETS User
In this program,

Code: Select all

public class TestClass
{
    char c;
    public void m1()
    {
        char[ ] cA = { 'a' , 'b'};
        m2(c, cA);
        System.out.println( ( (int)c)  + ", " + cA[1] );
    }
    public void m2(char c, char[ ] cA)
    {
        c = 'b';
        cA[1] = cA[0] = 'm';
    }
    public static void main(String args[])
    {
        new TestClass().m1();
    }
}
c is of type char... when it is casted to int in m1() why is it printing 0(zero) instead of 98(the ascii value of 'b')....

Re: About Question com.enthuware.ets.scjp.v6.2.205 :

Posted: Mon Jul 25, 2011 7:43 pm
by admin
Because the 'c' that m2 is changing (it is changing method parameter) is not the 'c' that m1 is printing (it is printing the instance field).

You might want to read about pass by value and pass by reference from a book.

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.205 :

Posted: Sat Dec 03, 2011 6:35 pm
by ETS User
This question is badly worded:
I knew about covariant return type and had made a note as such.

"An overriding method must have a same parameter list and the same return type as that of the overridden method."

However it is misleading in the manner that it is safe to assume that
"Class_B extends Class_A" = Class_B IS-A Class_A .... therefore what the question is actually asking me is

Code: Select all

public Class_A doStuff() { // code 
return new Class_A();
}

// wrong override
public Unrelated_Class_C doStuff() { // code 
return new Class_C();
}
It is very annoying to get these wrong .... what I would like to know is, do they ask this type of trick questions on OCP JP?

Re: About Question com.enthuware.ets.scjp.v6.2.205 :

Posted: Sun Dec 04, 2011 4:38 pm
by admin
Your comment doesn't seem relevant to this question. Could you please check the question id so that we can investigate the issue.

In general, yes, you can expect tricky questions in the exam, which expect you to be a human compiler.