-
Notifications
You must be signed in to change notification settings - Fork 90
/
ch14a.html
65 lines (40 loc) · 2.84 KB
/
ch14a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam ">
<title>Java 8 Programmer II Study Guide: Exam 1Z0-809</title>
<link href="css/code.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="js/common-sections.js"></script>
</head>
<body>
<div class="header">
<div class="title-container">
<div class="chapter-title">
<h1><i class="chapter">Chapter FOURTEEN</i><br />
Optional Class</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Develop code that uses the Optional class.</i><br /></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>Answers</h2>
<p><b>1. The correct answer is C.</b><br /> The method <code>orElseGet()</code> returns a value of the type of the <code>Optional</code>, it doesn't return an exception, no matter if it's not executed. For exceptions, we have to use <code>orElseThrow()</code>.</p>
<p><br /></p>
<p><b>2. The correct answer is C.</b><br /> Option A is false. The method <code>Optional.ifPresent()</code> is the one that takes a <code>Consumer<T></code> as an argument that is executed only if the <code>Optional</code> contains a value.<br /> Option B is false. The method <code>Optional.of()</code> cannot create an empty <code>Optional</code>.<br /> Option C is true. The method <code>Optional.of()</code> can throw a <code>NullPointerException</code> if its argument is <code>null</code>.<br /> Option D is false. The method <code>Optional.ifPresent()</code> takes a <code>Consumer<T></code> as an argument.</p>
<p><br /></p>
<p><b>3. The correct answer is D.</b><br /> The program is passing an invalid number to the method, so an exception is thrown by <code>Integer.parseInt()</code> and <code>Optional.empty</code> is returned. The method <code>get()</code> throws an exception if the <code>Optional</code> has an empty value, and this is exactly what happens.</p>
<p><br /></p>
<p><b>4. The correct answer is A.</b><br /> The <code>orElse()</code> method returns the argument when the <code>Optional</code> is empty, otherwise, it returns the encapsulated value of the <code>Optional</code>, in this case, <code>0</code>.</p>
<p><br /></p>
</div>
</div>
<footer></footer>
</body>
</html>