Java OCP Part 8 I/O and File Flashcards
8 Questions
100 Views

Java OCP Part 8 I/O and File Flashcards

Created by
@BenevolentDramaticIrony

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

  • BufferedReader
  • ObjectOutputStream
  • ObjectInputStream (correct)
  • BufferedInputStream (correct)
  • FileInputStream
  • BufferedWriter
  • Which of the following statements about Java serialization are correct? (Choose all that apply)

  • For a class to be serializable, that class and each of its super classes must implement the Serializable interface. (correct)
  • Multiple copies of an object may be added to a stream. (correct)
  • When deserializing an object, the no-argument constructor of each of its super classes that does not implement the Serializable interface is invoked.
  • When deserializing an object, the no-argument constructor of the class and each of its super classes is invoked.
  • 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();

  • The code runs without error but prints nothing.
  • An ArrayIndexOutOfBoundsException might be thrown.
  • A NullPointerException might be thrown. (correct)
  • An IOException might be thrown. (correct)
  • The code prints what was entered by the user. (correct)
  • The code does not compile.
  • Which of the following are true statements about serialization in Java? (Choose all that apply)

    <p>The readObject() method of ObjectInputStream may throw a ClassNotFoundException even if the return object is not explicitly cast.</p> 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: }

    <p>It can delete a single file.</p> Signup and view all the answers

    Which of the following are methods available to instances of the java.io.File class? (Choose all)

    <p>renameTo()</p> 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)

    <p>new File(&quot;c:/book/java&quot;);</p> Signup and view all the answers

    Why shouldn't every class be marked Serializable? (Choose all)

    <p>Classes can implement only one interface.</p> 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.

    Quiz Team

    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.

    Use Quizgecko on...
    Browser
    Browser