Chapter 14: I/O Flashcards

1
Q

True or False: NIO stands for non-blocking input/output API. If false, why?

A

False. NIO stands for New Input/Output, not necessarily non-blocking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Java 17 SE OCP 1Z0-829 covers both NIO version 1 and version 2. If false, why?

A

False. The exam only covers NIO version 2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False: Data can be stored in persistent storage devices like hard disk drives and memory cards.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

True or False: A file within a storage device can contain directories. If false, why?

A

False. A file holds data, while a directory contains files and other directories.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In Java, directories and files can be treated similarly.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The JVM must be manually configured to connect to the file system. If false, why?

A

False. The JVM automatically connects to the local file system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: The root directory is always the same across all operating systems. If false, why?

A

False. Windows uses drive letters (e.g., C:), while Linux uses “/”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: A path is a representation of a file or directory in the file system.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The path separator character is the same for all operating systems. If false, why?

A

False. It differs; Windows uses “”, while Linux uses “/”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The system property “file.separator” retrieves the local separator character.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False: An absolute path includes the full path from the root directory.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or False: A relative path always starts from the root directory. If false, why?

A

False. A relative path starts from the current working directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Whether a path is absolute or relative is independent of the file system. If false, why?

A

False. It depends on the file system’s conventions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The path /bird/parrot.png is an absolute path in Windows. If false, why?

A

False. In Windows, absolute paths start with a drive letter, not “/”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The “.” symbol represents the parent directory. If false, why?

A

False. “.” refers to the current directory, not the parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The “..” symbol refers to the parent directory.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

A symbolic link is a special file that points to another file or directory.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

A symbolic link is a copy of the original file. If false, why?

A

False. A symbolic link is a reference, not a copy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

A File object can represent only a file, not a directory. If false, why?

A

False. A File object can represent both a file and a directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

The File class provides only one constructor to create a File object. If false, why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

The Path interface can be instantiated directly using the new keyword. If false, why?

A

False. Path is an interface and cannot be instantiated directly; instead, we use factory methods like Path.of() or Paths.get().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

The Path.of() method was introduced in Java 8. If false, why?

A

False. Path.of() was introduced in Java 11.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Both Path.of() and Paths.get() can accept varargs to build a path dynamically. If false, why?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Paths is an interface similar to Path. If false, why?

A

False. Paths is a utility class with static methods, while Path is an interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

A URI always begins with “http://” or “https://”. If false, why?

A

False. A URI can start with other schemas like file://, ftp://, and more.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

A URI can be used to reference both local and remote resources. If false, why?

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

File and Path objects can interact with URIs. If false, why?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

The file separator used in Path.of() and Paths.get() is always “/”. If false, why?

A

False. The file separator depends on the operating system, e.g., \ on Windows and / on Linux/macOS.

31
Q

A File object can be converted to a Path using the toPath() method. If false, why?

32
Q

A Path object can be converted to a File using the toFile() method. If false, why?

33
Q

Paths.get() and Path.of() are both shortcuts for a method in FileSystem. If false, why?

34
Q

FileSystems is an instance of FileSystem. If false, why?

A

False. FileSystems is a factory class that provides instances of FileSystem.

35
Q

Files operates on both Path and File instances. If false, why?

A

False. Files operates only on Path instances, not File instances.

36
Q

The File class belongs to the NIO.2 API. If false, why?

A

False. The File class belongs to the traditional I/O (java.io) API.

37
Q

A Path object can be created using Path.of(URI uri). If false, why?

38
Q

Paths.create() is a valid method to create a Path object. If false, why?

A

False. There is no Paths.create() method; use Paths.get() or Path.of().

39
Q

A FileSystem instance can be obtained using FileSystems.getDefault(). If false, why?

40
Q

A Path object can be created from another Path object using Path.of(). If false, why?

41
Q

Classes with plural names, like Files and Paths, are typically factory or utility classes. If false, why?

42
Q

File.toPath() and Path.toFile() can be used interchangeably in all scenarios. If false, why?

A

