click below
click below
Normal Size Small Size show me how
In/Out (Chapter 12)
Question | Answer |
---|---|
How do you create a Scanner object for reading text from a URL? | Create a URL object and use new Scanner (url.openStream()) to create Scanner object for reading data from the URL stream. |
Describe the Java "Throwable" class, its subclasses, and the types of exceptions. | |
What is the advantage of using exception handling? | It enables a method to throw an exception to its caller, so the caller can handle the exception. Without this, the called method does not know how to handle the exception. So it needs to pass the exception to its caller for handling. |
Will this statement "System.out.println(1/0);" throw an exception? | Yes, because an integer can not be divided by 0. |
Will this statement "System.out.println(1.0/0);" throw an exception? | No. Note: a floating-point number (or double) divided by 0 does not raise an exception. |
Point out the problem in the following code: does the code throw any exception? "long value = Long.MAX_VALUE + 1; System.out.println(value);" | Adding 1 to Long.MAX_VALUE exceeds the maximum value allowed by a long value. But the current versions of Java does not report this as an exception. |
What does the JVM do when an exception occurs? How do you catch an exception? | When an exception occurs, Java searches for a handler in the "catch" clause. So to catch an exception in your program, you need to write a "try-catch" statement. |
Describe the Java Throwable class, its subclasses, and the types of exceptions. | The Throwable class is the root of Java exception classes. Error and Exception are subclasses of Throwable. Error describes fatal system errors, and Exception describes the errors that can be handle by Java programs. |
(continue) ... The subclasses of Error are LinkageError, VirtualMachineError, and AWTError. | (continue) ... The subclasses of Exception include RuntimeException, IOException, AWTException, and InstantiationException. |
What is the purpose of declaring exceptions? | The purpose of declaring exceptions is to tell the Java runtime system what can go wrong. |
How do you declare an exception, and where? | You declare an exception using the "throws" keyword in the method declaration. |
Can you declare multiple exceptions in a method header? | You can declare multiple exceptions, separated by commas. |
What is a checked exception? | A checked exception must be explicitly declared in the method declaration, if a method throws it. A checked exception must be caught in a try-catch block. |
What is an unchecked exception? | An unchecked exception does not need to be declared and does not need to be caught. In Java, the only unchecked exceptions are RuntimeException and Error and their subclasses. |
How do you throw an exception? Can you throw multiple exceptions in one throw statement? | You use the throw statement in the method to throw an exception. You cannot throw multiple exceptions in a single throw statement. |
What is the keyword "throw" used for? What is the keyword "throws" used for? | "throw" is for throwing exceptions and "throws" is for claiming exceptions |
What does the method getMessage() do? | the getMessage() is defined in the Throwable class to return a string that describes the exception. |
What does the method printStackTrace() do? | To display trace information to the console. |
Does the presence of a try-catch block impose overhead when no exception occurs? | No |
How do you define a custom exception class? | To define a custom class, extend Exception or a subclass of Exception. |
What is wrong about creating a File object using the following statment? new File(c:\book\test.dat); | The \ is a special character. It should be written as \\ in Java using the Escape sequence. |
How do you check whether a file already exists? | Use "exists()" in the File class to check whether a file exists. |
How do you delete a file? | Use "delete()" in the File class to delete this file. |
How do you rename a file? | Use "renameTo(File)" to rename the name for this file. |
Can you find the file size (number of bytes) using the File class? | Use "length()" to return the file size. |
How do you create a directory? | Use "mkdir" or mkdirs" to create a directory under the File object. |
Can you use the File class for I/O? Does creating a File object create a file on the disk? | No to both. File class can be used to obtain file properties and manipulate files, but cannot perform I/O. Creating a File object does not create a file/directory on the disk. |
How do you create a PrintWriter to write data to a file? | To create a PrintWriter for a file, use new PrintWriter(filename). |
What is the reason to declare throws Exception in the main method in Listing 12.13, WriteData.java? | This statement may throw an exception. Java forces you to write the code to deal with exceptions. One way to deal with it is to declare throws java.io.IOException in the method declaration. |
What would happen if the close() method were not invoked in Listing 12.13? | If the close() method is not invoked, the data may not be saved properly. |
How do you create a Scanner to read data from a file? | To create a Scanner for a file, use new Scanner(new File(filename)). |
What is the reason to define throws Exception in the main method in Listing 12.15, ReadData.java? | This statement may throw an exception. Java forces you to write the code to deal with exceptions. One way to deal with it is to declare throws Exception in the method declaration. |
What would happen if the close() method were not invoked in Listing 12.15? | If the close() method is not invoked, the problem will run fine. But it is a good practice to close the file to release the resource on the file. |
What will happen if you attempt to create a Scanner for a nonexistent file? What will happen if you attempt to create a PrintWriter for an existing file? | If you attempt to create a Scanner for a nonexistent file, an exception will occur. If you attempt to create a PrintWriter for an existing file, the contents of the existing file will be gone. |
Is the line separator the same on all platforms? What is the line sparator on Windows? | No. The line separator on Windows is \r\n. |
Before a URL is added to listOfPendingURLs, line 25 tests whether it has be traversed. Is it possible that listOfPendingURLs contains duplicate URLs? If so, give an example. | Yes. Possible. Suppose link1 is not in listOfPendingURLs, but it appears more than one time in a page. Duplicate link1 will be added into listOfPendingURLs. |