Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1399 :

Posted: Tue Apr 12, 2016 9:32 am
by chmelvv

Code: Select all

import java.util.*;
public class ClassnameTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("mrx");
        String s = sb.toString();
        list.add(s);
        System.out.println(s.getClass());
        System.out.println(list.getClass());
    }
}
========= Console output:
class java.lang.String
class java.util.ArrayList

Process finished with exit code 0
==========
So Explanation correct, but answer in test - NOT.
"The getClass method always returns the Class object for the actual object on which the method is called irrespective of the type of the reference."

Re: About Question enthuware.ocajp.i.v8.2.1399 :

Posted: Tue Apr 12, 2016 9:37 am
by admin
Why do you think the answer is incorrect? How does the output that you've shown prove otherwise?
-Paul.

Re: About Question enthuware.ocajp.i.v8.2.1399 :

Posted: Thu Apr 14, 2016 11:15 pm
by chmelvv
Because as the right answer exam system proposes:
class java.lang.String
class java.util.List


What output proves? Compiler output is truth in the last instance.

Re: About Question enthuware.ocajp.i.v8.2.1399 :

Posted: Fri Apr 15, 2016 1:11 pm
by admin
I see that option 3 is marked correct and it says:

class java.lang.String
class java.util.ArrayList