Welcome te  |  courses  |  logout

IT219 - Homework 1

1) When you run the following code, what will be displayed on the console?

class Test {
    public static void main(String[] args) {
      try {
        method();
        System.out.println("After the method call");
      }
      catch (RuntimeException ex) {
        System.out.println("RuntimeException");
      }
      catch (Exception ex) {
        System.out.println("Exception");
      }
    }

    static void method() throws Exception {
      try {
        String s = "5.6";
        Integer.parseInt(s); // Cause a NumberFormatException

        int i = 0;
        int y = 2 / i;
        System.out.println("Welcome to Java");
      }
      catch (RuntimeException ex) {
        System.out.println("RuntimeException");
      }
      catch (Exception ex) {
        System.out.println("Exception");
      }
    }
}
The program will display RuntimeException twice.
The program will display Exception twice.
The program will display Exception followed by RuntimeException.
The program will display RuntimeException followed by after the method call.
The program will have a compilation error.
 
2) What exception type does the following program throw?

public class Test {
    public static void main(String[] args) {
      String s = "abc";
      System.out.println(s.charAt(3));
    }
}
ArithmeticException
ClassCastException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
No exception
 
3) An instance of _________ describes the errors caused by your program and
external circumstances. These errors can be caught and handled by your program.
RuntimeException
NumberFormatException
Exception
Throwable
Error
 
4) What exception type does the following program throw?

public class Test {
    public static void main(String[] args) {
      Object o = new Object();
      String d = (String)o;
    }
}
ArithmeticException
ClassCastException
StringIndexOutOfBoundsException
ArrayIndexOutOfBoundsException
No exception
 
5) Which of the following is not a correct assertion statement?
assert sum > 10 && sum < 5 * 10 : "sum is " + sum;
assert "sum is " + sum;
assert (i > 10);
None of the above
 
6) When you run the following program, will be displayed on the console?

class Test {
    public static void main (String[] args) {
      try {
        System.out.println("Welcome to Java");
      }
      finally {
        System.out.println("The finally clause is executed");
      }
    }
}
Welcome to Java (followed by) The finally clause is executed (on the next line)
Welcome to Java
The finally clause is executed
None of the above
 
7) When you run the following program, what will be displayed on the console?

class Test {
    public static void main (String[] args) {
      try {
        System.out.println("Welcome to Java");
        return;
      }
      finally {
        System.out.println("The finally clause is executed");
      }
    }
}
Welcome to Java
Welcome to Java (followed by) The finally clause is executed (on the next line)
The finally clause is executed
None of the above
 
8) When you run the following program, what will be displayed on the console?

class Test {
    public static void main(String[] args) {
      try {
        System.out.println("Welcome to Java");
        int i = 0;
        int y = 2/i;
        System.out.println("Welcome to Java");
      }
      catch (RuntimeException ex) {
        System.out.println("Welcome to Java");
      }
      finally {
        System.out.println("End of the block");
      }

      System.out.println("End of the block");
    }
}
The program will display Welcome to Java two times followed by End of the block two times.
The program will disply Welcome to Java two times followed by End of the block.
You cannot catch RuntimeException errors.
The program will display Welcome to Java three times followed by End of the block.
 
9) When you run the following program, what will be displayed on the console?

class Test {
    public static void main(String[] args) {
      try {
        method();
        System.out.println("After the method call");
      }
      catch (RuntimeException ex) {
        System.out.println("RuntimeException");
      }
      catch (Exception ex) {
        System.out.println("Exception");
      }
    }

    static void method() throws Exception {
      try {
        String s = "5.6";
        Integer.parseInt(s); // Cause a NumberFormatException

        int i = 0;
        int y = 2 / i;
        System.out.println("Welcome to Java");
      }
      catch (NumberFormatException ex) {
        System.out.println("NumberFormatException");
        throw ex;
      }
      catch (RuntimeException ex) {
        System.out.println("RuntimeException");
      }
    }
}
The program will have a compilation error.
The program will display NumberFormatException twice.
The program will display NumberFormatException followed by RuntimeException.
The program will display NumberFormatException followed by After the method call.
 
10) What exception type will the following program throw?

public class Test {
    public static void main(String[] args) {
      Object o = null;
      System.out.println(o.toString());
    }
}
StringIndexOutOfBoundsException
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
ClassCastException
 
11) Analyze the following code:

class Test {
    public static void main(String[] args) {
      try {
        int zero = 0;
        int y = 2/zero;
        try {
          String s = "5.6";
          Integer.parseInt(s); // Cause a NumberFormatException
        }
        catch(Exception e) {
          }
        }
        catch(RuntimeException e) {
          System.out.println(e);
        }
      }
    }

    Which of the following statements is true?
A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.
The program has a compilation error because Exception appears before RuntimeException.
A try-catch block cannot be embedded inside another try-catch block.
None of the above.
 
12) The following code:

int number = Integer.MAX_VALUE + 1;

causes Java to throw _________.
an error
an exception
no exceptions
a throwable
RuntimeException
 
13) An instance of _________ describes system errors. If this type of error occurs,
there is little you can do beyond notifying the user and trying to terminate the
program gracefully.
RuntimeException
Exception
NumberFormatException
Throwable
Error
 
14) Which of the following statements are true? (Hint: There are multiple answers.)
    
If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.
You use the keyword throws to declare exceptions in the method heading.
A method may declare to throw multiple exceptions.
To throw an exception, use the key word throw.
 
15) Which of the following statements is correct?
Do not use assertions for argument checking in public methods.
Exception handling addresses robustness whereas assertion addresses correctness.
Use assertions to reaffirm assumptions.
Exception handling deals with unusual circumstances during program execution. Assertions are intended to ensure the correctness of the program.
All of the above.
 
16) When you run the following program, what will be displayed on the console?

class Test {
    public static void main(String[] args) {
      try {
        System.out.println("Welcome to Java");
        int i = 0;
        int y = 2/i;
        System.out.println("Welcome to Java");
      }
      catch (RuntimeException ex) {
        System.out.println("Welcome to Java");
      }
      finally {
        System.out.println("End of the block");
      }
    }
}
The program will display Welcome to Java two times.
The program will display Welcome to Java three times followed by End of the block.
The program will display Welcome to Java two times followed by End of the block.
The program will display Welcome to Java three times.
 
17) A method must declare to throw ________.
checked exceptions
unchecked exceptions
Error
RuntimeException
 
18) Analyze the following code:

class Test {
    public static void main(String[] args)
        throws MyException {
      System.out.println("Welcome to Java");
    }
}

class MyException extends Error {
    }

    Which of the following sentences is true?
You cannot declare an exception in the main method.
You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.
The program has a compilation error.
You declared an exception in the main method, but you did not throw it.
 
19) Which of the following statements is correct to rethrow exception ex along with
new information?
throw new Exception(ex, "New info");
throw ex; throw new Exception("Some new info");
throw new Exception("New info"); throw ex;
throw new Exception("New info", ex);
 
20) Analyze the following code:

class Test {
    public static void main(String[] args) {
      try {
        String s = "5.6";
        Integer.parseInt(s); // Cause a NumberFormatException

        int i = 0;
        int y = 2 / i;
      }
      catch (Exception ex) {
        System.out.println("NumberFormatException");
      }
      catch (RuntimeException ex) {
        System.out.println("RuntimeException");
      }
    }
}

Which of the following sentences is true?
The program will have a compilation error.
The program will display RuntimeException.
The program will display NumberFormatException.
The program will display NumberFormatException followed by RuntimeException.