About Question enthuware.ocajp.i.v7.2.1224

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

Moderator: admin

Post Reply
skissh
Posts: 7
Joined: Tue Jan 31, 2017 4:16 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1224

Post by skissh »

Since the if statement cannot change the execution flow could we just replace it with i++; --j; as we think about the loop's execution. I have seen this construction before in loop test questions, where an if statement when true executes continue;. If the if statement is the last line of the loop body then the if conditions result does not matter. Both true and false result in continuing the loop. Is my analysis correct?

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

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

Post by admin »

I am sorry but I did not understand the scenario you have described. Can you please post some code to show what you mean?
Paul.
If you like our products and services, please help us by posting your review here.

skissh
Posts: 7
Joined: Tue Jan 31, 2017 4:16 pm
Contact:

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

Post by skissh »

My point is that the if statement cannot change the flow of the program regardless of the values of i and j. Once the test taker notices this they should not waste time evaluating whether (i++ < j--) is true or false. The methods in the following code reproduce the function of the code in the test question, one with the if statement and one without. When using the same input for each of the methods they always produce the same result.

Code: Select all

class Main {
  public static void main(String[] args) {
    withIf(1,10);
    withoutIf(1,10);
  }

  private static void withIf(int i, int j) {
    int k =1;
    do {
      System.out.println("Iteration "+k+": i=" + i + " j=" + j);
      k++;
      if (i++ > --j) continue;
    } while (i < 5);
    System.out.println("i=" + i + " j=" + j);
  }
  private static void withoutIf(int i, int j) {
    int k =1;
    do {
      System.out.println("Iteration "+k+": i=" + i + " j=" + j);
      k++;
      i++; --j;
    } while (i < 5);
    System.out.println("i=" + i + " j=" + j);
  }
}

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 91 guests