Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 9 Exception Handling – absolute java test bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 9
Exception Handling
 Multiple Choice
1) Try blocks contain code that could possibly:
(a) handle an exception
(b) throw an exception
(c) catch an exception
(d) display an exception

2) The Exception class belongs to the package:
(a) java.io
(b) java.util
(c) java.lang
(d) java.except

3) The execution of a throw statement is referred to as:
(a) catching a block
(b) trying a block
(c) handling an exception
(d) throwing an exception

4) The catch block has ________ parameters.
(a) zero
(b) one
(c) two
(d) three

5) A ___________ block should immediately follow a try block.
(a) try
(b) catch
(c) fail
(d) final

6) When defining your own exception class, you extend an existing exception class. This is an example of:
(a) polymorphism
(b) encapsulation
(c) inheritance
(d) all of the above

7) If a method does not catch an exception, then it must at least warn programmers that any invocation of the method might possibly throw an exception. This warning is called a/an:
(a) Exception handler
(b) Throws clause
(c) Try block
(d) Catch block

8) If a method throws an exception, and the exception is not caught inside the method, then the method invocation:
(a) terminates
(b) transfers control to the catch block
(c) transfers control to the exception handler
(d) none of the above

9) Methods that process String arguments as if they were numbers could possibly throw a/an _________ exception.
(a) NumberFormatException
(b) NullPointerException
(c) both a and b
(d) none of the above

10) Which circumstance is an exception to the catch or declare rule?
(a) Exceptions that result from errors of some sort.
(b) Exceptions that are descendents of the class RuntimeException.
(c) Both A and B
(d) None of the above

11) Exceptions that are subject to the catch or declare rule are called:
(a) Checked exceptions
(b) Unchecked exceptions
(c) Fatal exceptions
(d) All of the above

12) A runtime exception is a/an:
(a) checked exception
(b) unchecked exception
(c) offending exception
(d) none of the above

13) All exceptions are descendants of the class:
(a) Throwable
(b) Catchable
(c) Tryable
(d) Blockable

14) Exception handling is an example of a programming methodology known as:
(a) structured programming
(b) object oriented programming
(c) goto programming
(d) event-driven programming

15) A _________ block executes regardless of whether an exception occurs.
(a) final
(b) finally
(c) catch
(d) none of the above

16) When using the Scanner class one should account for a/an:
(a) NumberFormatException
(b) InputMismatchException
(c) ArrayIndexOutOfBoundsException
(d) PowerFailureException

17) ArrayIndexOutOfBoundsException is a descendent of the class RuntimeException. This means:
(a) the exception must be caught
(b) a finally block must be included
(c) the exception does not have to be explicitly caught
(d) none of the above

18) An exception is caught in a _________ block.
(a) try
(b) catch
(c) finally
(d) all of the above

 True/False
1) The throw operator causes a change in the flow of control.

2) The basic way of handling exceptions in Java consists of the try-catch-throw trio.

3) When an exception is thrown, the code in the surrounding try block continues executing and then the catch block begins execution.

4) Every exception class is an ancestor of the class Exception.

5) The two most important things about an exception object are its type and the message that it carries in an instance variable of type String.

6) A program can catch multiple exceptions.

7) The compiler does not complain when the catch or declare rule is ignored.

8) Exceptions that must follow the Catch or Declare Rule are often called unchecked exceptions.

9) In event-driven programming, sending an event is called firing the event.

10) You can not place a try block and its following catch blocks inside a larger try block or inside a larger catch block.

11) The finally block contains code to be executed whether or not an exception is thrown in a try block.

 Short Answer/Essay
