EECS2030A Test 1A

Thu Sep 22, 16:00-16;30
LAB 1
Version A


Programming question

Implement this API. You do not have to include javadoc comments. If you do not know the value of the constant, just choose a value yourself.

public class Suitcase {

    private Suitcase() {}

    public static final double LIMIT = 23.5;

    /**
     * Checks whether the given weight is over the limit.
     *
     * @param weight - the weight of a suitcase.
     * @pre. weight >= 0
     * @return true if the given weight is over the limit, false otherwise.
     */

    public static boolean isOverLimit(double weight) {
        if (weight > LIMIT) {
            return true;
        } else {
            return false;
        }
    }
}

Multiple choice question

Question 1 Consider the following code snippet.
int a = 0;
int b = 1;
Utility.swapper(a, b);
System.out.printf("%d %d", a, b);

Which of the following statements is correct?

A. The above snippet prints the string "1 0".
B. The above snippet prints the string "0 1".
C. What the snippet prints depends on the body of the swapper method.
D. None of the above.

Answer:
The answer is B. The printf statement prints the values of a and b after the method Utility.swapper has been run. It is impossible for a Java method to change the value of a variable that belongs to the client (in this case, the variables a and b) because Java uses pass-by-value to transfer information between the client and the method. Pass-by-value means that the value of a (0) and the value of b (1) are copied to the method Utility.swapper, but the method has no knowledge of the variables named a and b.