Final Review Questions (with 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;
      }
    }
    
    Intention seems to be x = y; this does not set the instance variable 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 {}
    
      Class3 does not define a constructor that takes an int.
    
  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;
      }
    }
    
    Nothing wrong; Class4 implicitly has a no-argument constructor defined.
    It is ok if subclasses add new constructors.
    
  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;
      }
    }
    
    x is private in Class6, so it cannot be accessed in Class7.
    
  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;
      }
    }
    
    No problems; Class9 now has access to x, since x is protected.
    
  6. What is wrong with the following code, if anything?
    public abstract class Class10 {
      public abstract void m();
    }
    
    public class Class11 extends Class10 {}
    
    Class11 does not override m.
    
  7. What is wrong with the following code, if anything?
    public abstract class Class12 {
      public abstract void m();
    }
    
    public abstract class Class13 extends Class12 {}
    
    No problem; since Class13 is abstract it does not need to implement all abstract methods.
    
  8. What is wrong with the following code, if anything?
    public interface Interface1 {
      public void m();
    }
    
    public abstract class Class14 implements Interface1 {}
    
    No problem; since Class14 is declared abstract, it does not need to override m.
    
  9. What is wrong with the following code, if anything?
    public interface Interface2 {
      public void m();
    }
    
    public class Class15 implements Interface2 {}
    
    Class15 is not abstract and does not override m.
    
  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();
      }
    }
    
    No problems.  Class17 is-a Class16, so an instance of Class17 can be assigned to a variable of type Class16.
    
  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;
      }
    }
    
    getX() is declared static, so it does not have access to the non-static instance variable 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;
      }
    }
    
    Technically nothing; the code will compile since instance methods have access to static items.
    However, it's strange; getX() above could have been declared static for instance, and the name getX() implies that this is a getter for an instance variable.
    
  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();
      }
    }
    
    foo
    bar
    bar
    
  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);
      }
    }
    
    1
    2  
    4
    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);
      }
    }
    
    1
    2  
    4
    5
    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();
      }
    }
    
    Class27 does not inherit from Exception.
    
  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();
      }
    }
    
    Method m of Class30 is not annotated with throws Class29, but it does throw 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;
      }
    }
    
    Method m of Class32 is annotated to throw Class31, but it will never throw Class31.
    
  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;
      }
    }
    
    While a Class33 exception is thrown within the m method of Class34, this exception will always be caught, so m will never throw it.
    The throws Class33 annotation on m needs to be removed.
    
  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;
        }
      }
    }
    
    This will not compile, as Java is not smart enough to know that throwsException will always throw an exception.
    If throwsException happened to not throw an exception, method m would not return anything.
    However, m must return an int.