EECS2030 Sample Test 2


Instructions

There is one programming question and one multiple choice question.

Instructions for submitting your work are given in the question sections. You may submit as many times as you want; your most recent submission will be the one recorded.

You may leave the lab when you are finished with the test. Please return to the lab when the test is over for your regularly scheduled lab.


Programming question

Implement this API. You do not have to include javadoc comments.

Submit your program using the following command in a terminal (make sure you are in the directory containing your file Test2D.java):

submit 2030 test2D Test2D.java


Multiple choice question

Question 1

Inside of a constructor, the keyword this refers to what?

A. the class of the object being constructed
B. the fields of the object being constructed
C. the object being constructed
D. the memory location of the client's variable that refers to the new object

Question 2

Suppose that two Student objects are considered equal if their student numbers are equal; in the Student implementation, the student number is stored as a String, and student numbers are guaranteed to not be null.

The implementation of the Student class looks like:

public class Student {
  private String studentNumber;
  
  public Student(long number) {
    this.studentNumber = new String(Long.toString(number));
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (this.getClass() != obj.getClass()) {
      return false;
    }
    Student other = (Student) obj;
    return this.studentNumber == other.studentNumber;
  }
  
  // hashCode not shown, but guaranteed to be correct
}

What does the following client code output?

Student s = new Student(123);
Student t = new Student(123);
System.out.println(s.equals(t));

Type your answer here:

Question 3

You were taught that if you override equals in a class, then you must also override hashCode. Is it also true that if you override hashCode then you must also override equals?

You must explain your answer; simply stating yes or no will not be considered as an answer.