Олимпиадный тренинг

Задача . 37344


Задача

Темы:
Refer to the following classes for Question.
public class Address
{
    private String name;
    private String street;
    private String city;
    private String state;
    private String zip;
    //constructors
      ...
    //accessors
    public String getName()
    { return name; }
    public String getStreet()
    { return street; }
    public String getCity()
    { return city; }
    public String getState()
    { return state; }
    public String getZip()
    { return zip; }
}
public class Student
{
    private int idNum;
    private double gpa;
    private Address address;
    //constructors
     ...
    //accessors
    public Address getAddress()
    { return address; }
    public int getIdNum()
    { return idNum; }
    public double getGpa()
    { return gpa; }
}
Here is a method that locates the Student with the highest idNum:
/** Precondition: Array stuArr of Student is initialized.
* @return Student with highest idNum
*/
public static Student locate(Student[] stuArr)
{
     /* method body */
}
Which of the following could replace /* method body */ so that the method works as intended?

I
int max = stuArr[0].getIdNum();
for (Student student : stuArr)
    if (student.getIdNum() > max)
    {
        max = student.getIdNum();
        return student;
    }
return stuArr[0];

II
Student highestSoFar = stuArr[0];
int max = stuArr[0].getIdNum();
for (Student student : stuArr)
    if(student.getIdNum() > max)
    {
       max = student.getIdNum();
       highestSoFar = student;
    }
return highestSoFar;

III
int maxPos = 0;
for(int i = 1; i < stuArr.length; i++)
     if(stuArr[i].getIdNum() > stuArr[maxPos].getIdNum())
           maxPos = i;
return stuArr[maxPos];
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only

time 1000 ms
memory 32 Mb
Правила оформления программ и список ошибок при автоматической проверке задач

Статистика успешных решений по компиляторам
Комментарий учителя