144k views
3 votes
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);
II only
III only
I, II and III
I only
II and III

User MikeMurko
by
6.5k points

1 Answer

6 votes

I.

for (String s: a) {

if (s.toUpperCase().charAt(0) != s.charAt(0)) {

return true;

}

}

return false;

User Zinx
by
6.0k points