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 Test2C.java):

submit 2030 test2C Test2C.java


Multiple choice question

Question 1

Constructor chaining is useful because it:

A. guarantees that fields will be set correctly
B. prevents a client from creating objects
C. reduces code duplication
D. gives the client a static method that can be used to create new objects

Question 2

Suppose a client uses the Complex number class in a program like so:

Complex y = new Complex(1, 5);
Complex z = new Complex(1, 5);
y.setReal(-3);

A memory diagram is shown below for when the setReal method is running:

        100 |     client      |
     y      |  400a           |
     z      |  500a           |
            |                 |
            |                 |
        400 | Complex object  |
  real      |  1              |
  imag      |  5              |
            |                 |
            |                 |
        500 | Complex object  |
  real      |  1              |
  imag      |  5              |
            |                 |
            |                 |
        800 | Complex setReal |
  this      |                 |
  real      |  -3             |
            |                 |

What is the value in the memory location labelled this?

Type your answer here:

Question 3

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;
  
  // constructor not shown, but guaranteed to be correct

  @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
}

There are two things wrong with the equals implementation; what are they?