Chapter 14: I/O Flashcards
True or False: NIO stands for non-blocking input/output API. If false, why?
False. NIO stands for New Input/Output, not necessarily non-blocking.
True or False: Java 17 SE OCP 1Z0-829 covers both NIO version 1 and version 2. If false, why?
False. The exam only covers NIO version 2.
True or False: Data can be stored in persistent storage devices like hard disk drives and memory cards.
True
True or False: A file within a storage device can contain directories. If false, why?
False. A file holds data, while a directory contains files and other directories.
In Java, directories and files can be treated similarly.
True
The JVM must be manually configured to connect to the file system. If false, why?
False. The JVM automatically connects to the local file system.
True or False: The root directory is always the same across all operating systems. If false, why?
False. Windows uses drive letters (e.g., C:), while Linux uses “/”.
True or False: A path is a representation of a file or directory in the file system.
True
The path separator character is the same for all operating systems. If false, why?
False. It differs; Windows uses “”, while Linux uses “/”.
The system property “file.separator” retrieves the local separator character.
True
True or False: An absolute path includes the full path from the root directory.
True
True or False: A relative path always starts from the root directory. If false, why?
False. A relative path starts from the current working directory.
Whether a path is absolute or relative is independent of the file system. If false, why?
False. It depends on the file system’s conventions
The path /bird/parrot.png is an absolute path in Windows. If false, why?
False. In Windows, absolute paths start with a drive letter, not “/”.
The “.” symbol represents the parent directory. If false, why?
False. “.” refers to the current directory, not the parent.
The “..” symbol refers to the parent directory.
True
A symbolic link is a special file that points to another file or directory.
True
A symbolic link is a copy of the original file. If false, why?
False. A symbolic link is a reference, not a copy.
A File object can represent only a file, not a directory. If false, why?
False. A File object can represent both a file and a directory.
The File class provides only one constructor to create a File object. If false, why?
False. The File class has multiple constructors that accept different parameters, such as a single String, a parent and child String, or a parent File and child String.
If we pass null as the parent in new File(parent, “data/stripes.txt”), it behaves like new File(“data/stripes.txt”). If false, why?
True
The Path interface can be instantiated directly using the new keyword. If false, why?
False. Path is an interface and cannot be instantiated directly; instead, we use factory methods like Path.of() or Paths.get().
The Path.of() method was introduced in Java 8. If false, why?
False. Path.of() was introduced in Java 11.
Both Path.of() and Paths.get() can accept varargs to build a path dynamically. If false, why?
True
All of the following create equivalent Path objects: Path.of(“/home/tiger/data/stripes.txt”) and Paths.get(“/home”, “tiger”, “data”, “stripes.txt”). If false, why?
True
Paths is an interface similar to Path. If false, why?
False. Paths is a utility class with static methods, while Path is an interface.
A URI always begins with “http://” or “https://”. If false, why?
False. A URI can start with other schemas like file://, ftp://, and more.
A URI can be used to reference both local and remote resources. If false, why?
True.
File and Path objects can interact with URIs. If false, why?
True
The file separator used in Path.of() and Paths.get() is always “/”. If false, why?
False. The file separator depends on the operating system, e.g., \ on Windows and / on Linux/macOS.
A File object can be converted to a Path using the toPath() method. If false, why?
True
A Path object can be converted to a File using the toFile() method. If false, why?
True
Paths.get() and Path.of() are both shortcuts for a method in FileSystem. If false, why?
True
FileSystems is an instance of FileSystem. If false, why?
False. FileSystems is a factory class that provides instances of FileSystem.
Files operates on both Path and File instances. If false, why?
False. Files operates only on Path instances, not File instances.
The File class belongs to the NIO.2 API. If false, why?
False. The File class belongs to the traditional I/O (java.io) API.
A Path object can be created using Path.of(URI uri). If false, why?
True
Paths.create() is a valid method to create a Path object. If false, why?
False. There is no Paths.create() method; use Paths.get() or Path.of().
A FileSystem instance can be obtained using FileSystems.getDefault(). If false, why?
True
A Path object can be created from another Path object using Path.of(). If false, why?
True
Classes with plural names, like Files and Paths, are typically factory or utility classes. If false, why?
True
File.toPath() and Path.toFile() can be used interchangeably in all scenarios. If false, why?
False. Conversion is possible but may not always be reversible due to symbolic links, different file system behaviors, or URI-based paths.
What is the purpose of the resolve(Path other) method in Java?
It resolves the given path against the current path, effectively joining them to form a new path.
What happens if the other path provided to resolve() is absolute?
The method simply returns other as it is, without considering the current path.
What is returned if an empty path is passed to resolve(Path other)?
The method returns the original path without any modification
How does resolve(Path other) behave when other is a relative path?
It joins other to the current path, treating the current path as a directory.
What happens if the other path has a root component?
The resolution behavior is implementation-dependent and unspecified.
What is the output of Paths.get(“/home/user”).resolve(“docs/file.txt”)?
/home/user/docs/file.txt (assuming a Unix-based file system).
What is the difference between resolve() and relativize()?
resolve() joins paths, while relativize() calculates the relative path between two paths.
If basePath = Paths.get(“C:\Users\John”), what does basePath.resolve(“Documents\file.txt”) return?
C:\Users\John\Documents\file.txt
How does resolve() behave if the base path does not represent a directory?
The method still performs a logical path joining, even if the base path is a file.
What does resolve() return when used with a path containing
..or
.?
It joins the paths as given; normalization (removing
..and
.) requires calling normalize().
What is the purpose of java.io.RandomAccessFile?
RandomAccessFile allows reading and writing to a file at any position using a file pointer. It supports both sequential and non-sequential access to file contents.
What are the valid modes when constructing a RandomAccessFile?
You can use “r” for read-only or “rw” for read and write access.
Which method moves the RandomAccessFile file pointer to a specific location?
The seek(long position) method moves the file pointer to a given byte offset from the beginning of the file.
Does RandomAccessFile support reading and writing primitive types?
Yes, it provides methods like writeInt(), writeDouble(), readUTF(), and readInt() for handling primitive types and strings.
Is RandomAccessFile part of the java.nio.file package?
No, it is part of java.io, not java.nio.file. While it is still supported, the exam emphasizes java.nio.file for file operations.
What does /./ represent in a file path, and how does normalization handle it?
/./ means “current directory” and has no effect on the path. Normalization removes it.
What does /../ mean in a file path?
/../ means “go up one directory level.” Normalization cancels it with the preceding segment, if possible.
What is the purpose of the Path.normalize() method in Java?
It simplifies a path by removing redundant elements like . and resolving .. segments.
What is the result of normalizing this path?
Paths.get(“a/./b/../c”);
a/c
What does this print?
System.out.println(Paths.get(“/x/./y/../z”).normalize());
/x/z
After normalization, what is the result of this path?
Paths.get(“foo/./bar/../baz/././”).normalize()
foo/baz
What is the primary use of FileReader in Java?
To read character-based data from a file (text files).
What exception must you handle or declare when using FileReader?
IOException
How do you create a FileReader for the file “notes.txt”?
FileReader reader = new FileReader(“notes.txt”);
What is FileWriter used for in Java?
To write character data to a file, typically text.
What happens if the file doesn’t exist when using FileWriter?
Java will create the file automatically.
How do you write to a file without overwriting existing content?
Use the constructor with append = true:
FileWriter writer = new FileWriter(“log.txt”, true);
What is serialization, and why is it important in object-oriented programming?
- Serialization is the process of converting an object into a byte stream or a formatted data structure, such as JSON or XML.
- It’s important for:
Persisting objects (saving them to disk, a database, or network storage).
Sending objects over networks between different JVMs or applications.
Cloning or duplicating objects.
Achieving compatibility across different platforms and programming languages.
What happens to an object’s state during serialization, and how is it maintained after deserialization?
- During serialization:
The object’s state (fields, including superclass fields) is converted into a byte stream.
The object’s class hierarchy and necessary metadata (like field types) are also included in the serialized data. - After deserialization:
The object’s original state is restored, including its fields and class hierarchy.
Transient (non-serializable) fields are not restored, as per their intended design.
How does serialization handle inheritance, and what should you consider when designing serializable classes in an inheritance hierarchy?
Back:
- Serialization handles inheritance by:
- Including the state and metadata of all superclasses in the serialized data.
- Preserving the class hierarchy during deserialization.
- When designing serializable classes in an inheritance hierarchy, consider:
- Using the transient keyword for fields that shouldn’t be serialized.
- Implementing the java.io.Serializable interface in all classes in the hierarchy.
- Ensuring that all necessary fields (including superclass fields) are accessible for serialization.
- Using the serialVersionUID field to maintain compatibility across different releases or versions of a class.
How are objects created during Java deserialization?
Java creates objects without calling constructors of Serializable classes. It uses reflection to initialize fields and invoke methods.
Which constructor is called when deserializing an object in Java?
No-argument constructor of the first non-serializable superclass.