Java OCA OCP Practice Question 2202

Question

Which can fill in the blank Main class to compile and logically complete the code? (Choose two.)


public class Test { 
   boolean pass; 
   protected boolean passed() { 
     return pass; 
   } /*from   www .j  a v a2 s.  c  om*/
} 

class Main extends Test { 
   private Test oca; 
   private Test ocp; 
   // assume getters and setters are here 
    
} 
  • A. boolean passed() { return oca.pass && ocp.pass; }
  • B. boolean passed() { return oca.passed() && ocp.passed(); }
  • C. boolean passed() { return super.passed(); }
  • D. public boolean passed() { return oca.passed() && ocp.passed(); }
  • E. public boolean passed() { return oca.pass && ocp.pass; }
  • F. public boolean passed() { return super.passed(); }


D, E.

Note

Since Main is a subclass of Test, it cannot have a more specific visibility modifier.

Test uses protected, which is broader than package-private in Main.

This rules out Options A, B, and C.

The other three options all compile.

However, Option F has a problem.

Suppose your Main object has an Test object with pass set to true for both the oca and ocp variables.

The implementation in Option F doesn't look at either of those variables.

It looks at the superclass's pass value.

This isn't logically correct.

Therefore, Options D and E are correct.




PreviousNext

Related