1) Write a Java statement that throws a new exception with the String: File Not Found.
Answer: throw new Exception(“File Not Found!”);
2) Write a Java statement that throws a new exception with the String: Disk drive not ready.
Answer: throw new Exception(“Disk Drive Not Ready!”);
3) Use a catch block to display the exception thrown in number one above.
Answer:
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
4) Use a catch block to display the exception thrown in number two above.
Answer:
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
5) Define an exception class called FileNotFoundException. The class should have a constructor with no parameters. If an exception is thrown with this zero-argument constructor, getMessage should return “File Not Found!” The class should also have a constructor with a single parameter of type String. If an exception is thrown with this constructor, then getMessage returns the value that was used as an argument to the constructor.
Answer:
public class FileNotFoundException extends Exception
{
public FileNotFoundException()
{
super(“File Not Found!”);
}

public FileNotFoundException(String message)
{
super(message);
}
}
6) Define an exception class called DiskDriveNotReady. The class should have a constructor with no parameters. If an exception is thrown with this zero-argument constructor, getMessage should return “Disk Drive Not Ready!” The class should also have a constructor with a single parameter of type String. If an exception is thrown with this constructor, then getMessage returns the value that was used as an argument to the constructor.
Answer:
public class DiskDriveNotReadyException extends Exception
{
public DiskDriveNotReadyException()
{
super(“Disk Drive Not Ready!”);
}

public DiskDriveNotReadyException(String message)
{
super(message);
}
}
7) What is the catch or declare rule?
Answer: The catch or declare rule states that checked exceptions that might be thrown when a method is invoked must be accounted for in one of two ways:
1. The possible exception can be caught in a catch block within the method definition.
2. The possible exception can be declared at the start of the method definition by placing the exception class name in the throws clause (and letting whoever uses the method worry about how to handle the exception.
8) Write a complete Java program that prompts the user for two nonnegative integer numbers. Your program should handle bad input data.
Answer:
import java.util.*;
public class GetNumbers2
{
public static void main(String args[]) throws InputMismatchException
{
Scanner stdin = new Scanner(System.in);
System.out.print(“Enter the first non-negative number: “);
int firstNumber = stdin.nextInt();
System.out.print(“Enter the second non-negative number: “);
int secondNumber = stdin.nextInt();
System.out.println(“Your numbers are ” + firstNumber + “and ” +
secondNumber);
}
}

9) Revise the program in number 8 above to use a try/catch block to handle the IOException.
Answer:
import java.util.*;
public class GetNumbers2
{
public static void main(String args[])
{
Scanner stdin = new Scanner(System.in);
try
{
System.out.print(“Enter the first non-negative number: “);
int firstNumber = stdin.nextInt();
System.out.print(“Enter the second non-negative number: “);
int secondNumber = stdin.nextInt();
System.out.println(“Your numbers are ” + firstNumber + “and ” +
secondNumber);
}
catch(InputMismatchException e)
{
System.out.println(“Please enter a positive integer.”);
}
}
}
10) Define an exception class called NegativeNumberException. The class should have a constructor with no parameters. If an exception is thrown with this zero-argument constructor, getMessage should return “Negative Number Not Allowed!” The class should also have a constructor with a single parameter of type String. If an exception is thrown with this constructor, then getMessage returns the value that was used as an argument to the constructor.
Answer:
public class NegativeNumberException extends Exception
{
public NegativeNumberException()
{
super(“Negative Number Detected!”);
}

public NegativeNumberException(String message)
{
super(message);
}
}
11) Revise the program in number 9 above to throw a NegativeNumberException if the user enters a negative number.
Answer:
import java.util.*;
public class GetNumbers2
{
public static void main(String args[]) throws InputMismatchException,
NegativeNumberException
{
Scanner stdin = new Scanner(System.in);
System.out.print(“Enter the first non-negative number: “);
int firstNumber = stdin.nextInt();
if(firstNumber < 0)
throw new NegativeNumberException();

System.out.print(“Enter the second non-negative number: “);
int secondNumber = stdin.nextInt();
if(secondNumber < 0)
throw new NegativeNumberException();
System.out.println(“Your numbers are ” + firstNumber + “and ” +
secondNumber);
}
}
12) If you were going to catch an exception of type Exception, where should this catch block be placed within your code?
Answer: Catching an exception of type Exception is a very general exception, therefore it should be the last catch block in the series of catches, with the more specific exceptions being caught first.
13) What is the purpose of the finally block?
Answer: The finally clause is an excellent place to put program clean-up code, such as closing files.
14) Should an application catch objects of type Error?
Answer: Most programs should not attempt to catch errors, because the program usually can not recover from such errors.
15) List five common examples of exceptions that occur.
Answer: There may be several examples of exceptions provided, some may include: IOException, division by zero, arithmetic overflow, memory exhaustion, invalid method parameters.
16) What is the purpose of the method getMessage() when used with exception handling?
Answer: getMessage will return the string that is passed as an argument to the constructor of an exception handling class.

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!