Java OCA OCP Practice Question 2893

Question

Given:

2. class MyClass {  
3.   MyClass(int s) { size = s; }  
4.   int size;  /*from  w  w  w .  j  a  va2  s . c  om*/
5.   void spin() { System.out.print(size + " inch wheel spinning, "); }  
6. }  
7. public class Main {  
8.   public static void main(String[] args) {  
9.     MyClass[] wa = {new MyClass(15), new MyClass(17)};  
10.     for(MyClass w: wa)  
11.        w.spin();  
12. } } 

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. If size was private, the degree of coupling would change.
  • C. If size was private, the degree of cohesion would change.
  • D. The Main class is tightly coupled with the MyClass class.
  • E. The Main class is loosely coupled with the MyClass class.
  • F. If size was private, the degree of encapsulation would change.


E and F are correct.

Note

E is correct because, as it stands, Main accesses MyClass only through its informal API, the method and the constructor.

F is correct because MyClass is not well encapsulated, and making size private would make MyClass well encapsulated.

A is incorrect because the code is legal.

B and C are incorrect because, in general, encapsulation is mostly independent of cohesion, and Main is already treating MyClass in a loosely coupled way.

D is incorrect based on the above.




PreviousNext

Related