Limited access

Upgrade to access all content for this subject

Assume that the mammals array has a list of mammal names, but we do not know how big the array is and whether it is full of mammal names. Each element is either populated with a valid mammal name or has not been assigned any value.

Which of the following segments of code will always be able to print the count of the number of mammal names in the array?

String[] mammals = // list of mammals
A
int count = 0;
for (String m : mammals)
    if (m.length() > 0)
        count++;
System.out.print(count);
B
int count = 0;
for (String m : mammals)
    if (!m.equals(""))
        count++;
System.out.print(count);
C
int count = 0;
for (String m : mammals)
    if (m != null)
        count++;
System.out.print(count);
D
int count = 0;
for (String m : mammals)
    if (m.compareTo("")>0)
        count++;
System.out.print(count);
E
System.out.println(mammals.length);
Select an assignment template