Final Review Guide
In Addition to the Below Review Guide, Study Midterm 1 and Midterm 2 Review Guides:

Exception Handling

  • What are the three types of exceptional conditions that can occur?
  • What is the difference between a checked and unchecked exception?
  • What will the following program display to the console?
public class ExceptionPropagation {

public static void main(String[] args) {
try {
methodD();
} catch(Exception e) {
System.out.println("Caught exception in main");
System.out.println(e.getMessage());
}
}

public static void methodD() throws Exception {
try {
methodC();
throw new Exception("Throwing exception in D");
} catch (Exception e) {
System.out.println("Caught exception in D");
System.out.println(e.getMessage());
}
}

public static void methodC() throws Exception {
methodB();
}

public static void methodB() throws Exception {
methodA();
}

public static void methodA() throws Exception {
throw new Exception("Throwing exception in A");
}
}
  • Update the below method so that it throws an IllegalStateException when the List is empty, writing an error message that is descriptive of the problem and also states the name of the method where the problem has occurred:
    /**
     * Returns the data stored at the head node
     * @return the first element
     * @throws IllegalStateException when List is empty
     */
    public E getHead()
    {
      return head.data; 
    }
  • Update the below method so that it throws an IndexOutOfBoundsException when the specified index is out of the bounds of the ArrayList class, writing an error message that is descriptive of the problem and also states the name of the method where the problem has occurred:
   
    /**
     * Returns the item stored at
     * the given index
     * @param index the index of the
     * item to return
     * @return a random item
     * @throws IndexOutOfBoundsException
     * if index < 0 or index > numItems

     */
    public E get(int index)
    {
        return array[index];
    }

  • Now, call each of the above methods inside of a try-catch. When you catch the exception, print out the error message with a call to getMessage(), printStackTrace() and toString() (i.e. practice calling all 3 methods on the exception).
  • Add a try with two catch blocks to the below code to handle the FileNotFoundException that may be thrown by the Scanner constructor and to handle the NullPointerException that may be thrown by the File constructor
public static void main(String[] args) {
    File file = new File("in.txt");
    Scanner input = new Scanner (file);
    double num = input.nextDouble();
    System.out.println("Double stored in file: " + num);
    input.close()
}

  • Which one of the exceptions above is considered a checked exception?
  • Now, handle the InputMismatchException that may be thrown by the nextInt() method by also catching this exception.


String and StringBuilder

  • How many unique Strings will be created after running the code below and what will the below program display to the console?
public static void main(String[] args) {
    String one = "Apple";
    String two = "Mango";
    String three = new String("Apple");
    String four = "Apple";
    String five = "apple";
   
    System.out.println(one == three);
    System.out.println(one.equals(three));
    System.out.println(one == four);
    System.out.println(one == "Apple");
    System.out.println(one == five);
       
}

  • What will the following display to the console? Work out the solution by hand (don't run the code except to double-check your work). Make sure you understand why you are getting that particular output (as you will have to answer a similar question on the final).

          StringBuilder sb1 = new StringBuilder("Tree");
        StringBuilder sb2 = new StringBuilder(sb1.append("Maple").insert(2, "Sequoia"));
        sb2.replace(1, 3, "Redwood");
        System.out.println(sb1);
        System.out.println(sb2);