jeanne’s java 8 upgrade exam experience

On Friday, I took the Java 8 Upgrade exam beta.

As I did with the OCPJP 7 and OCAJP 8I’m going to take a stab at guessing my score. I’ll edit this post when I know my actual score.  I think I got between 75% and 85%.
I got a 66% on the upgrade exam, but an 88% on the full exam which I more prepared for. (passing is 65% so I barely passed) I think I was overconfident. As I gained more experience (after the exam) both through using Java 8 and writing the chapters for our OCP book, I realized the topic was far deeper than I realized when studying for the exam. Also, I spent a lot of time studying Nashorn which I didn’t know at all before the objectives came out. Didn’t get any questions wrong on that. I also had mis-read the date/time objectives and didn’t know a good number of the APIs when taking the exam. Know them now!

How did I study?

Topic How studied
Inner classes Re-read that Advanced Class design chapter in the draft version of our OCP book. Also re-read the draft of our section on effectively final
Lambdas/filtering/collections/streams Re-read the parts of the functional programming chapter I had drafted along with my outline for the rest. (This is a big chapter and it isn’t finished yet. We do have the key points listed though which shows what to study/look for)My favorite book for learning these topics in the first place is Java 8 for the Really Impatient. Also Java 8 New Features is good. I also used the lambda tutorial to learn from originally.I’ve been doing a good bit of functional programming in Groovy in addition to learning lambdas deeply for Java. I felt prepared for this part of the exam. Which is good because it is a large percentage of the exam.
Static/default methods on interfaces Read the sections on interfaces in our OCA book and re-did the mock questions.
Java 8 Date/Time API Read the sections on Java 8 Dates in our OCA book and re-did the mock questions.Note: I completely missed the fact that the upgrade exam covers ZonedDateTime (and daylight savings time.) This is not on the OCA and therefore not in our OCA book. If you aren’t from the US, I recommend remembering that in the US, we move the clocks forward one hour (there is no 2-2:59am) on one day in the spring and backward one hour (2am occurs twice) on one day in the fall.I hope they don’t put this on the OCP exam. I don’t think something that requires this knowledge of how United States daylight savings time works belongs on the exam.
Nashorn Unlike lambdas, my experience with Nashorn is that I read a chapter on it in Java 8 for the Really Impatient. When I found out it was on the upgrade exam, I started learning it more deeply. I didn’t think they were going to put it on the exam. It’s not a major part of the language.Anyway, I wrote up the key points for the chapter before taking the exam. This turned out to be good preparation.I have coded in JavaScript. Which helped with one question, but wasn’t a major contributor to getting these questions right on the exam.I also used the API from the tutorial along with the shell tutorial. (the shell scripting parts aren’t on the exam – the objectives don’t say they are, but I was skeptical. Glad to see they scoped this down at least).

And of course, I wrote a bunch of practice code.

Test day

The objectives list the beta as being 150-225 minutes. (quite the range.) That’s 2.5 to 3.75 hours. My exam was 3 hours. (I’m glad I didn’t get an even longer one. Three hours is a long time to sit!) In my three hour window I got 126 questions to answer. It took me two hours to go through my first pass of answering each question. I had about 20 to go back to. One good thing about having so many questions is that sometimes a fact you don’t remember is answered in another question. That happened for a few of mine. After I finished going back to those 20, I called it done. I didn’t review the others because I was tired. Did I mention three hours is a long time?

When I took the OCAJP 8 beta, there was new functionality where you could right click an answer to cross it out (strikethrough) in the exam itself. It didn’t work at that point, but it does now. I really like this feature. It was nice to be able to mark off answers as I read. This also meant I had less to write down on the markerboard during the exam.

I even used the cross out feature for some questions that I wasn’t going back to. if I was thinking about “process of elimination” I used it. Or even if the answers were long. Or I had to choose the best two or three answers. It was nice to have less to remember in my head!

why I like regular expressions + who says they aren’t readable

Scott and I are working on our second book (OCP 8). I’m excited that I get to write the part about regular expressions as that is one of my favorite programming topics. Why, you ask? Because it lets you write clear and efficient code.

The scenario

For the book, I wrote an example showing how validating a simplified phone number is so much easier with a regular expression. The rules for the example were:

  1. a phone number is exactly 10 digits
  2. a phone number may contain dashes to separate the first three digits and next three digits, but not anywhere else
  3. no other characters are allowed (no parens around the area code in this example)

For example, 123-456-7890, 123-4567890 and 123456-7890 are valid. In real life, the third one wouldn’t be; we allow this typo here to be nice.  However dashes aren’t allowed in random positions. 12-45-67-890 is not a phone number.

Without regular expressions

This isn’t in the book, but i tired to write the code “the long way” to ensure it was annoying long. It was. I tried to write the code in a readable way and the best I could think of was:

private static boolean validateLong(String original) {
   String phone = original;
   // remove first dash (if present)
   if (phone.charAt(3) == '-') {
     phone = phone.substring(0, 3) + phone.substring(4);
   }
   // remove second dash (if present)
   if (phone.charAt(6) == '-') {
      phone = phone.substring(0, 6) + phone.substring(7);
   }

   // validate 10 characters left
   if (phone.length() != 10) {
      return false;
   }

   // validate only numbers left
   Set<Character> digits = new HashSet<>(Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'));
   for (int i = 0; i < phone.length(); i++) {
      if (!digits.contains(phone.charAt(i))) {
         return false;
      }
   }
   return true;
   }

This is a lot of code. And to those who think regular expressions are unreadable, what do you think of the above? I don’t find it easy to see what is going on even though I wrote it. There’s just too much logic and too much detail to ensure is correct. (And no, it didn’t work on my first attempt.)

With regular expressions

Re-writing to use regular expression gives me this:

private static boolean validate(String phone) {
   String threeDigits = "\\d{3}";
   String fourDigits = "\\d{4}";
   String optionalDash = "-?";
   String regEx = threeDigits + optionalDash + threeDigits + optionalDash + fourDigits;
   return phone.matches(regEx);
}

Even if you don’t know the regular expression syntax, it should be obvious what is going on here. We look for three digits, an optional dash, three more digits, another optional dash and a final four digits.

It’s a tiny bit longer in the book version because {3} isn’t on the exam so that part is:

String threeDigits = "\\d\\d\\d";
String fourDigits = "\\d\\d\\d\\d";

Still. Way easier to read and faster to write than the original code without regular expressions. I consider regular expressions like a hammer. They aren’t the right tool for every job, but they are quite helpful when they are the right tool.