Written Final Exam Review Questions (With Answers)

The written final exam will be Wednesday, May 16, 2018 from 10:15 AM - 12:15 PM in JD 1600A. 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.

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.

The exam is cumulative, but it is biased towards material covered since the last exam (labs 17-22). 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.

The exam is purely individual effort; any violations will result in a 0 on the exam.

Questions

  1. Consider the following code snippet:

    int[] arr1 = new int[]{1, 2, 3};
    int[] arr2 = arr1;
    

    Write a memory diagram representing how memory “looks” after the above code snippet is executed.

  2. Consider the following code snippet:

    int[] arr1 = new int[]{1, 2, 3};
    int[] arr2 = arr1;
    arr1[0] = 5;
    arr2[2] = 7;
    

    Write a memory diagram representing how memory “looks” after the above code snippet is executed.

  3. Consider the following code snippet:

    int[] a = new int[]{4, 5, 6};
    int[] b = new int[]{7, 8};
    int[] c = new int[]{9};
    int[] d = new int[0];
    int[][] arr = new int[][]{a, b, c, d};
    

    Write a memory diagram representing how memory “looks” after the above code snippet is executed.

  4. Consider the following code snippet:

    public class TwoInstance {
      private int x;
      private Object obj;
    
      public TwoInstance(int x, Object obj) {
        this.x = x;
        this.obj = obj;
      }
    
      public static void main(String[] args) {
        TwoInstance first = new TwoInstance(3, new Object());
        Object temp = new Object();
        TwoInstance second = new TwoInstance(7, temp);
        // HERE
        System.out.println();
      }
    }
    

    Assume we run the main method of the above program. Write a memory diagram representing how memory “looks” when // HERE is reached. You do not need to include args or this in your diagram.

  5. What is wrong with the following code, if anything?
    public class Class1 {
      private int x;
      public Class1(int x) {
        this.x = x;
      }
    }
    
    public class Class2 extends Class1 {}
    
      Class2 does not define a constructor that takes an int.
    
  6. What is wrong with the following code, if anything?
    public class Class3 {
      protected int x;
      public Class3(int x) {
        this.x = x;
      }
    }
    
    public class Class4 extends Class3 {
      public Class4(int x) {
        super(x);
      }
      public int getX() {
        return x;
      }
    }
    
    No problems; Class4 now has access to x, since x is protected.
    
  7. What is wrong with the following code, if anything?
    public abstract class Class5 {
      public abstract void m();
    }
    
    public class Class6 extends Class5 {}
    
    Class6 does not override m.
    
  8. What is wrong with the following code, if anything?
    public abstract class Class7 {
      public abstract void m();
    }
    
    public abstract class Class8 extends Class7 {}
    
    No problem; since Class8 is abstract it does not need to implement all abstract methods.
    
  9. What is wrong with the following code, if anything?
    public interface Interface1 {
      public void m();
    }
    
    public abstract class Class9 implements Interface1 {}
    
    No problem; since Class9 is declared abstract, it does not need to override m.
    
  10. What is wrong with the following code, if anything?
    public interface Interface2 {
      public void m();
    }
    
    public class Class10 implements Interface2 {}
    
    Class10 is not abstract and does not override m.
    
  11. What is wrong with the following code, if anything?
    public class Class11 {}
    public class Class12 extends Class11 {
      public static void m() {
        Class11 x = new Class12();
      }
    }
    
    No problems.  Class12 is-a Class11, so an instance of Class12 can be assigned to a variable of type Class11 (polymorphism).
    
  12. What is the output of the main method of Class13 below?
    public class Class14 {
      public void m() {
        System.out.println("foo");
      }
    }
    
    public class Class15 extends Class14 {
      public void m() {
        System.out.println("bar");
      }
    }
    
    public class Class13 {
      public static void main(String[] args) {
        Class14 x = new Class14();
        Class14 y = new Class15();
        Class15 z = new Class15();
    
        x.m();
        y.m();
        z.m();
      }
    }
    
    foo
    bar
    bar
    
  13. What is the output of the main method of Class16 below?
    public class Class17 extends Exception {}
    
    public class Class16 {
      public static void throwException() throws Class17 {
        throw new Class17();
      }
    
      public static void main(String[] args) {
        System.out.println(1);
        try {
          System.out.println(2);
          throwException();
          System.out.println(3);
        } catch (Class17 e) {
          System.out.println(4);
        }
        System.out.println(5);
      }
    }
    
    1
    2  
    4
    5
    
  14. What is wrong with the following code, if anything?
    public class Class18 {}
    
    public class Class19 {
      public static void m() throws Class18 {
        throw new Class18();
      }
    }
    
    Class18 does not inherit from Exception.
    
  15. What is wrong with the following code, if anything?
    public class Class20 extends Exception {}
    
    public class Class21 {
      public static void m() {
        throw new Class20();
      }
    }
    
    Method m of Class21 is not annotated with throws Class20, but it does throw Class20.
    
  16. What is wrong with the following code, if anything?
    public class Class22 extends Exception {}
    
    public class Class23 {
      public static int m() throws Class22 {
        return 42;
      }
    }
    
    Method m of Class23 is annotated to throw Class22, but it will never throw Class22.
    
  17. What is wrong with the following code, if anything?
    public class Class24 extends Exception {}
    
    public class Class25 {
      public static void throwsException() throws Class24 {
        throw new Class24();
      }
      public static int m() throws Class24 {
        try {
          throwsException();
        } catch (Class24 e) {
          return 1;
        }
        return 0;
      }
    }
    
    While a Class24 exception is thrown within the m method of Class25, this exception will always be caught, so m will never throw it.
    The throws Class24 annotation on m needs to be removed.
    
  18. What is wrong with the following code, if anything?
    public class Class26 extends Exception {}
    
    public class Class27 {
      public static void throwsException() throws Class26 {
        throw new Class26();
      }
      public static int m() throws Class26 {
        try {
          throwsException();
        } catch (Class26 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.
    
  19. What is wrong with the following code, if anything?
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Class28 {
      public static void main(String[] args) {
        try {
          Scanner input = new Scanner(new File("input.txt"));
          if (input.hasNextLine()) {
            System.out.println(input.nextLine());
          }
        } catch(FileNotFoundException e) {
          System.out.println(e.getMessage());
        }
      }
    }
    
    Does not close the input file (missing input.close()).
    
  20. What is wrong with the following code, if anything?
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Class29 {
      public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("input.txt"));
        if (input.hasNextLine()) {
          System.out.println(input.nextLine());
        }
        input.close();
      }
    }
    
    In the event that there is a problem reading the file (but not opening the file), this will fail to close the file.
    The .close() should be wrapped in a finally block, as in this example.