CIS-315 Chapter 12

25 July 2022
4.7 (114 reviews)
58 test answers

Unlock all answers in this set

Unlock answers (54)
question
Which method can be used to create an output object for file temp.txt?
answer
new PrintWriter(new File("temp.txt")) new PrintWriter("temp.txt")
question
A method can throw a subclass of RuntimeException.
answer
true
question
Any number divided by 0 would cause a compilation error.
answer
false it's a runtime error
question
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); } } }
answer
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.
question
What are the reasons to create an instance of the File class?
answer
to rename the file to delete the file to obtain the properties of the file such as whether the file can be read, written, or is hidden to determine whether the file exists
question
________ is the root class of exception classes.
answer
Throwable
question
Closing a PrintWriter object ensures that the data in the buffer are sent to the file.
answer
true
question
You must place a method call in a try-catch block if the method claims an exception.
answer
true
question
Which of the following statements are true? (File creating .txt objects)
answer
None of the above
question
________ are unchecked exceptions.
answer
RuntimeException
question
What exception type does the following program throw? public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } }
answer
ArrayIndexOutOfBoundsException
question
Which of the following statements are correct? I: try (PrintWriter output = new PrintWriter("output.txt")) { output.println("Welcome to Java"); } II: try (PrintWriter output = new PrintWriter("output.txt");) { output.println("Welcome to Java"); } III: PrintWriter output; try (output = new PrintWriter("output.txt");) { output.println("Welcome to Java"); } IV: try (PrintWriter output = new PrintWriter("output.txt");) { output.println("Welcome to Java"); } finally { output.close(); }
answer
I II III IV would not be right because of the closure of output outside of the block
question
What is displayed on the console when running the following program? 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"); } finally { System.out.println("End of the block"); } System.out.println("End of the block"); } }
answer
The program displays Welcome to Java and End of the block, and then terminates because of an unhandled exception.
question
What exception type does the following program throw? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } }
answer
NullPointerException
question
Which of the following classes are in the java.lang package?
answer
RuntimeException Exception Throwable
question
________ are checked exceptions.
answer
Exception Throwable IOException
question
Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code. Scanner input = new Scanner(System.in); int v1 = input.nextInt(); int v2 = input.nextInt(); String line = input.nextLine();
answer
The program has a runtime error because 34.3 is not an integer.
question
If an exception occurs in a try-catch block, the code in the finally clause ________.
answer
is executed
question
An instance of ________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.
answer
Exception
question
What is displayed on the console when running the following program? class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (NumberFormatException ex) { System.out.println("NumberFormatException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } } static void method() { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } }
answer
The program displays NumberFormatException
question
What is displayed on the console when running the following program? 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"); } } }
answer
The program displays Welcome to Java two times followed by End of the block
question
Placing an exception class after its superclass in the catch block results in a compilation error.
answer
true
question
Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code. Scanner input = new Scanner(System.in); double v1 = input.nextDouble(); double v2 = input.nextDouble(); String line = input.nextLine();
answer
After the last statement is executed, line contains characters ' ', '7', '8', '9'.
question
You cannot place a method call in a try-catch block unless the method claims an exception.
answer
false
question
Which of the following returns the path separator character?
answer
File.pathSeparatorChar
question
To create an InputStream to read from a file on a Web server, you use the method ________ in the URL class.
answer
openStream();
question
The presence of the try-catch block imposes overhead when no exception occurs.
answer
false
question
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"); } } }
answer
The program has a compilation error
question
A method must declare to throw ________.
answer
checked exceptions
question
Which class contains the method for checking whether a file exists?
answer
File
question
An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.
answer
RuntimeException
question
Analyze the following code: public static boolean isCorrect() { try { // Perform some tasks // ... return true; } finally { return true; } }
answer
When invoking the isCorrect method, it always returns false.
question
When an Error-type exception occurs, the GUI application may continue to run.
answer
false
question
What is displayed on the console when running the following program? class Test { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; double y = 2.0 / i; System.out.println("Welcome to HTML"); } finally { System.out.println("The finally clause is executed"); } } }
answer
The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
question
Which of the following statements are true? (exception throwing)
answer
To throw an exception, use the key word throw. You use the keyword throws to declare exceptions in the method heading. A method may declare to throw multiple exceptions. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.
question
What is displayed on the console when running the following program? class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } finally { System.out.println("The finally clause is executed"); } } }
answer
Welcome to Java followed by The finally clause is executed in the next line
question
Which method can be used to create an input object for file temp.txt?
answer
new Scanner(new File("temp.txt"))
question
What is wrong in the following program? class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } }
answer
You cannot have a try block without a catch block or a finally block
question
What is displayed on the console when running the following program? 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"); } } }
answer
The program displays RuntimeException followed by After the method call.
question
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; } }
answer
ClassCastException
question
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)); } }
answer
StringIndexOutOfBoundsException
question
What is displayed on the console when running the following program? 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"); } } }
answer
The program displays RuntimeException followed by After the method call
question
Which method can be used to write data?
answer
print
question
Analyze the following program. 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; System.out.println("Welcome to Java"); } catch (Exception ex) { System.out.println(ex); } } }
answer
An exception is raised due to Integer.parseInt(s);
question
What is displayed on the console when running the following program? 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"); } } }
answer
Welcome to Java followed by The finally clause is executed in the next line
question
An exception is always an instance of ________.
answer
Throwable
question
Which class do you use to read data from a text file?
answer
Scanner
question
Which method can be used to read a whole line from the file?
answer
nextLine
question
The following code causes Java to throw ________. int number = Integer.MAX_VALUE + 1;
answer
no exceptions
question
A method can throw a subclass of RuntimeException.
answer
true
question
Which of the following statements creates an instance of File on Window for the file c:temp.txt?
answer
new File("c:temp.txt")
question
________ are checked exceptions.
answer
Exception Throwable IOException
question
The methods getMessage(), printStackTrace(), and toString() are available in ________ class.
answer
RuntimeException Exception Throwable IOException
question
What is displayed on the console when running the following program? 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"); } } }
answer
The program displays NumberFormatException followed by RuntimeException.
question
An instance of ________ are unchecked exceptions.
answer
Runtime Exception Error NumberFormatException
question
Analyze the following code: class Test { public static void main(String[] args) throws MyException { System.out.println("Welcome to Java"); } } class MyException extends Error { }
answer
You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.
question
A method should not claim Error or RuntimeException in the method declaration, but it may throw exceptions of these types.
answer
true
question
public class Test { public static void main(String[] args) { Object o = null; System.out.println(o); } }
answer
No exception