Java OCA OCP Practice Question 2597

Question

Given:

2. public class Employee {  
3.   private String name;  
4.   private String city;  
5.   String getName() { return name; }  
6.   void setName(String n) { name = n; }  
7.   void setCity(String c) {  
8.     if(c == null) throw new NullPointerException();  
9.     city = c;  //from   ww w  . j a  v a2  s  . c om
10.   }  
11.   String getCity() { return city; }  
12. } 

Which are true? (Choose all that apply.).

A.   Compilation fails.
B.   The class is well encapsulated.
C.  The setCity() method is an example of loose coupling.
D.  The setCity() method has better encapsulation than  setName().
E.  The setCity() method is cohesive; the  setName() method is not.


B is correct.

Note

The setCity() method includes argument validation, which is a good thing.

It's not, by itself, an example of coupling or cohesion.

Also, validation doesn't make a method more encapsulated.

A is incorrect because it's legal to throw a NullPointerException without handling or declaring it.

C, D, and E are incorrect based on the above.




PreviousNext

Related