OCP Flashcards – 8. Java I/O Fundamentals

OCP Exam Objective 8 – Java I/O Fundamentals

Flashcard OCP.8.Java.IO.Fundamentals.001

What can a File object be used for?

Click here to view answer
A File object can be used to create a new file or directory, delete it, or inquire about or modify its attributes.

 

Flashcard OCP.8.Java.IO.Fundamentals.002

True or false: a File instance must be associated with an existing file or directory, otherwise a RuntimeException gets thrown?

Click here to view answer
False

 

Flashcard OCP.8.Java.IO.Fundamentals.003

What does the File’s method list() return?

Click here to view answer
An array of subdirectories and files.

 

Flashcard OCP.8.Java.IO.Fundamentals.004

What is the type of the abovementioned array?

Click here to view answer
String

 

Flashcard OCP.8.Java.IO.Fundamentals.005

What is the abstract base class for all input streams?

Click here to view answer
java.io.InputStream

 

Flashcard OCP.8.Java.IO.Fundamentals.006

What does the InputStream‘s read() method return?

Click here to view answer
It returns either the next byte of data or -1 when the end of the file has been reached.

 

Flashcard OCP.8.Java.IO.Fundamentals.007

What method is used to release system resources associated with the InputStream or OutputStream object?

Click here to view answer
close()

 

Flashcard OCP.8.Java.IO.Fundamentals.008

What classes would you use to read and write raw bytes from and to a file?

Click here to view answer
FileInputStream and FileOutputStream, respectively.

 

Flashcard OCP.8.Java.IO.Fundamentals.009

Which classes are used to read/write primitive values and objects from/to files?

Click here to view answer
An ObjectOutputStream can write primitive values and objects to an OutputStream, which can be read by an ObjectInputStream.

 

Flashcard OCP.8.Java.IO.Fundamentals.010

What are the abstract base classes for reading and writing Unicode characters?

Click here to view answer
Reader and Writer.

 

Flashcard OCP.8.Java.IO.Fundamentals.011

What are convenience classes for reading and writing character data from/to files?

Click here to view answer
FileReader and FileWriter.

 

Flashcard OCP.8.Java.IO.Fundamentals.012

How to increase efficiency or reading and writing data to/from files?

Click here to view answer
By using buffer arrays.

 

Flashcard OCP.8.Java.IO.Fundamentals.013

What are the classes that allow to use buffered character streams?

Click here to view answer
BufferedReader and BufferedWriter. (EXTRA: the default buffer size is 8192 characters)

 

Flashcard OCP.8.Java.IO.Fundamentals.014

Which method of the BufferedReader class allows to ignore and discard a specified number of characters?

Click here to view answer
skip()

 

Flashcard OCP.8.Java.IO.Fundamentals.015

Which marker interface a class must implement so that an object of this particular datatype can be written to and read from a stream?

Click here to view answer
Serializable

 

Flashcard OCP.8.Java.IO.Fundamentals.016

What is the difference between static and instance fields in respect to serialization?

Click here to view answer
Static fields are not serialized.

 

Flashcard OCP.8.Java.IO.Fundamentals.017

Which keyword indicates an instance field should be omitted during serialization?

Click here to view answer
transient

 

Flashcard OCP.8.Java.IO.Fundamentals.018

What are the three built-in (“standard”) streams that are already open?

Click here to view answer
System.in, System.out, and System.err

 

Flashcard OCP.8.Java.IO.Fundamentals.019

Which System method retrieves a Console instance?

Click here to view answer
console()

 

Flashcard OCP.8.Java.IO.Fundamentals.020

Which Console method disables echoing to provide a secure login from the console?

Click here to view answer
readPassword()

 

Flashcard OCP.8.Java.IO.Fundamentals.021

Why does readPassword() return a char[] rather than String? Select one:

  • because all String objects are placed into a shared pool;
  • because String objects are immutable;
  • because char[] is more memory efficient than String;
  • because arrays cannot be serialized to disk.
Click here to view answer
It’s about the immutability: an attempt to overwrite a String simply produces a new String, while the old one will be just sitting on the heap waiting to be GCd.

Reminder: Only String literals get interned into a pool automatically and stay there for as long as the current JVM instance exits; any String that has been created at runtime, gets GCd after becoming unreachable but it might take some time during which the memory could be dumped on disk.

As for serialization, the JLS, §10.8 Class Objects for Arrays, says that ‘…every array type implements the interfaces Cloneable and java.io.Serializable‘.

Leave Comment

Your email address will not be published.