1. x = 1  // member variable set to 1 via constructor
   x = 2  // argument shadows member variable
   x = 3  // local variable shadows member variable
   x = 0  // member variable intialized to 0
   x = 1  // local (in main) x has not changed since initialization

   Two points for each correct answer.

2.  

    disambiguate counter vs. bound [3pts]
    move fact initialisation out of loop [2pts]
    correct fact initialisation [2pts]
    correct loop bounds [3pts]

    public class Factorial {
      static int i = 3;
    
      public static void main(String[] args) {
        System.out.println("i = " + i);
        int fact = 1;
        for (int j = 1; j <= i; j++) {
          System.out.println("j = " + j);
          fact = j * fact;
        }
        System.out.println("The factorial is " + fact);
      }
    }

3.  You need to comment out the last two lines (4pts).

    The program will print:

      In class A 1
      In class B 3
      In class B 3
    
    Each correct answer is worth 2pts.

4.  B: (3pts)
      this.name = name;
      this.street = street;
      this.avenue = avenue;
    C: (3pts)
      street += dx;
      avenue += dy;
    D1: (1pt)
      move(-1, 0); // north
    D2: (1pt)
      move(1, 0);  // south
    D3: (1pt)
      move(0, -1); // west
    D4: (1pt)
      move(0, 1);  // east
    E: (3pts)
      return "Name: " + name + "\n" +
             "Street: " + street + "\n" +
             "Avenue: " + avenue + "\n";
    F: (4pts)
      super(name, street, avenue);
    G1: (1pt)
      goN();
      goW();
    G2: (1pt)
      goN();
      goE();
    G3: (1pt)
      goS();
      goW();
    G4: (1pt)
      goS();
      goE();
    H: (1pt)
      private int load;
    I: (4pts)
      super(name, street, avenue);
      this.load = load;
    J: (4pts)
      return super.toString() + 
             "Load: " + load + "\n";

5. 
    (a):

    public static void main(String[] args) {
        Car car = new Car("My Car", -2, 3); // 2pts
        Truck truck = new Truck("My Truck", 2, 5, 100); // 2pts
        System.out.println("Moving car:"); 
        car.goSW();
        car.goSW();
        for (int i = 0; i < 6; i++) {           // 2pts
            car.goS();
        }
        System.out.println(car);
        System.out.println("Moving truck:");
        for (int i = 0; i < 4; i++)
            truck.goS();
        for (int i = 0; i < 4; i++)            // 2pts
            truck.goW();
        System.out.println(truck);
    }

    Both vehicles take eight steps to reach the target.  (2pts)

    (b): 2pts each of:

    i.    none
    ii.   v.move(1, 1)
    iii.  v.move(1, 1)
    iv.   v.move(1, 1)
    v.    v.move(1, 1) and System.out.println("Street: " + v.street)

6.

    public static int[] evenRun(int[] nums) {
        int length = nums.length;
        int[] res  = new int[length];

        boolean foundEven = false;
        int  lastEven  = 0;

        for (int i = 0; i < length; i++)
            if (nums[i] % 2 == 0) {
                foundEven = true;
                lastEven  = nums[i];
                res[i]    = nums[i];
            } else {
                if (foundEven) 
                    res[i] = lastEven;
                else
                    res[i] = nums[i];
            }

        return res;
    }
