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

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

Moderator: admin

Post Reply
baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

why 'a' is remembered when this is done at runtime?

after the expression (a = b)[3] is evaluated JVM knows that 'a' now refers to 'b' so it should be b[3], no? or is it Sun pulling a rabbit from a hat??

this reminds me of finally tries to alter a value that catch() would return.

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

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

Post by admin »

It works that way because that is how it was designed :) There is no particularly great reason for it. You may want to check out the posts in this thread as well: viewtopic.php?f=2&t=1056

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

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

as the right answer is "It will print 1" I think the question needs to have a choice "It will print 2" too. because it could be another chose to challenge test takers.

thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

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

Post by thchuong »

I think the reason is because JVM stores copies of ALL intermediate values during the process of evaluating expression. And it processes from left to right.
So I have the easy way to determine the final result: Make the tree of expression.
Example:

Code: Select all

int z = 1; z = z + (z=2)*3 + z;
Tree:
               +
             /    \
           /       \
         +         z
        /  \
       /    \
      z     *
           /   \
          /     \
        z = 2    3
Here the JVM accesses z and stores 1 in the list of intermediate values.
               +
             /    \
           /       \
         +         z
        /  \
       /    \
      1     *
           /   \
          /     \
        z = 2    3
Then it assigns 2 to the ACTUAL z. Then stores 2 in the list of intermediate values.
               +
             /    \
           /       \
         +         z
        /  \
       /    \
      1     *
           /   \
          /     \
         2      3
Enough info for the multiply action, so it calculates 2*3=6:
               +
             /    \
           /       \
         +         z
        /  \
       /    \
      1     6
Then 1 + 6 = 7;
               +
             /    \
           /       \
         7          z
Then it ACCESSes z again, this time z has the value of 2 ,and it stores 2 in the list of intermediate values.
               +
             /    \
           /       \
         7          2
Finally 7 + 2 = 9!

If we apply the principle above to this question, it is the same (note that a is a reference variable)
               []
             /    \
           /       \
         a         []
                   /  \
                  /    \
               a = b    3
I am not expert in Java, just beginning to learn, so please teach me if I am wrong.

hadesgrid@gmail.com
Posts: 2
Joined: Thu Dec 04, 2014 12:37 pm
Contact:

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

Post by hadesgrid@gmail.com »

Hello,

in the (a=b)[3]
what does the [3] do

i konw that a=b asings b to a
but [3] ?

thank you in advance

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

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

Post by admin »

hadesgrid@gmail.com wrote:Hello,

in the (a=b)[3]
what does the [3] do

i konw that a=b asings b to a
but [3] ?

thank you in advance
[] syntax is to refers to a particular element of an array. Since a refers to an array, a[3] refers to the 4th element in that array ( 0, 1, 2, 3 ).
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Code: Select all

public class P11
{
	   public static void main(String[ ] args)
	   {
	      int[] a = { 1, 2, 3, 4 };
	      int[] b = { 2, 3, 1, 0 };
	      System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
	      System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
	      System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
	      System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
	   }
	}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2

klhlubek
Posts: 7
Joined: Mon Oct 12, 2015 9:58 am
Contact:

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

Post by klhlubek »

subhamsdalmia wrote:

Code: Select all

public class P11
{
	   public static void main(String[ ] args)
	   {
	      int[] a = { 1, 2, 3, 4 };
	      int[] b = { 2, 3, 1, 0 };
	      System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
	      System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
	      System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
	      System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
	   }
	}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2

You can see here very good, that a = b has an effect on a after the statements. Thank you for your great example ;)

Deleted User 2655

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

Post by Deleted User 2655 »

values of array b is assigned to a.

Code: Select all

public class P11
{
      public static void main(String[ ] args)
      {
         int[] a = { 1, 2, 3, 4 };
         int[] b = { 2, 3, 1, 0 };
         System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
          
          System.out.println( " a first "+a[0]);
          System.out.println( " a first "+a[1]);
          System.out.println( " a first "+a[2]);
          System.out.println( " a first "+a[3]);
          System.out.println( " ************************");
          System.out.println( " b first "+b[0]);
          System.out.println( " a first "+a[1]);
          System.out.println( " a first "+a[2]);
          System.out.println( " a first "+a[3]);
          
         System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
         
          System.out.println( " a second "+a[0]);
          System.out.println( " a second "+a[1]);
          System.out.println( " a second "+a[2]);
          System.out.println( " a second "+a[3]);
          System.out.println( " ************************");
          System.out.println( " b second "+b[0]);
          System.out.println( " a second "+a[1]);
          System.out.println( " a second "+a[2]);
          System.out.println( " a second "+a[3]);
          
         System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
          
          System.out.println( " a third "+a[0]);
          System.out.println( " a third "+a[1]);
          System.out.println( " a third "+a[2]);
          System.out.println( " a third "+a[3]);
          System.out.println( " ************************");
          System.out.println( " b third "+b[0]);
          System.out.println( " a third "+a[1]);
          System.out.println( " a third "+a[2]);
          System.out.println( " a third "+a[3]);
          
          
         System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
          
          System.out.println( " a fourth "+a[0]);
          System.out.println( " a fourth "+a[1]);
          System.out.println( " a fourth "+a[2]);
          System.out.println( " a fourth "+a[3]);
          System.out.println( " ************************");
          System.out.println( " b fourth "+b[0]);
          System.out.println( " a fourth "+a[1]);
          System.out.println( " a fourth "+a[2]);
          System.out.println( " a fourth "+a[3]);
      }
   }


