- Home
- Programming
- OCP Java SE8
21.
Which of these classes best implement(s) the singleton pattern?
class ExamAnswers {
private static ExamAnswers instance = new ExamAnswers();
private List answers = new ArrayList<>();
private ExamAnswers() {}
public ExamAnswers getExamAnswers() {
return instance;
}
public List getAnswers() {
return answers;
}
}
class TestAnswers {
private static TestAnswers instance = new TestAnswers();
private List answers = new ArrayList<>();
private TestAnswers() {}
public static TestAnswers getTestAnswers() {
return instance;
}
public List getAnswers() {
return answers;
}
}
class ExamAnswers {
private static ExamAnswers instance = new ExamAnswers();
private List
private ExamAnswers() {}
public ExamAnswers getExamAnswers() {
return instance;
}
public List
return answers;
}
}
class TestAnswers {
private static TestAnswers instance = new TestAnswers();
private List
private TestAnswers() {}
public static TestAnswers getTestAnswers() {
return instance;
}
public List
return answers;
}
}
- A.ExamAnswers
- B.TestAnswers
- C.Both classes
- D.Neither class
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
The singleton pattern requires that only one instance of the class exist. The ExamAnswers class is close. However, getExamAnswers() is not static, so you can’t retrieve the instance. Option B is the answer because TestAnswers is a correct implementation. It has a static variable representing the one instance and a static method to retrieve it. |
22.
How many lines does the following code output?
public class Cars {
static {
System.out.println("static");
}
private static void drive() {
System.out.println("fast");
}
public static void main(String[] args) {
drive();
drive();
}
}
public class Cars {
static {
System.out.println("static");
}
private static void drive() {
System.out.println("fast");
}
public static void main(String[] args) {
drive();
drive();
}
}
- A.one
- B.two
- C.three
- D.None of the above. The code does not compile.
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
The static initializer is only run once. The static method is run twice since it is called twice. Therefore, three lines are printed, and Option C is correct. |
23.
Which is not a true statement given this diagram?
- A.Instance methods in the Blanket class can call the Flashlight class’s turnOn().
- B.Instance methods in the Flashlight class can call the Flashlight class’s replaceBulb().
- C.Instance methods in the Phone class can call the Blanket class’s wash().
- D.Instance methods in the Tent class can call the Tent class’s pitch().
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Option A is allowed because the turnOn() method is public and can be called from anywhere. Options B and D are allowed since the method is in the same class, which is always allowed! Option C is not allowed because wash() is a package-private method in another package. Option C is the correct answer. |
24.
In Given Diagram, how many of the classes can call the display() method?
- A.one
- B.two
- C.three
- D.four
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
The display() method has protected access. This means it can be accessed by instance methods in the same package and any subclasses. There are no subclasses in this example, so we only need to count the classes in the same package. Option B is correct because Flashlight and Phone are in the package. |
25.
What does the following print?
1: class SmartWatch extends Watch {
2: private String getType() { return "smart watch"; }
3: public String getName(String suffix) {
4: return getType() + suffix;
5: }
6: }
7: public class Watch {
8: private String getType() { return "watch"; }
9: public String getName(String suffix) {
10: return getType() + suffix;
11: }
12: public static void main(String[] args) {
13: Watch watch = new Watch();
14: SmartWatch smartWatch = new SmartWatch();
15: System.out.print(watch.getName(","));
16: System.out.print(smartWatch.getName(""));
17: }
18: }
1: class SmartWatch extends Watch {
2: private String getType() { return "smart watch"; }
3: public String getName(String suffix) {
4: return getType() + suffix;
5: }
6: }
7: public class Watch {
8: private String getType() { return "watch"; }
9: public String getName(String suffix) {
10: return getType() + suffix;
11: }
12: public static void main(String[] args) {
13: Watch watch = new Watch();
14: SmartWatch smartWatch = new SmartWatch();
15: System.out.print(watch.getName(","));
16: System.out.print(smartWatch.getName(""));
17: }
18: }
- A.smart watch,watch
- B.watch,smart watch
- C.watch,watch
- D.None of the above
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Line 15 calls the method on line 9 since it is a Watch object. That returns watch, making Option A incorrect. Line 16 calls the method on line 3 since it is a SmartWatch object and the method is properly overridden. That returns smart watch, so Option B is the answer, and Option C is incorrect. |