1. Consider the following program:
    void main() {
        System.out.println("main begins");
        try {
        } catch (Ex1 ex) {
            System.out.println("main caught Ex1");
        } catch (Ex3 ex) {
            System.out.println("main caught Ex3");
        } finally {
            System.out.println("main finally");
        System.out.println("main completes");
    void a() {
        System.out.println("a begins");
        try {
            if (condForEx1) {
            } else {
                throw new Ex1();
        } catch (Ex2 ex) {
            System.out.println("a caught Ex2");
        System.out.println("a completes");
    void b() {
        System.out.println("b begins");
        try {
            if (condForEx2) throw new Ex2();
            if (condForEx3) throw new Ex3();
        } catch (Ex3 ex) {
            System.out.println("b caught ex3");
        } finally {
            System.out.println("b finally");
        System.out.println("b completes");

    For each part below determine the complete output that would be output if the code above was run with the values of the condForEx variables as specified below. Assume the exception classes Ex1, Ex2, and Ex3 each extend RuntimeException. If an exception is passed out of main, show the output of the runtime environment as "Program terminated due to Exception ExN  ", where N is the particular exception number.

    1. What would be output if condForEx1, condForEx2, and condForEx3 are all false?
    2. What would be output if condForEx1 is true and condForEx2 and condForEx3 are false?
    3. What would be output if condForEx1 and condForEx2 are true and condForEx3 is false?
    4. What would be output if condForEx1 and condForEx3 are true and condForEx2 is false?
    5. How would the code need to be modified if exception type Ex2 were a checked exception?

  2. Consider the following secret generic method:

       public static <E> boolean secret(Listnode<E> chain1, Listnode<E> chain2) {
          while (chain1 != null && chain2 != null) {
             if (chain1.getData().equals(chain2.getData())) {
                chain1 = chain1.getNext();
                chain2 = chain2.getNext();
             else return false;
          if (chain1 == null && chain2 == null) return true;
          return false;
    1. What is returned by method secret() given the contents of the chains of nodes described below. To get the maximum practice from this question, carefully trace the code rather than attempting to run it.

      1. chain1 is null and chain2 is null
      2. chain1 contains Integers 11, 22, 33 and chain2 contains Integers 33, 22, 11
      3. chain1 contains Integers 33, 55, 77 and chain2 also contains Integers 33, 55, 77
      4. chain1 contains Integers 99, 22 and chain2 contains Integers 99, 22, 44
      5. both chain1 and chain2 contain Strings "This", "is, "a", "test"
      6. chain1 contains Strings "Hello", "from", "the", "outside" and chain2 contains "Hello", "from", "the", "other side"
      7. chain1 contains Strings "The Cat", "in", "the Hat" and chain2 is null

    2. In one line describe the purpose of the secret() method. Be very precise in your description and do not explain the steps in the code.

  3. Complete the Java method specified below. Assume this method is implemented in a main class.
    Hint: Consider using two references to step through the singly linked list and think carefully about when to advance them.