False. Conversion is possible but may not always be reversible due to symbolic links, different file system behaviors, or URI-based paths.

43
Q

What is the purpose of the resolve(Path other) method in Java?

A

It resolves the given path against the current path, effectively joining them to form a new path.

44
Q

What happens if the other path provided to resolve() is absolute?

A

The method simply returns other as it is, without considering the current path.

45
Q

What is returned if an empty path is passed to resolve(Path other)?

A

The method returns the original path without any modification

46
Q

How does resolve(Path other) behave when other is a relative path?

A

It joins other to the current path, treating the current path as a directory.

47
Q

What happens if the other path has a root component?

A

The resolution behavior is implementation-dependent and unspecified.

48
Q

What is the output of Paths.get(“/home/user”).resolve(“docs/file.txt”)?

A

/home/user/docs/file.txt (assuming a Unix-based file system).

49
Q

What is the difference between resolve() and relativize()?

A

resolve() joins paths, while relativize() calculates the relative path between two paths.

50
Q

If basePath = Paths.get(“C:\Users\John”), what does basePath.resolve(“Documents\file.txt”) return?

A

C:\Users\John\Documents\file.txt

51
Q

How does resolve() behave if the base path does not represent a directory?

A

The method still performs a logical path joining, even if the base path is a file.

52
Q

What does resolve() return when used with a path containing

..
or
.
?
A

It joins the paths as given; normalization (removing

..
and
.
) requires calling normalize().
53
Q

What is the purpose of java.io.RandomAccessFile?

A

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.

54
Q

What are the valid modes when constructing a RandomAccessFile?

A

You can use “r” for read-only or “rw” for read and write access.

55
Q

Which method moves the RandomAccessFile file pointer to a specific location?

A

The seek(long position) method moves the file pointer to a given byte offset from the beginning of the file.

56
Q

Does RandomAccessFile support reading and writing primitive types?

A

Yes, it provides methods like writeInt(), writeDouble(), readUTF(), and readInt() for handling primitive types and strings.

57
Q

Is RandomAccessFile part of the java.nio.file package?

A

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.

58
Q

What does /./ represent in a file path, and how does normalization handle it?

A

/./ means “current directory” and has no effect on the path. Normalization removes it.

59
Q

What does /../ mean in a file path?

A

/../ means “go up one directory level.” Normalization cancels it with the preceding segment, if possible.

60
Q

What is the purpose of the Path.normalize() method in Java?

A

It simplifies a path by removing redundant elements like . and resolving .. segments.

61
Q

What is the result of normalizing this path?
Paths.get(“a/./b/../c”);

62
Q

What does this print?
System.out.println(Paths.get(“/x/./y/../z”).normalize());

63
Q

After normalization, what is the result of this path?
Paths.get(“foo/./bar/../baz/././”).normalize()

64
Q

What is the primary use of FileReader in Java?

A

To read character-based data from a file (text files).

65
Q

What exception must you handle or declare when using FileReader?

A

IOException

66
Q

How do you create a FileReader for the file “notes.txt”?

A

FileReader reader = new FileReader(“notes.txt”);

67
Q

What is FileWriter used for in Java?

A

To write character data to a file, typically text.

68
Q

What happens if the file doesn’t exist when using FileWriter?

A

Java will create the file automatically.

69
Q

How do you write to a file without overwriting existing content?

A

Use the constructor with append = true:

FileWriter writer = new FileWriter(“log.txt”, true);

70
Q

What is serialization, and why is it important in object-oriented programming?

A
  • 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.
71
Q

What happens to an object’s state during serialization, and how is it maintained after deserialization?

A
  • 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.
72
Q

How does serialization handle inheritance, and what should you consider when designing serializable classes in an inheritance hierarchy?

A

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.
73
Q

How are objects created during Java deserialization?

A

Java creates objects without calling constructors of Serializable classes. It uses reflection to initialize fields and invoke methods.

74
Q

Which constructor is called when deserializing an object in Java?

A

No-argument constructor of the first non-serializable superclass.