Deleted User 3513

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

Post by Deleted User 3513 »

subhamsdalmia wrote:

Code: Select all

public class P11
{
	   public static void main(String[ ] args)
	   {
	      int[] a = { 1, 2, 3, 4 };
	      int[] b = { 2, 3, 1, 0 };
	      System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
	      System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
	      System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
	      System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
	   }
	}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I got lost here: a [ (a = b)[1] ] 0
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?

Tanooki
Posts: 9
Joined: Wed Dec 28, 2016 12:35 pm
Contact:

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

Post by Tanooki »

Deleted User 3513 wrote:
subhamsdalmia wrote:

Code: Select all

public class P11
{
	   public static void main(String[ ] args)
	   {
	      int[] a = { 1, 2, 3, 4 };
	      int[] b = { 2, 3, 1, 0 };
	      System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
	      System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
	      System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
	      System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
	   }
	}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I got lost here: a [ (a = b)[1] ] 0
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?
Because of the previous line. :) The variable a (array) now points to the object that b points to.

Did you execute all lines of code, or just one?

Deleted User 3513

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

Post by Deleted User 3513 »

Tanooki wrote: Because of the previous line. :) The variable a (array) now points to the object that b points to.

Did you execute all lines of code, or just one?
Hi Tanooki,

I did this manually with a pen and paper. First, I got 3 correctly but after executing the 2nd output, I don't quite understand why it results to 0. Can you please help me understand what happens in the 2nd line(a [ (a = b)[1] ] 0) exactly?

Thanks in advance. :)

JavaJunky
Posts: 4
Joined: Wed Dec 14, 2016 11:07 pm
Contact:

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

Post by JavaJunky »

Deleted User 3513 wrote:
subhamsdalmia wrote:

Code: Select all

public class P11
{
	   public static void main(String[ ] args)
	   {
	      int[] a = { 1, 2, 3, 4 };
	      int[] b = { 2, 3, 1, 0 };
	      System.out.println( " a [ (a = b)[0] ] "+a [ (a = b)[0] ] );
	      System.out.println( " a [ (a = b)[1] ] "+a [ (a = b)[1] ] );
	      System.out.println( " a [ (a = b)[2] ] "+a [ (a = b)[2] ] );
	      System.out.println( " a [ (a = b)[3] ] "+a [ (a = b)[3] ] );
	   }
	}
Output:
a [ (a = b)[0] ] 3
a [ (a = b)[1] ] 0
a [ (a = b)[2] ] 3
a [ (a = b)[3] ] 2
I got lost here: a [ (a = b)[1] ] 0
I'm getting 4 instead of 0. Can someone please explain in detail how it resulted to 0?

@Deleted User 3513
after you print the first print statement. Your array a is replaced by array b. So now when you refer array a the next, this is actually an array b. Now your question why a[(a=b)[1]] =0 ? remember your array a has been changed here already to {2,3,1,0} and array b is also {2,3,1,0}. when you do (a=b)[1]--> this gives you 3. now a[(a=b)[1]] -->a[3] which is the fourth element of an array a[3]=0. Hope that help you. I spent some time on this too.

Deleted User 3513

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

Post by Deleted User 3513 »

JavaJunky wrote: @Deleted User 3513
after you print the first print statement. Your array a is replaced by array b. So now when you refer array a the next, this is actually an array b. Now your question why a[(a=b)[1]] =0 ? remember your array a has been changed here already to {2,3,1,0} and array b is also {2,3,1,0}. when you do (a=b)[1]--> this gives you 3. now a[(a=b)[1]] -->a[3] which is the fourth element of an array a[3]=0. Hope that help you. I spent some time on this too.
Thank you very much! I get it now.

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests