Java OCA OCP Practice Question 2837

Question

Given:

2. public class Main {  
3.   int count;  
4.   int getCount() { return count; }  
5.   void setCount(int s) {  
6.     assert(s > 30000);  
7.     count = s;  
8. } } 

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. The class is well encapsulated as it stands.
  • C. Removing line 6 would weaken the class's degree of cohesion.
  • D. Removing line 6 would weaken the class's degree of encapsulation.
  • E. If the count variable was private, the class would be well encapsulated.
  • F. Removing line 6 would make the class's use of assertions more appropriate.


E and F are correct.

Note

F is correct because line 6 currently uses the assert mechanism to validate a non-private method's argument, which is not considered appropriate.

A is incorrect because the code is legal.

B is incorrect because count is not private.

C and D are incorrect because the assert mechanism is mainly independent of the OO concepts of cohesion and encapsulation.




PreviousNext

Related