26 B, F. Calling get() on an empty Optional causes an exception to be thrown, making option B correct. Option F is also correct because filter() makes the Optional empty before it calls get(). Option C is incorrect because the infinite stream is made finite by the intermediate limit() operation. Options A and E are incorrect because the source streams are not infinite. Therefore, the call to max() sees only three elements and terminates. For more information, see Chapter 10.
27 C. Music does not compile because records cannot include instance variables not listed in the declaration of the record, as it could break immutability. Song does not compile because a compact constructor cannot set an instance variable. The record would compile if this were removed from the compact constructor, as compact constructors can modify input parameters. March does not compile because it is an invalid override; it reduces the visibility of the toString() method from public to package access. Ballet does not compile because the subclass of a sealed class must be marked final, sealed, or non-sealed. Since the only one that compiles is Dance, option C is the answer. For more information, see Chapter 7.
28 B, D. Option A does not compile, as the expression 3 + 2.0 is evaluated as a double, and a double requires an explicit cast to be assigned to an int. Option B compiles without issue, as a long value can be implicitly cast to a double. Option C does not compile because the ternary operator (? :) is missing a colon (:), followed by a second expression. Option D is correct. Even though the int value is larger than a short, it is explicitly cast to a short, which means the value will wrap around to fit in a short. Option E is incorrect, as you cannot use a decimal (.) with the long (L) postfix. Finally, option F is incorrect, as an underscore cannot be used next to a decimal point. For more information, see Chapter 2.
29 F. The code compiles without issue. The key to understanding this code is to notice that our thread executor contains only one thread, but our CyclicBarrier limit is 3. Even though 12 tasks are all successfully submitted to the service, the first task will block forever on the call to await(). Since the barrier is never reached, nothing is printed, and the program hangs, making option F correct. For more information, see Chapter 13.
30 F. Line 5 does not compile as the FileNotFoundException thrown on line 12 is not handled or declared by the method. Line 7 does not compile because StringBuilder does not implement AutoCloseable and is therefore not compatible with a try-with-resource statement. Finally, line 10 does not compile as RuntimeException is a subclass of Exception in the multi-catch block, making it redundant. Since this method contains three compiler errors, option F is the correct answer. For more information, see Chapter 11.
Chapter 1 Building Blocks
OCP EXAM OBJECTIVES COVERED IN THIS CHAPTER:
Handling date, time, text, numeric and boolean valuesUse primitives and wrapper classes including Math API, parentheses, type promotion, and casting to evaluate arithmetic and boolean expressions
Utilizing Java Object-Oriented ApproachDeclare and instantiate Java objects including nested class objects, and explain the object life-cycle including creation, reassigning references, and garbage collectionUnderstand variable scopes, use local variable type inference, apply encapsulation, and make objects immutable
Welcome to the beginning of your journey to achieve a Java 17 certification. We assume this isn't the first Java programming book you've read. Although we do talk about the basics, we do so only because we want to make sure you have all the terminology and detail you need for the exam. If you've never written a Java program before, we recommend you pick up an introductory book on Java 8 or higher. Examples include Head First Java, 3rd Edition (O'Reilly Media, 2022) and Beginning Programming with Java for Dummies (For Dummies, 2021). Then come back to this certification study guide.
As the old saying goes, you have to learn how to walk before you can run. Likewise, you have to learn the basics of Java before you can build complex programs. In this chapter, we present the basics of Java packages, classes, variables, and data types, along with the aspects of each that you need to know for the exam. For example, you might use Java every day but be unaware that you cannot create a variable called 3dMap
or this
. The exam expects you to know and understand the rules behind these principles. While most of this chapter should be review, there may be aspects of the Java language that are new to you since they don't come up in practical use often.
Learning about the Environment
The Java environment consists of understanding a number of technologies. In the following sections, we go over the key terms and acronyms you need to know and then discuss what software you need to study for the exam.
Major Components of Java
The Java Development Kit (JDK) contains the minimum software you need to do Java development. Key commands include:
javac: Converts .java source files into .class bytecode
java: Executes the program
jar: Packages files together
javadoc: Generates documentation
The javac
program generates instructions in a special format called bytecode that the java
command can run. Then java
launches the Java Virtual Machine (JVM) before running the code. The JVM knows how to run bytecode on the actual machine it is on. You can think of the JVM as a special magic box on your machine that knows how to run your .class
file within your particular operating system and hardware.
Where Did the JRE Go?
In Java 8 and earlier, you could download a Java Runtime Environment (JRE) instead of the full JDK. The JRE was a subset of the JDK that was used for running a program but could not compile one. Now, people can use the full JDK when running a Java program. Alternatively, developers can supply an executable that contains the required pieces that would have been in the JRE.
When writing a program, there are common pieces of functionality and algorithms that developers need. Luckily, we do not have to write each of these ourselves. Java comes with a large suite of application programming interfaces (APIs) that you can use. For example, there is a StringBuilder
class to create a large String
and a method in Collections
to sort a list. When writing a program, it is helpful to determine what pieces of your assignment can be accomplished by existing APIs.
You might have noticed that we said the JDK contains the minimum software you need. Many developers use an integrated development environment (IDE) to make writing and running code easier. While we do not recommend using one while studying for the exam, it is still good to know that they exist. Common Java IDEs include Eclipse, IntelliJ IDEA, and Visual Studio Code.
Downloading a JDK
Every six months, Oracle releases a new version of Java. Java 17 came out in September 2021. This means that Java 17 will not be the latest version when you download the JDK to study for the exam. However, you should still use Java 17 to study with since this is a Java 17 exam. The rules and behavior can change with later versions of Java. You wouldn't want to get a question wrong because