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

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

Moderator: admin

Rafael
Posts: 3
Joined: Thu Jul 11, 2013 5:39 am
Contact:

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

Post by Rafael »

Sorry but I don't understand this expression

Code: Select all

   getArray()[index=2]++
What means? How does it work? I'm looking for information on google but I don't understand this format call : method()[];


Regards.

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

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

Post by admin »

Think of it this way:

someArray = getArray()
someInt = someArray[index=2]
someInt++

So, first getArray() returns an array, on which you apply [index] to access a specific element, and then you increment the value that you get by applying index to the array.

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

Rafael
Posts: 3
Joined: Thu Jul 11, 2013 5:39 am
Contact:

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

Post by Rafael »

Thanks Paul, I had never seen that expression and I was a little confused.

bbakla
Posts: 3
Joined: Wed Mar 18, 2015 1:16 am
Contact:

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

Post by bbakla »

Hi,
I modified the code as below:

Code: Select all

public static int[] getArray()
	{
		return new int[]{0, 1, 4, 6, 8, 10};
	}

public static void main(String[] args)
	{
		int index = 1;
		int g = 0;
		
		try
		{
			 g = getArray()[index=2]++;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		System.out.println("index = " + index);
		System.out.println("index = " + g);
		
	}}

Since it is a post increment and it seems to increment index not result. The print out is

Code: Select all

index = 2
index = 4
But when I change the code in try expression as below

Code: Select all

g = getArray()[index=2]<<2;
g is printed out 16 so it shifts actually the result!..

Can you please help me to understand that?

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

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

Post by admin »

Your print statement has a problem. It says "index =" but prints g.
index is not incremented due to ++. It is set to 2 because of the assignment operation index=2
If you like our products and services, please help us by posting your review here.

bbakla
Posts: 3
Joined: Wed Mar 18, 2015 1:16 am
Contact:

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

Post by bbakla »

My intention is actually to print g.

if the ++ doesnt increment the variable index, shouldn't g be 5? I guess ++ operator increments g in this case

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

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

Post by admin »

bbakla wrote:My intention is actually to print g.

if the ++ doesnt increment the variable index, shouldn't g be 5?
No, because it is postfix.
I guess ++ operator increments g in this case
No, it doesn't. Not sure why you think so.
If you like our products and services, please help us by posting your review here.

bbakla
Posts: 3
Joined: Wed Mar 18, 2015 1:16 am
Contact:

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

Post by bbakla »

Now I got it. postfix increments the element in the array..

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM »

Hello,

Could you please explain me the observation written at this exercise with a small example?

"In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated."

If we have this:
 int[] a = { 1, 2, 3, 4 };       
int[] b = { 2, 3, 1, 0 };       
System.out.println( a [ (a = b)[3] ] );

First it evaluates the whole expression: a[expression]. Keeps this on its stack trace.
Then evaluates (a=b)[3]. Here a points to the same array as b. So a[3] is equivalent to b[3] which is 0.
When it gets back on the stack to evaluate a[expression], which one is evaluated here? the initially declared array a or the one that is pointing to the same array as b?

Thank you for explaining ;)

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

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

Post by admin »

1. As it clearly says, "the expression to the left of the brackets appears to be fully evaluated ",
in a [ (a = b)[3] ], the expression to the left of the brackets is "a", which is evaluated first. Which means, the reference to the object pointed to be a is put on the stack first.

2. Now, (a = b)[3] is evaluated. Again, the expression to the left of brackets is (a = b), thus b is assigned to a. (This doesn't change the value that has already been put on the stack in previous step.)

3. Now, a[3] is evaluated, which is actually same as b[3] because a points to the same array as b i.e. 0.

4. Finally, [0] is applied to the original reference that was stored on the stack in step 1. It points to the array { 1, 2, 3, 4 }. Thus, the complete expression finally returns 1.

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

qmwuzapz
Posts: 3
Joined: Sun Jan 29, 2017 1:54 pm
Contact:

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

Post by qmwuzapz »

from the exam explanation:
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Is this the case ?

Code: Select all

class X {
    public static int [] apply(){
       if (Y.i==Y.i)throw new NullPointerException();
        return new int[]{10,8,4,6};
    }
}
public class Y {
    public static int i = 0;
    public static void main(String[] args) {
        try {
            System.out.println(X.apply()[i = 3]);
        } finally {
            System.out.println(i);
        }
    }
}

EricLeesburg
Posts: 3
Joined: Sun Feb 26, 2017 12:35 pm
Contact:

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

Post by EricLeesburg »

Code: Select all


     class Test  {

         public static int[ ] getArray() {  return null;  } 

         public static void main(String[] args)    {      
         
               int index = 1;     

               try{         
 
                     getArray()[index=2]++;     //1 
                   
                     } catch (Exception e){  }  //empty catch      
  
             System.out.println("index = " + index);   
             }
      }
For the statement 1, my understanding is that it equals the statements below:

Code: Select all

                 
                int temp= getArray()[index=2];
                
                 temp++;
and getArray()[index=2] actually is a element of the array. So by the explanation " In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.", the getArray () should be evaluated first, then the index=2 statement. In this case, getArray() throws a NullPointerException which leaves the index=2 statement unevaluated. So, what's wrong here?
Last edited by admin on Sun Feb 26, 2017 10:19 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

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

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

Post by admin »

qmwuzapz wrote:from the exam explanation:
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Is this the case ?

Code: Select all

class X {
    public static int [] apply(){
       if (Y.i==Y.i)throw new NullPointerException();
        return new int[]{10,8,4,6};
    }
}
public class Y {
    public static int i = 0;
    public static void main(String[] args) {
        try {
            System.out.println(X.apply()[i = 3]);
        } finally {
            System.out.println(i);
        }
    }
}
Yes, i will remain 0. i = 3 will not be executed.
If you like our products and services, please help us by posting your review here.

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

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

Post by admin »

Your getArray method doesn't throw a NullPointerException. It returns null. So the statement, "if evaluation of the expression to the left of the brackets completes abruptly," doesn't apply here.
If you like our products and services, please help us by posting your review here.

MariaRoxana
Posts: 2
Joined: Sun Dec 09, 2018 10:33 am
Contact:

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

Post by MariaRoxana »

Hello,

Can someone please give an example when it applies this rule "if evaluation of the expression to the left of the brackets completes abruptly"? Meaning in which cases the evaluation completes abruptly?

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

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

Post by admin »

For example, if the expression to the left of the brackets is a method call and that method throws an exception.
If you like our products and services, please help us by posting your review here.

Mikhail Volkov
Posts: 8
Joined: Mon Jul 29, 2019 11:25 am
Contact:

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

Post by Mikhail Volkov »

Guys, I think I understand... It's very easy in fact!
Look at two examples:
1) public static int[ ] getArray() { return null; } // [index=2] will work!!! This is case form question.
2) public static int[ ] getArray() { throw new RuntimeException(); } //[index=2] will not work. RTE - it is abnormal finish!

For 1:
If the array reference expression produces null(!!!!!!) instead of a reference to an array, then a NullPointerException is thrown at runtime, but only(!!!) after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.
return null - it is normal finish!

For 2:
Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
RunTimeException - it is abnormal finish!

Post Reply

Who is online

Users browsing this forum: No registered users and 105 guests