It is fairly controversial to take Oracle’s Java certificates. One of the very vocal opponents of them in the local Java scene is Wojciech Seliga, who claims that he noticed that people with certificates performed worse at job interviews at Spartez than those that don’t have them (around 4:15). Even with such claims, I decided that I want to pass OCJP this year as I thought that it might be a good idea to review my Java fundamentals knowledge and make sure I don’t miss anything. What surprised me a bit is that with Java 8, the certification process is different than a few years ago and now one needs to first pass an OCA exam. At first, I was a bit dismissive – come on, this is just an exam that shows you how to write for loops – but then I took an example exam and scored… 25%.
So I learned. One important thing to notice is that Oracle’s Java exams test if you can be a compiler – there are multiple cases where they aim to trick you into parsing something that won’t compile. One can be surprised how much caveats there might be and that’s where a good point is to read a good book that prepares for an exam. I read a book by Jeanne Boyarsky and Scott Selikoff and highly recommend that one. The benefit of those preparations is that I structured some of the core Java knowledge – some things I haven’t yet seen in practice (thanks to Flying Spaghetti Monster!) as they aren’t usually good code examples – but at least when I see it in some legacy project in future I won’t run away screaming. Some of them are just something that’s easy and quite obvious, yet it can surprise.
Examples below aim mostly for junior/wannabe developers, but they might be a good refresher also for a developer with some experience. You can see all examples in my Github repository.
1. Can we use a different args name in main() method?
Yes, we can! The name can be any valid Java identifier.
2. Will that code compile?
Yes. We can have only one public class in the file and the file is named just as the public class.
3. What will be the output?
The output is 1. Number variable will be first assigned 2, then 3, then 1. With 3 you can see a static block initialization.
4. That’s a legal number?
A nice, fancy way to write 1000000.000222. Usually used to separate by three numbers, like int result = 1_000_000. Underscores cannot be at the beginning, end and around dot sign.
5. What will that code print?
A trick one. We abuse the fact that assigning a variable returns the variable. The result will be Hello!. It would work that way only with boolean. I highly discourage you from doing that.
6. Which statements are valid?
Line 5 is fine. Same with line 6 – we assign a value to s1 and s2 is declared without a value. Line 7 does not compile – we cannot declare variables with two types in one statement – even if the type is actually the same. Line 8 is okay, it’s just two statements in one line. Line 9 – does not compile. We didn’t provide a type for i4. If we declared i4 somewhere above with a type, it would be fine.
7. Why doesn’t this compile?
Division of two shorts gives us an int that cannot be implicitly cast to short. We need to explicitly do the casting:
8. Does this compile?
Nope. The first line is an int because it’s outside short’s range. Second one lacks an L sign at the end and is too large to be an int. BTW, did you know that you can add plus signs before numbers? Not that it’s helpful, but you can 馃槈
9. What will that code return?
Well, it won’t return anything because it won’t compile! In Java 8, one cannot switch on long and boolean types. In Java 7, switch on String was added that also previously wasn’t possible.
10. What will be the output?
The output here is:
Weekday Saturday
Note that we use a static code block for the output. The main trick is that we put default not as the last one (as you would expect) and we forgot to put a break; there.
11. What does this code output?
1234 5
We use a label to break out of two for loops at once. Feels a bit like goto, but can be used only in certain cases.
12. Are these strings the same? What will be the output here?
true false true true true false
In this case I’ll guide you to a good answer on string constant pool from StackOverflow. One more thing to notice is that this pool is an implementation of Flyweight design pattern.
13. Can we get a new dog? What will be the result of running this code?
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at pl.mmkay.oca.Level13ListFromArray.main(Level13ListFromArray.java:12)
Arrays.asList creates an array-backed List implementation. In that one we cannot add or remove elements. We could only replace an element. That’s why we needed to create a new ArrayList to add a dog.
(That one is actually a real-life example, in a flat that I currently rent with my wife, we have a contract saying that we cannot have more than 2 pets at a time ;))
14. What will be the output here?
1 -2
It’s good to know what Arrays.binarySearch return values are. If an element is found, it will return an element’s index. If not, it will return a negative number that is
-1 * index - 1
where index is what would be the index of this element if it was in the array.
Important thing here is that array must be sorted beforehand! If it’s not, the results might be quite surprising.
15. Which element will stay in the list?
The only one that will stay will be 1. There are two remove methods in List class:
remove(int index) remove(Object o)
For the first removal, the best match was the one with index. The second one looked for the right object.
That’s all, folks!
Hope you liked the examples. Now you know a bit more on how to act like a compiler.