Questions and Answers
Which classes will allow the following to compile? (Choose all that apply) InputStream is = new BufferedInputStream(new FileInputStream("zoo.txt")); InputStream wrapper = new ________________ (is);
Which of the following statements about Java serialization are correct? (Choose all that apply)
What is the result of executing the following code? (Choose all) String line; Console c = System.console(); Writer w = c.writer(); if ((line = c.readLine()) != null) w.append(line); w.flush();
Which of the following are true statements about serialization in Java? (Choose all that apply)
Signup and view all the answers
Choose all that apply. 1: public static void deleteTree(File file) { 2: if ( !file.isFile() ) 3: for( File entry: file.listFiles() ) 4: deleteTree(entry); 5: else file.delete(); 6: }
Signup and view all the answers
Which of the following are methods available to instances of the java.io.File class? (Choose all)
Signup and view all the answers
Suppose that the file c:\book\java exists. Which of the following lines of code creates an object that represents the file? (Choose all that apply)
Signup and view all the answers
Why shouldn't every class be marked Serializable? (Choose all)
Signup and view all the answers
Study Notes
I/O and File Concepts in Java
- BufferedInputStream can be wrapped multiple times as it supports high-level streams wrapping other high-level streams.
- When using InputStream in combination with BufferedInputStream and FileInputStream, subclasses like ObjectInputStream and BufferedWriter can also act as wrappers.
Java Serialization
- For a class to be serializable, it and all its superclasses must implement the Serializable interface.
- Upon deserialization, the no-argument constructor of the class and any non-Serializable superclasses is called.
- Multiple instances of the same object can exist in a stream during serialization.
Handling Console Input/Output
- Reading from the console and outputting to it can be done using Console methods; if no console is available, a NullPointerException occurs.
- The append() method may throw an IOException when handling console input.
Serialization Process in Java
- Deserialization transforms serialized data back into Java memory structures.
- Non-thread classes should not be marked Serializable unnecessarily.
- The Serializable interface does not require explicit serialize() and deserialize() methods; it is final and cannot be extended.
- ClassNotFoundException may be thrown by the readObject() method of ObjectInputStream even when the object is not explicitly cast.
File Deletion Operations
- The deleteTree() method can recursively delete directories and files but may throw exceptions at runtime due to file system issues.
- The method can delete files and an entire directory structure but requires handling potential exceptions.
Methods in java.io.File Class
- To rename or "move" files, use the renameTo() method; there are no direct move() methods.
- To create directories, use mkdir() for a single directory and mkdirs() for multiple directories.
- There is no copy() method in the java.io.File class; copying files requires reading their content via streams.
File Path Representation
- In file paths, backslashes must be escaped with another backslash in Java.
- Both forward slashes and properly escaped backslashes can be used to represent file paths correctly.
Considerations for Serializable Classes
- Not all classes should be marked Serializable to avoid unnecessary overhead and potential exceptions.
- Only certain classes should be marked Serializable based on context and specific design decisions.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge on Java I/O and file handling with these flashcards specifically designed for the OCP certification exam. This quiz focuses on the various classes that interact with BufferedInputStream. Select all applicable classes that will allow the provided code snippet to compile correctly.