Page 1 of 1

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

Posted: Wed Jan 23, 2013 12:29 am
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?

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

Posted: Wed Jan 23, 2013 6:26 am
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.

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

Posted: Thu Jan 24, 2013 2:04 am
by icepeanuts
got it. Thank u.

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

Posted: Mon Jun 03, 2013 5:20 am
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?

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

Posted: Mon Jun 03, 2013 6:21 am
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.

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

Posted: Tue Jan 28, 2014 11:53 pm
by kecker
I really wish these quizzes would stick to questions that will be on the exam.

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

Posted: Wed Jan 29, 2014 12:30 am
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.

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

Posted: Tue Nov 29, 2016 10:33 am
by Qusite
Hello,

Can you tell me why point 5 is correct?

Br,
Pawel

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

Posted: Tue Nov 29, 2016 11:33 am
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.

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

Posted: Fri Feb 10, 2017 2:49 pm
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

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

Posted: Fri Feb 10, 2017 9:41 pm
by admin
Yes, you are right. b points to 'x'. ""+'x' will be converted to "x".

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

Posted: Sun Apr 16, 2017 4:03 am
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

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

Posted: Sun Apr 16, 2017 5:50 am
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.

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

Posted: Fri Jun 22, 2018 5:27 pm
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?

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

Posted: Fri Jun 22, 2018 10:03 pm
by admin
Cannot guarantee but mostly likely not on the exam.