About Question enthuware.ocajp.i.v7.2.1331 :

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

Moderator: admin

Post Reply
icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

About Question enthuware.ocajp.i.v7.2.1331 :

Post by icepeanuts »

I don't exactly know what the 4th option means (i.e., c = AccessTest.this.a;). I thought AccessTest.this.a was the instance member a of class Inner. Can any one explain this?

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

AccessTest.this refers to the outer class object. Within a non-static inner class, "this" refers to the inner class object and "outerclassname.this" refers to the outer class instance.

So that means, AccessTest.this.a refers to the instance member a of AccessTest.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by icepeanuts »

got it. Thank u.

bptoth
Posts: 33
Joined: Mon May 06, 2013 9:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by bptoth »

Although it is not a very tough question, it deals with inner classes, which are not part of the OCAJP exam, as much as I am aware, so I wonder why it is still part of the question bank?

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

Some candidates have reported getting a questions with references to inner classes. So we have kept a few questions to avoid surprises in the exam.

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

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by kecker »

I really wish these quizzes would stick to questions that will be on the exam.

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

There are really only very few questions like to that in the he question bank and all are clearly marked as such. We have kept them because we believe they are important.
If you like our products and services, please help us by posting your review here.

Qusite
Posts: 1
Joined: Tue Nov 29, 2016 10:31 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by Qusite »

Hello,

Can you tell me why point 5 is correct?

Br,
Pawel

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

Qusite wrote:Hello,

Can you tell me why point 5 is correct?

Br,
Pawel
c = ""+b; <-- This makes c point to "x" because "" is empty and b points to 'x'.
return c; <-- This returns c

Thus, the call to new Inner().get(), will return a String reference that points to a String containing "x", which is then printed.

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

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by jamesmccreary »

Paul, I believe b actually points to 'x' (single quotes instead of double quotes).
Thus,

Code: Select all

c = ""+b;  
results in c = "x" because the JVM converts what b points to into a string according to the rule of string concatenation specified here: https://docs.oracle.com/javase/tutorial ... s/op1.html

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

Yes, you are right. b points to 'x'. ""+'x' will be converted to "x".
If you like our products and services, please help us by posting your review here.

Chandu
Posts: 7
Joined: Thu Mar 30, 2017 7:15 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by Chandu »

Note: This question may be considered too advanced for this exam.

Which statements can be inserted at line 1 in the following code to make the program write x on the standard output when run?

Code: Select all

public class AccessTest{
   String a = "x";
   static char b = 'x';
   String  c = "x";
   class Inner{
      String  a = "y";
      String  get(){
         String c = "temp";
         // Line 1
         return c;
      }
   }

   AccessTest() { 
     System.out.println(  new Inner().get()  ); 
   }

   public static void main(String args[]) {  new AccessTest();  }
}
c = ""+b;

How come you can call static variable directly without using class name (it should be c = AccessTest.b; like this calling by using class name?)? and what "" this means is it a predefined syntax and if yes how it functions?
//Totally new to this topic please helpout
Last edited by admin on Sun Apr 16, 2017 5:52 am, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

Chandu wrote: How come you can call static variable directly without using class name (it should be c = AccessTest.b; like this calling by using class name?)? and what "" this means is it a predefined syntax and if yes how it functions?
//Totally new to this topic please helpout
Java language allows you to access a static member of a class using the class name syntax (i.e. AccessTest.b ) as well as through any reference variable whose declared type is of that class.
Now, recall that "this" is a reference variable whose type is same as that of the current class. Thus, c = ""+b; is actually same as c = ""+this.b; and is therefore a valid way to access a static member b of class AccessTest.

It should be noted that accessing a static member using this or a reference variable is not a good practice even though it is legal. Using the class name is the preferred way because it is clear.

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

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by flex567 »

Since last comment about if this can be on the exam is 5yrs old, my question is if nowdays this kind of question about inner classes can occur in the exam?

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

Re: About Question enthuware.ocajp.i.v7.2.1331 :

Post by admin »

Cannot guarantee but mostly likely not on the exam.
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 2 guests