The path to sjcp exam 310-065 - Part 2

It would be benefitial to examine a little more thoroughly the methodology of finding an answer to a question “from scratch” and turn that into an experience that will provide the appropriate knowledge to pass the exam.

Apart from reading and understanding and adopting a theoretical approach, the size of the items covered demands writing a big number of small programs so that the compiler's behaviour will get “wired” to your brain. Allow an extract from the SJCP Certificate book mentioned in the previous post:

or alternatively the modern stack overflow question: Resources and techniques/methods for SCJP preparation?.

Intentionally chosen, is the subject of labels in java (http://www.janeg.ca/scjp/flow/labels.html) will be discussed. It is important before that to consider the “why”s of this.

Why Labels

There is a number of questions in the SJCP examination concerning labels. Before that, there is some QnA about the topic:

Q. Why study labels?

A. Because they are part of the exam.

Q. But why really? Is there any other more arcane/mystic/deeper reason?

A. It does not matter.

Q. But please, is there any answer?

A. OK, for a number of different reasons, such as: labels are used primarily in the implementation of finite state machines. They can also be found in some old implementations of algorithms that got inherited in java from other implementations in other languages, such as C, or just to maintain consistency with the mathematical paper that describes the algorithm. Additionally they give a better understanding of how things work in assembly level. All of the above does not mean that people usually encounter labels in their real life while working with the language (most people don't know their existance, specially in Java). This fact may rationally lead to the following question…

Q. So since their use is limited, I can skip them or ignore them?

A. Not at all. There is no quota or percentage ratio on the questions areas that pop up in the exams, hence this post.

Now let’s answer one question…

Which are the values stored in variables i and j after running the following piece of code:

outer:
  for (i=0; i < 10; i++) {
    for (j=10; j > 0; j--) {
       if (j==5) {
         break outer;
       }
    }
  }

Additional question, answer the same for the following piece of code:

outer:
  for (i=0; i < 10; i++) {
    for (j=10; j > 0; j--) {
       if (j==5) {
         continue outer;
       }
    }
  }

Before getting down to the details, it is assumed that both variables are “something” (possibly a primitive, such as an int), that can store values between 0 and 10. On top of this if there ever was some code before or some code after those lines,as well as for the whole application around it, is valid and also can compile and that shall be the law(Too much time is spend on this, for a good reason). Which means that when we see “extracts” of code such as this, we don't look for syntax errors or so called “gotchas”.

This also means that now, that we will write a small application which will have a

public static void you_know_what(...)

the i will be initialised in this way:

int i=0;

before we print it, and not a:

int i;

which throws a compilation error, something you definetely know, else you would not be reading this blog post. (If you are, you know that you will first run one or two mini-programs and come back).

One way or another our java apprentice will end up with two programs which will look somehow like this:

class LoopCase1 {
  public static void main(String[] args) {
    int i=0;
    int j=0;
    outer:
    for (i=0; i < 10; i++) {
      for (j=10; j > 0; j--) {
        if (j==5) {
        break outer;
        }
      }
    }
    System.out.println("i = " + i + " j = " + j);
  }
}

Where the call to System.out.println(…) goes immediately after the code under examination. A mini-app with the source code above, can be downloaded here: LoopCase1.java.

class LoopCase2 {
  public static void main(String[] args) {
    int i=0;
    int j=0;
    outer:
    for (i=0; i < 10; i++) {
      for (j=10; j > 0; j--) {
        if (j==5) {
          continue outer;
        }
      }
    }
    System.out.println("i = " + i + " j = " + j);
  }
}

Which can be found here: LoopCase2.java.

Now we are ready to compile and run.

First - “break” case:

Second - “continue” case

Summary

Apparently this seems to be the depth and the approaching mindset needed for the exam. It is a matter of "switching" from usual forms and ways of learning as well as practice.

Good Luck.