Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 28
Question ID: UKOCP55895
What will be the result of compiling and executing Test class?
- package com.udayankhattry.ocp;
- public class Test {
- enum JobStatus {
- SUCCESS, FAIL; //Line n1
- }
- enum TestResult {
- PASS, FAIL; //Line n2
- }
- public static void main(String[] args) {
- JobStatus js = JobStatus.FAIL;
- TestResult tr = TestResult.FAIL;
- System.out.println(js.equals(tr)); //Line n3
- System.out.println(js == tr); //Line n4
- }
- }
- A Compilation error at Line n3
- B Compilation error at Line n4
-
C
true
true
-
D
false
false
Reveal correct answer
Correct answer: B
Explanation
UKOCP55895:
enums JobStatus and TestResult are siblings as both implicitly extend from java.lang.Enum class.
Siblings cannot be compared using double equals operator (==).
equals(Object) method accepts any instance which Object can refer to, which means all the instances.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
