-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathch09a.html
104 lines (62 loc) · 5.06 KB
/
ch09a.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!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 NINE</i><br />
Lambda Expressions</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Create and use Lambda expressions.</i></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>Answers</h2>
<p><b>1. The correct answer is C.</b><br /> Option A is invalid since the parameter list doesn't have parenthesis.<br /> Option B is invalid since an empty <code>return</code> statement is invalid.<br /> Option C is valid. The body doesn't need to use the <code>return</code> keyword if it only has one statement.<br /> Option D is invalid since the body has two statements and they must be enclosed in curly brackets.</p>
<p><br /></p>
<p><b>2. The correct answer is D.</b><br /> Option A is invalid because a is already used as the variable identifier.<br /> Option B is invalid because the lambda expression must return an <code>int</code> value.<br /> Option C is invalid because you can't pass a constant value in the parameter list of the lambda expression.<br /> Option D is valid because it takes a <code>String</code> argument and returns an <code>int</code> value.</p>
<p><br /></p>
<p><b>3. The correct answers are A and C.</b><br /> Lambda expressions can be used in:</p>
<ul>
<li>A variable declaration</li>
<li>An assignment</li>
<li>A return statement</li>
<li>An array initializer</li>
<li>As a method or constructor arguments</li>
<li>A ternary conditional expression</li>
<li>A cast expression</li>
</ul>
<p><b><br /></b></p>
<p><b>4. The correct answer is B.</b><br /> Option A is invalid because the interface is not functional (it doesn't have an <code>abstract</code> method).<br /> Option B is correct because the interface method doesn't take an argument and the type of the lambda expression can be cast to a <code>java.lang.Number</code> object.<br /> Option C is invalid because a <code>double</code> can't be cast to an <code>int</code>.<br /> Option D is invalid because the lambda expression's signature doesn't match the signature of the functional interface method <code>m(Integer[])</code>.</p>
<p><br /></p>
<p><b>5. The correct answer is C.</b><br/>
A lambda expression can access the default methods of its functional interface (<code>aMethod()</code> in this case), since default methods are inherited. But it cannot be accessed from <code>main()</code> because it's a <code>static</code> method and has no <code>this</code> reference.</p>
<p><br /></p>
<p><b>6. The only correct answer is A.</b><br /> A <code>return</code> keyword is not always required (or optional) in a lambda expression. It depends on the signature of the functional interface method.</p>
<p>Curly brackets are required whenever the <code>return</code> keyword is used in a lambda expression. Both can be omitted if the lambda expression's body is just one statement:</p>
<p><code class="java hljs">() -> <span class="hljs-number">2</span> * <span class="hljs-number">3</span>; <span class="hljs-comment">// Valid</span><br />
() -> { <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> * <span class="hljs-number">3</span>; }; <span class="hljs-comment">// Valid</span><br />
() -> <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> * <span class="hljs-number">3</span>; <span class="hljs-comment">// Not Valid!</span></code></p>
<p><b><br /></b></p>
<p><b>7. The correct answer is D.</b><br /> For a lambda expression, this resolves to the enclosing class where the lambda is written.</p>
<p><br /></p>
<p><b>8. The correct answer is C.</b><br /> Lambda expressions can access instance and static variables, but in this case, the parameter of the lambda expression shadows the instance variable <code>i</code>, so the value of <code>6</code> is used.</p>
<p><br /></p>
</div>
</div>
<footer></footer>
</body>
</html>