Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!

Yevhen Ikonnykov

Greenhorn
+ Follow
since Aug 08, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Yevhen Ikonnykov

Hello,

Oracle Certified Associate Java SE 8 Programmer 1 STUDY GUIDE EXAM 1Z0-808 Introduction Assessment Test Question 12 states:

1: public class Egret {
2:    private String color;
3:         public Egret() {
4:          this("white");
5: }
6: public Egret(String color) {
7:    color = color;
8:  }
9: public static void main(String[] args) {
10:   Egret e = new Egret();
11:   System.out.println("Color:" + e.color);
12: }
13:}

A. Color:
B. Color:null
C. Color:White
D. Compiler error on line 4.
E. Compiler error on line 10.
F. Compiler error on line 11.

The Study guide answer states:
B. Line 10 calls the constructor on lines 3–5. That constructor calls the other construc- tor. However, the constructor on lines 6–8 assigns the method parameter to itself, which leaves the color instance variable on line 2 set to its default value of null. For more information, see Chapter 4.

But the right answer seems to be F, because it is prohibited to call a private field outside the class. Am I right?
Hello,

Oracle Certified Associate Java SE 8 Programmer 1 STUDY GUIDE EXAM 1Z0-808 Assessment Test 1 Question 2 states:

What is the output of the following program?

1: public class WaterBottle {
2: private String brand;
3: private boolean empty;
4: public static void main(String[] args) {
5: WaterBottle wb = new WaterBottle();
6: System.out.print("Empty = " + wb.empty);
7: System.out.print(", Brand = " + wb.brand);
8: } }

A. Line 6 generates a compiler error.
B. Line 7 generates a compiler error.
C. There is no output.
D. Empty = false, Brand = null
E. Empty = false, Brand =
F. Empty = null, Brand = null

The Study guide answer states:
D. Boolean fields initialize to false and references initialize to null, so empty is false and brand is null. Brand = null is output.

Actually answers A and B look right, because you cannot call private fields outside the class. Am I right?