volume_mute

Which method will give a compilation error?

publish date2021/06/24 22:22:00 GMT+10

volume_mute
(a)
public boolean isRich(int money) {

   if (money != 0)
     return true;
   else 
      return false;
}

(b)
public boolean isRich(int money) {

   if (money != 0)
     return true;

}
(a) will give an error
(b) will give an error
both will give errors
non of them will give error

Correct Answer

(b) will give an error

Explanation

(b) will give an error

Because the if condition does not cover the other possibility if money == 0 which will make the method incapable of achieving it is goal

(a) will not give an error because if condition covers all possibilities

A hint

This method could be further simplified by writing it like that

public boolean isRich(int money) {
   return money != 0;

}

Quizzes you can take where this question appears