Java_Magazine_NovemberDecember_2018

(singke) #1

88


// fi x t h i s /


((myStr.spliterator().characteristics() & Spliterator.ORDERED) != 0)

Alternatively, if you simply want to test whether a particular data structure has an encounter
order, you can request the spliterator directly from that structure and test it, without ever going
to a stream. For a collection called myColl, you could use this code:

((myColl.spliterator().characteristics() & Spliterator.ORDERED) != 0)

Finally, it’s worth mentioning that it’s possible to encounter in a real exam question a comment
that describes the behavior of a piece of code that’s omitted from the body of a description (as
with the // CODE OMITTED HERE comment in this question). It’s not something that happens often,
and the complexity of the description in this case is almost certainly more than a real ques-
tion would allow. However, the guidance notes on the Oracle website include the following text
(expand the Review Exam Topics list):

“ Descriptive comments: Take descriptive comments, such as ‘setter and getters go here,’ at
face value. Assume that correct code exists, compiles, and runs successfully to create the
described effect.”

So, don’t be afraid to take literally any such comments, and know that the “hidden” code is
not an attempt to trick you but instead is simply an effort to allow the code that’s presented to
focus on what’s important for understanding the question.

Answer 4. Option B is correct. This question investigates the behavior of code that uses continue
and break in a loop. These kinds of questions require that you mentally simulate the Java inter-
preter. Doing that can be a surprisingly effective way to determine if you really understand the
language, but it also demands that you pay close attention to all the details in the question. As
a result, such a question can be time-consuming and frustrating in an exam, because the time
required to notice details can encourage you to rush, and a tiny slip can cause you to miss the

Question 4
page 80
Free download pdf