I’m usually pretty quick at finishing exams. However, when I took my first Java certification, I used almost every minute available. I realized how important it is to be able to dismiss some questions in order not to waste time.
Concretely, when passing a programming certification, one should be able to know immediately if the code won’t compile or raise an exception, so they don’t waste time evaluating the result for nothing.
In the Java Certification (OCA Oracle Certified Associate Java SE 8 Programmer 1Z0-808), many questions consist of a code snippet for which you have to tick either the correct output, or how it fails (doesn’t compile or raises an exception). Therefor, you should be able to notice quickly problems that would prevent compiling or raise errors, so you don’t spend time “executing” the whole code mentally.
Note this kind of question might be found in job interviews and technical tests when applying for a programming job!
This article is the first of a whole series on how to prepare for the OCA certification, make sure to register to get future posts in your mailbox. Next articles will break each theme of the certification with main attention points, pitfalls, and some advice.
This article was mentioned in JetBrains’ Java Annotated Monthly (October 2020)
Does it even compile?
One major thing to master when passing a test in Java is knowing either the code will compile or not. Thus also knowing what compiling means and knowing that compiling and running are two different things.
If you take the OCA, you’ll get asked what’s the input of given code snippets a lot. Very often, you will find these two options in the multiple choice options: “It doesn’t compile” and “It doesn’t run“.
Just knowing that it won’t work is not enough. You have to know why and at which stage it will fail.
In Java, we talk about “compile-time” and “runtime” problems/errors/issues… The compiling part has become quite abstract for most programmers now because the IDE (Eclipse, IntelliJ, NetBeans…) does it for us, in real time. Here’s a really short guide to compiling and running a Java application manually.
A short and fast way to understand the difference between compile-time and runtime errors is: when using an IDE which compiles in real time, any error appearing on the screen is a compile-time error. It would usually be underlined in red. Anyway, compile-time problems won’t allow you to run the application (running is what you do when you press the “play” or “run” button ▶️ in the IDE).
First thing to do when you have a code snippet to solve is to check for compile-time issues. That’s the fastest case because if the code can’t be compiled, there will be no output, no result whatsoever, and no error or exception thrown! As soon as you find a compile-time problem, you can leave the rest of the code and answer the question, and next!
Some common compile-time problems include:
- unreachable code (code that’s after a return/break/continue statement),
- missing return statement or returning the wrong type,
- trying to instantiate an abstract class or interface,
- initialized variable,
- no type match (but be careful with inheritance!),
- missing ; or { }, wrong code organization in general,
- missing imports or package declaration,
- incomplete method signature or class declaration, or wrong word order,
- wrong or redundant identifier, or reserved word used as identifier,
- variables out of scope…
Supa dupa important: you can’t run the application if compilation fails. Just remember this and keep it in mind during the exam. Also, compile-time errors don’t throw actual errors or exceptions. They can’t, because the program has to run to throw an error…
If compilation can’t fail…
You also want to check the answers before reading the question, because if there is no option “Compilation fails”, it means you don’t have to look for compilation errors at all! In that case, the code has to be solved, either to find out the output and/or find out the errors or exceptions occurring.
What about errors and exceptions?
Remember that an error or exception can only be thrown if the code runs, and that having exceptions thrown does not mean that there will be no other output, because an exception can happen later in the execution of the code! And they can be caught, which we will also see in a later post. Also remember that runtime issues in Java always happen with an error or exception thrown.
Studying for the certification
When I took the exam for the OCA certification, I already passed Introduction to algorithms and programming and Object Oriented Programming classes at school (CS business informatics bachelor degree), both in Java.
However, I learnt a lot while studying for my certification and had to practice again and again to ensure I would pass. The certification makes sure that you know Java very well, not just that you’re able to write applications with it.
I used the very well known “OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808” book which covers all the topics of the certification, with a lot of exercises. They might even be a bit harder than the actual exam questions, but that’s what makes it an excellent study material.
I also took a license at Enthuware (OCA pack costs around $10 which is very low price for what you get). Enthuware software allows you to train on exam-like questions with a timer (mock exams). There is a really large bank of questions and you might choose to train on particular topics or choose only the toughest questions to challenge yourself.
Both will help you master the compiling vs running understanding and have you prepared to answer such questions!
Since then I’ve passed a few other IT certifications and I experienced both in center and at home exams (all with Pearson Vue), please feel free to drop any question you might have!
3 thoughts on “Will the code even compile? | Preparing for Java Oracle Associate Certification (OCA) / Java basic technical tests”