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

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.703 :

Post by ETS User »

Hi guys.
I am working with this question and it seems that there is a mistake.

Which of the given options if put at //1 will correctly instantiate objects of various classes defined in the following code?

Code: Select all

public class TestClass
{
	public class A
	{
	}
	public static class B
	{
	}
	public void useclasses()
	{
		//1
	}
}
one of the proper anwser is

new TestClass.A();

My JVM says it is wrong. Can you tell me what is wrong?

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

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

Post by admin »

Are you sure you ran the exact same code as given in the question? I just tried it and it works fine.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Lodengruen
Posts: 5
Joined: Fri Oct 11, 2013 11:21 am
Contact:

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

Post by Lodengruen »

The problem is the missing semicolon in the proposed answer from the exam, option 4 is:
new TestClass.A()
but the proper answer would be:
new TestClass.A();
as you noted above!

Lodengruen
Posts: 5
Joined: Fri Oct 11, 2013 11:21 am
Contact:

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

Post by Lodengruen »

btw: What can I do if I find such a mistake in a question during the exam?

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

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

Post by admin »

Hi,
I do see ; in that option. Please make sure you are using the latest version of the question bank.

If you get such error in the exam, you may ignore the typo if it is obvious and pick it as the right option. But there is no guarantee as to what the question setter had in mind while writing the question so your guess is as good as mine.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

borkutip
Posts: 13
Joined: Sat Mar 08, 2014 1:20 am
Contact:

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

Post by borkutip »

Hello,

Code: Select all

class E1 {
    public int a;
    public class A {};
    public void mI() {
        A a1 = new A();
        A a2 = this.new A();
        A a3 = new E1.A();
        A a4 = new E1().new A();
        // E1.a = 5; 
    }
}
I see and know, that all the above instantiations work, but I do not understand, how A a3 = new E1.A() could work.
class A is not static, how can I reference to it with E1.A ?
I thought, that similarly to E1.a (which obviously does not work) new E1.A() also should not work.

Moreover, It also does not clear for me, how new E1(). new A() or this.new A() could work. As I understand, dot operator is stronger than new operator so (based on my mental model, which must be bad, but I do not know where is its fallacy) new this.A() should work, but in reality, this is not working.

Thank you in advance
Péter

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

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

Post by admin »

>how A a3 = new E1.A() could work.
Because this statement is already inside an instance method of E1, so, there is already an instance of E1 associated when you do new E1.A(). Try it from a static method of E1 and see what happens.

Where did you read that the dot operator is stronger than new?
If you like our products and services, please help us by posting your review here.

borkutip
Posts: 13
Joined: Sat Mar 08, 2014 1:20 am
Contact:

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

Post by borkutip »

Thank you for your answer.

(dot and new precedence): I read it here:
https://introcs.cs.princeton.edu/java/11precedence/
This is not very authentic, but at Oracle, I found only much less information:
https://docs.oracle.com/javase/tutorial ... ators.html
Here, there are many missing, for example, dot, new, cast, etc.

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

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

Post by admin »

borkutip wrote:
Mon Dec 14, 2020 3:00 pm
Thank you for your answer.

(dot and new precedence): I read it here:
https://introcs.cs.princeton.edu/java/11precedence/
This is not very authentic,
and is also incorrect because if dot had higher precedence than new, the following line wouldn't compile:

System.out.println(new String("hello").length()); //prints 5

This shows that new has higher (or at least same) precedence than dot.
If you like our products and services, please help us by posting your review here.

borkutip
Posts: 13
Joined: Sat Mar 08, 2014 1:20 am
Contact:

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

Post by borkutip »

Thank you for your answer, your example is great.

edufin166@yahoo.com
Posts: 24
Joined: Wed Sep 28, 2022 9:41 am
Contact:

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

Post by edufin166@yahoo.com »

Sorry but, I can not understand WHY new TestClass.A(); Is being used to call a Instance-Class..

For Public Static Class B, thats ok... so new TestClass.B() BEcause I do not need a instance od TestClass, in order to, use Class B once it is static.

But for public class A . I believe that the correte would be: new TestClass().A();

Am i wronged??

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

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

Post by admin »

Yes, since A is not a static class, an instance of its outer class is needed. But if you put the statement new TestClass.A(); within an instance method of the outer class (which is what the question is asking), then it already has a reference to an instance of the outer class at that point (the "this" reference). So, new TestClass() is not required there.
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 50 guests