Final Review Questions (without Answers)

The final exam will be broken into two components:

The lab-based portion will require you to write code on the lab machines, which is to be turned in via Canvas by the end of the class. It will be similar in style to the rest of the assignments in the course. You may NOT reference your previous submissions, and you may NOT access the Internet for any reason other than to submit your solution via Canvas. Violations of these rules will result in a 0 on the exam.

The written portion will require you to read and understand code, as well as answer short-answer questions related to programming. You may also be asked to write short amounts of code.

For both portions of the exam, you may bring in two 8 1/2 x 11 inch sheets of paper containing handwritten notes, and you may write on both sides of the paper. If you so choose, you can bring in a different sheet of paper for each portion of the exam (they are on separate days, and you can keep it).

The exam is cumulative, but it is biased towards material covered since the last exam (labs 13-24). The exam will be composed of questions based off of the following sources:

All topics which could potentially be on the exam are somehow covered by one of the above sources.

Questions

  1. What is wrong with the following code, if anything?
    public class Class1 {
      private int x;
      public Class1(int y) {
        y = x;
      }
    }
    
  2. What is wrong with the following code, if anything?
    public class Class2 {
      private int x;
      public Class2(int x) {
        this.x = x;
      }
    }
    
    public class Class3 extends Class2 {}
    
  3. What is wrong with the following code, if anything?
    public class Class4 {}
    
    public class Class5 extends Class4 {
      private int x;
      public Class5(int x) {
        super();
        this.x = x;
      }
    }
    
  4. What is wrong with the following code, if anything?
    public class Class6 {
      private int x;
      public Class6(int x) {
        this.x = x;
      }
    }
    
    public class Class7 extends Class6 {
      public Class7(int x) {
        super(x);
      }
      public int getX() {
        return x;
      }
    }
    
  5. What is wrong with the following code, if anything?
    public class Class8 {
      protected int x;
      public Class8(int x) {
        this.x = x;
      }
    }
    
    public class Class9 extends Class8 {
      public Class9(int x) {
        super(x);
      }
      public int getX() {
        return x;
      }
    }
    
  6. What is wrong with the following code, if anything?
    public abstract class Class10 {
      public abstract void m();
    }
    
    public class Class11 extends Class10 {}
    
  7. What is wrong with the following code, if anything?
    public abstract class Class12 {
      public abstract void m();
    }
    
    public abstract class Class13 extends Class12 {}
    
  8. What is wrong with the following code, if anything?
    public interface Interface1 {
      public void m();
    }
    
    public abstract class Class14 implements Interface1 {}
    
  9. What is wrong with the following code, if anything?
    public interface Interface2 {
      public void m();
    }
    
    public class Class15 implements Interface2 {}
    
  10. What is wrong with the following code, if anything?
    public class Class16 {}
    public class Class17 extends Class16 {
      public static void m() {
        Class16 x = new Class17();
      }
    }
    
  11. What is wrong with the following code, if anything?
    public class Class18 {
      private int x;
      public Class18(int x) {
        this.x = x;
      }
      public static int getX() {
        return x;
      }
    }
    
  12. What is wrong with the following code, if anything?
    public class Class19 {
      public static int x;
      public Class19(int y) {
        x = y;
      }
      public int getX() {
        return x;
      }
    }
    
  13. What is the output of the main method of Class22 below?
    public class Class20 {
      public void m() {
        System.out.println("foo");
      }
    }
    
    public class Class21 extends Class20 {
      public void m() {
        System.out.println("bar");
      }
    }
    
    public class Class22 {
      public static void main(String[] args) {
        Class20 x = new Class20();
        Class20 y = new Class21();
        Class21 z = new Class21();
    
        x.m();
        y.m();
        z.m();
      }
    }
    
  14. What is the output of the main method of Class24 below?
    public class Class23 extends Exception {}
    
    public class Class24 {
      public static void throwException() throws Class23 {
        throw new Class23();
      }
    
      public static void main(String[] args) {
        System.out.println(1);
        try {
          System.out.println(2);
          throwException();
          System.out.println(3);
        } catch (Class23 e) {
          System.out.println(4);
        }
        System.out.println(5);
      }
    }
    
  15. What is the output of the main method of Class26 below?
    public class Class25 extends Exception {}
    
    public class Class26 {
      public static void throwException() throws Class25 {
        throw new Class25();
      }
    
      public static void main(String[] args) {
        System.out.println(1);
        try {
          System.out.println(2);
          throwException();
          System.out.println(3);
        } catch (Class25 e) {
          System.out.println(4);
        } finally {
          System.out.println(5);
        }
        System.out.println(6);
      }
    }
    
  16. What is wrong with the following code, if anything?
    public class Class27 {}
    
    public class Class28 {
      public static void m() throws Class27 {
        throw new Class27();
      }
    }
    
  17. What is wrong with the following code, if anything?
    public class Class29 extends Exception {}
    
    public class Class30 {
      public static void m() {
        throw new Class29();
      }
    }
    
  18. What is wrong with the following code, if anything?
    public class Class31 extends Exception {}
    
    public class Class32 {
      public static int m() throws Class31 {
        return 42;
      }
    }
    
  19. What is wrong with the following code, if anything?
    public class Class33 extends Exception {}
    
    public class Class34 {
      public static void throwsException() throws Class33 {
        throw new Class33();
      }
      public static int m() throws Class33 {
        try {
          throwsException();
        } catch (Class33 e) {
          return 1;
        }
        return 0;
      }
    }
    
  20. What is wrong with the following code, if anything?
    public class Class35 extends Exception {}
    
    public class Class36 {
      public static void throwsException() throws Class35 {
        throw new Class35();
      }
      public static int m() throws Class35 {
        try {
          throwsException();
        } catch (Class35 e) {
          return 1;
        }
      }
    }