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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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')....

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ETS User

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

Post 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?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 241 guests