198k views
0 votes
HELP PLEASE:

Consider the following method that is intended to test if all the Strings in the ArrayList start with an uppercase letter:

public static boolean capitalized(ArrayList a) {
/* Missing Code */
}


Which of the following could replace /* Missing Code */ so that the method works as intended?

I.

for (String s: a) {
if (s.toUpperCase().charAt(0) != s.charAt(0)) {
return true;
}
}
return false;


II.

for (String s: a) {
if (s.toUpperCase().charAt(0) != s.charAt(0)) {
return false;
}
}
return true;


III.

int flag = 1;
for (String s: a) {
if (s.toUpperCase().charAt(0) != s.charAt(0)) {
flag = 0;
}
}
return (flag == 1);


...possible answers are (I only), (II only), ( II and III), (I, II, and III), or (III only)

User Foxan Ng
by
6.5k points

1 Answer

5 votes

Answer:

II and III

Step-by-step explanation:

If you put II into words, it means if the capitalized letter at the first space in the ArrayList is not the same as the actual letter in the first space (ie: if it is not also capitalized), then tell the user that the first letter is not a capital letter. III is the same, but it uses a boolean to determine this. I is the opposite of II, and it is incorrect because it returns "true" if the first letter in the ArrayList is not capitalized.

User Wassim Dhif
by
6.9k points