Written Final Exam Review Questions (Without 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 {}
    
  6. What is wrong with the following code, if anything?
    public abstract class Class5 {
      public abstract void m();
    }
    
    public class Class6 extends Class5 {}
    
  7. What is wrong with the following code, if anything?
    public abstract class Class7 {
      public abstract void m();
    }
    
    public abstract class Class8 extends Class7 {}
    
  8. What is wrong with the following code, if anything?
    public interface Interface1 {
      public void m();
    }
    
    public abstract class Class9 implements Interface1 {}
    
  9. What is wrong with the following code, if anything?
    public interface Interface2 {
      public void m();
    }
    
    public class Class10 implements Interface2 {}
    
  10. 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();
      }
    }
    
  11. 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();
      }
    }
    
  12. 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);
      }
    }
    
  13. What is wrong with the following code, if anything?
    public class Class18 {}
    
    public class Class19 {
      public static void m() throws Class18 {
        throw new Class18();
      }
    }
    
  14. What is wrong with the following code, if anything?
    public class Class20 extends Exception {}
    
    public class Class21 {
      public static void m() {
        throw new Class20();
      }
    }
    
  15. 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;
      }
    }
    
  16. 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;
      }
    }
    
  17. 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;
        }
      }
    }
    
  18. 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());
        }
      }
    }
    
  19. 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();
      }
    }