25 Feb 2012 [ 201 week8 ]

The exam will cover all of the review material, and the basics of how to design classes (everything we covered up through last Tuesday). This includes all or part of the following chapters (which might have different chapter numbers, depending on the version you're using):

The best place to start reviewing is doing exercises from those chapters. Also, don't forget to make your page of notes (front and back).

Example exam problems

  1. Define a class called Point2D that represents a point in the x-y plane. Ensure that instances of the class are immutable by making all member variables private. Define the following methods:

    • dist(Point q): p.dist(q) should return the distance between p and q. Recall that the distance between two points is and that Math.sqrt provides the square root function.
    • closest(Point[] points): should return the closest point in the input array.
  2. What does the following code print?

    int x = 5;
    int y = 10;
    if (x > y) {
        System.out.println("a");
        x += 1;
    else {
        System.out.println("b");
        x += 2;
    }
    System.out.println(x);
    
  3. What is the value of g(12, 9) if g is the following method?

    public static int g(int a, int b) {
        if (b == 0) {
            return a;
        }
        return g(b, a % b);
    }
    
  4. What does the following code print?

    public class Counter {
        private int count = 0;
    
        public Counter() {
        }
    
        public void add() {
            count++;
        }
    
        public int getCount() {
            return count;
        }
    
        public static void main(String[] args) {
            Counter c = new Counter();
            System.out.println(c.getCount());
            c.add();
            c.add();
            System.out.println(c.getCount());
        }
    }
    
  5. Define a static method called max that takes 3 int inputs and returns the largest.

  6. What is the value of z at the end of the following program?

    int[] x = { 1, 2, 3, 4, 5 };
    int[] y = x;
    x[3] = 11;
    int q = y[3];
    int z = 5 * q;
    
  7. Define a method that takes an array of integers as input, and returns a new array with elements double that of the input. For example, with input {2, 5, 3} the output should be {4, 10, 6}.

This post will be updated with additional practice questions later.