forked from eh3rrera/ocpj8-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch01a.html
78 lines (48 loc) · 3.4 KB
/
ch01a.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
<!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 style="text-align: center;">Chapter ONE</i><br />
Encapsulation and Immutable Classes</h1>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Implement encapsulation.<br />
Override hashCode, equals, and toString methods from Object class.<br />
Create and use singleton classes and immutable classes.<br />
Develop code that uses static keyword on initialize blocks, variables, methods, and classes.<br />
Develop code that uses final keyword.</i><br /></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>Answers</h2>
<p><b>1. The correct answer is D.</b><br /> As a <code>final</code> attribute, <code>a</code> is required to have been initialized, so compilation fails on the line marked as <code>// 3</code>.</p>
<p><br /></p>
<p><b>2. The correct answer is D.</b><br /> Private is the most restrictive of all access modifiers. <code>public</code> is the least restrictive of all.</p>
<p>Between default and <code>protected</code>, the latter is less restrictive since even if a subclass belongs to another package than the superclass, it can access protected members, in contrast to the default access (which has a same-package only level).</p>
<p><br /></p>
<p><b>3. The correct answer is C.</b><br /> As a <code>final</code> variable, <code>n</code> must be initialized either when declared, in a constructor, or a block. This, and the fact that changing its value is not allowed (in the setter method) generates a compile-time error.</p>
<p><br /></p>
<p><b>4. The correct answer is A.</b><br /> Although attribute <code>list</code> is <code>final</code>, this keyword only makes its reference immutable; it cannot be assigned another object (like a new <code>List</code>), but the values inside the object can change (if they are not <code>final</code> themselves). This (and other details) makes the class mutable.</p>
<p><br /></p>
<p><b>5. The correct answer is B.</b><br /> Attribute <code>s</code> is <code>private</code>, however, since the <code>main</code> method is in the same class, it can use the attribute without any problem.</p>
<p><br /></p>
<p><b>6. The correct answer is D.</b><br /> Attribute <code>b</code> is not <code>static</code>. A <code>static</code> block cannot reference a non-static variable. That's the reason compilation fails.</p>
<p> </p>
</div>
</div>
<footer></footer>
</body>
</html>