CIS-315 Chapter 12

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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"); } } }

The program has a compilation error

What is wrong in the following program? class Test { public static void main (String[] args) { try { System.out.println("Welcome to Java"); } } }

You cannot have a try block without a catch block or a finally block

A method must declare to throw ________.

checked exceptions

The presence of the try-catch block imposes overhead when no exception occurs.

false

When an Error-type exception occurs, the GUI application may continue to run.

false

Which of the following statements creates an instance of File on Window for the file c:\temp.txt?

new File("c:\\temp.txt")

What are the reasons to create an instance of the File class?

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

A method can throw a subclass of RuntimeException.

true

A method should not claim Error or RuntimeException in the method declaration, but it may throw exceptions of these types.

true

Analyze the following code: public static boolean isCorrect() { try { // Perform some tasks // ... return true; } finally { return true; } }

When invoking the isCorrect method, it always returns false.

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); } } }

An exception is raised due to Integer.parseInt(s);

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]); } }

ArrayIndexOutOfBoundsException

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)); } }

StringIndexOutOfBoundsException

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"); } }

The program displays NumberFormatException

An exception is always an instance of ________.

Throwable

________ is the root class of exception classes.

Throwable

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); } } }

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.

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();

After the last statement is executed, line contains characters ' ', '7', '8', '9'.

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; } }

ClassCastException

An instance of ________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

Exception

________ are checked exceptions.

Exception Throwable IOException

________ are checked exceptions.

Exception Throwable IOException

Which class contains the method for checking whether a file exists?

File

Which of the following returns the path separator character?

File.pathSeparatorChar

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(); }

I II III IV would not be right because of the closure of output outside of the block

public class Test { public static void main(String[] args) { Object o = null; System.out.println(o); } }

No exception

Which of the following statements are true? (File creating .txt objects)

None of the above

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()); } }

NullPointerException

An instance of ________ are unchecked exceptions.

Runtime Exception Error NumberFormatException

An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

RuntimeException

________ are unchecked exceptions.

RuntimeException

The methods getMessage(), printStackTrace(), and toString() are available in ________ class.

RuntimeException Exception Throwable IOException

Which of the following classes are in the java.lang package?

RuntimeException Exception Throwable

Which class do you use to read data from a text file?

Scanner

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"); } } }

The program displays NumberFormatException followed by RuntimeException.

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"); } } }

The program displays RuntimeException followed by After the method call

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"); } } }

The program displays RuntimeException followed by After the method call.

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"); } }

The program displays Welcome to Java and End of the block, and then terminates because of an unhandled exception.

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"); } } }

The program displays Welcome to Java two times followed by End of the block

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"); } } }

The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.

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();

The program has a runtime error because 34.3 is not an integer.

Which of the following statements are true? (exception throwing)

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.

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"); } } }

Welcome to Java followed by The finally clause is executed in the next line

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"); } } }

Welcome to Java followed by The finally clause is executed in the next line

Analyze the following code: class Test { public static void main(String[] args) throws MyException { System.out.println("Welcome to Java"); } } class MyException extends Error { }

You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.

You cannot place a method call in a try-catch block unless the method claims an exception.

false

Any number divided by 0 would cause a compilation error.

false it's a runtime error

If an exception occurs in a try-catch block, the code in the finally clause ________.

is executed

Which method can be used to create an output object for file temp.txt?

new PrintWriter(new File("temp.txt")) new PrintWriter("temp.txt")

Which method can be used to create an input object for file temp.txt?

new Scanner(new File("temp.txt"))

Which method can be used to read a whole line from the file?

nextLine

The following code causes Java to throw ________. int number = Integer.MAX_VALUE + 1;

no exceptions

To create an InputStream to read from a file on a Web server, you use the method ________ in the URL class.

openStream();

Which method can be used to write data?

print

Closing a PrintWriter object ensures that the data in the buffer are sent to the file.

true

Placing an exception class after its superclass in the catch block results in a compilation error.

true

You must place a method call in a try-catch block if the method claims an exception.

true


संबंधित स्टडी सेट्स

Internal Combustion Engine Theory & Performance

View Set

Human Communication Test 3 (7, 8, 9)

View Set

Перевірка знання Закону України "Про освіту"

View Set

2: Race and Ethnicity- Race/Ethnicity/ and Discrimination

View Set

Ch 9. Prenatal Influences on Development

View Set