Java_Magazine_NovemberDecember_2018

(singke) #1

90


// fi x t h i s /


aged in modern programming: they tend to get overlooked, and they make it harder to compre-
hend code. Misreading this code would likely lead you to select option A, because the modified
arithmetic would cause the output to be Three.
If you additionally mistook the meaning of the break keyword—thinking it performed like
continue (perhaps that’s not very likely in a question that has both break and continue to remind
you that they’re different)—the expected output would be Three, Four, and Five on consecutive
lines, prompting you to select D.
Part of the point here is that distractors are, as far as possible, selected based on what would
be true if a particular, and preferably fairly likely, error is made by test candidates.
Finally, you might think that it’s unfair for a question to use a programming style that’s
known to be tricky, such as the side effect used in this situation. Of course, the harsh reality is
that although you (and I) would never do anything so unprofessional, it’s not safe to assume that
all of your colleagues and the predecessors on your projects were always as thoughtful. Also, in
this particular case, there’s a persistent belief among some programmers that this kind of code
will run faster than it would if the side effect were separated out. That’s unlikely to be true with a
modern compiler and a JVM, all of which have extensive optimizers built into them. </article>

Simon Roberts joined Sun Microsystems in time to teach Sun’s first Java classes in the UK. He created the
Sun Certified Java Programmer and Sun Certified Java Developer exams. He wrote several Java certification
guides and is currently a freelance educator who publishes recorded and live video training through Pearson
InformIT (available direct and through the O’Reilly Safari Books Online service). He remains involved with
Oracle’s Java certification projects.

Mikalai Zaikin is a lead Java developer at IBA IT Park in Minsk, Belarus. During his career, he has helped Oracle
with development of Java certification exams, and he has been a technical reviewer of several Java certifica-
tion books, including three editions of the famous Sun Certified Programmer for Java study guides by Kathy
Sierra and Bert Bates.
Free download pdf