Read and write console and file data using I/O Streams.
Serialize and de-serialize Java objects.
Create, traverse, read, and write Path objects and their properties using java.nio.file API.
1. The correct answer is D.
Explanation:
/home/user
resolve
method appends the given path to the base path. It does not return the base path alone./home/user/documents
resolve
method includes the entire relative path provided as an argument, not just part of it./documents/notes.txt
resolve
method combines the base path with the given relative path; it does not replace the base path with the relative path./home/user/documents/notes.txt
resolve
method appends the relative path to the base path, resulting in /home/user/documents/notes.txt
.2. The correct answer is C.
Explanation:
/home/user/../documents/./notes.txt
normalize
method removes redundant .
and ..
elements, so it wouldn’t leave the path as is./home/user/documents/notes.txt
.
is removed, the ..
navigates one directory up, resulting in an incorrect final path./home/documents/notes.txt
normalize
method processes the path by removing the .
and moving one directory up due to ..
, resulting in /home/documents/notes.txt
./documents/notes.txt
normalize
method does not completely remove the leading part of the path up to documents
. It only processes the .
and ..
elements.3. The correct answer is B.
Explanation:
FileOutputStream
FileOutputStream
is used for writing binary data to a file, not for reading character streams.FileReader
FileReader
is designed for reading character streams from a file, making it the appropriate class for this purpose.BufferedOutputStream
BufferedOutputStream
is used to write binary data to an output stream, buffering the data for efficient writing. It is not used for reading character streams.ObjectInputStream
ObjectInputStream
is used for deserializing objects from an input stream, not for reading character streams.4. The correct answer is C.
Explanation:
Path source = Paths.get("source.txt");
Path target = Paths.get("target.txt");
Files.copy(source, target, StandardCopyOption.ATOMIC_MOVE);
StandardCopyOption.ATOMIC_MOVE
is used for moving files atomically, not for copying. It does not ensure that an existing file is overwritten.Path source = Paths.get("source.txt");
Path target = Paths.get("target.txt");
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
Files.move
is used to move or rename a file, not to copy it. StandardCopyOption.REPLACE_EXISTING
ensures the target file is overwritten during a move, not a copy.Path source = Paths.get("source.txt");
Path target = Paths.get("target.txt");
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
Files.copy
with StandardCopyOption.REPLACE_EXISTING
ensures the target file is overwritten if it exists, which is the correct way to copy a file with overwriting.Path source = Paths.get("source.txt");
Path target = Paths.get("target.txt");
Files.copy(source, target, StandardCopyOption.APPEND);
StandardCopyOption.APPEND
does not exist in the StandardCopyOption
enum, making this code snippet invalid.5. The correct answer is D.
Explanation:
Path path = Paths.get("file.txt");
List<String> lines = Files.readAllBytes(path);
Files.readAllBytes(path)
returns a byte array, not a List<String>
.Path path = Paths.get("file.txt");
List<String> lines = Files.readString(path);
Files.readString(path)
returns a single String
containing the entire content of the file, not a List<String>
.Path path = Paths.get("file.txt");
List<String> lines = Files.lines(path);
Files.lines(path)
returns a Stream<String>
, not a List<String>
. It provides a lazy-loaded stream of lines.Path path = Paths.get("file.txt");
List<String> lines = Files.readAllLines(path);
Files.readAllLines(path)
reads all lines from the file and returns them as a List<String>
, which is the desired behavior.6. The correct answer is A.
Explanation:
Path path = Paths.get("output.txt");
List<String> lines = Arrays.asList("line1", "line2", "line3");
Files.write(path, lines);
Files.write(path, lines)
writes the given list of strings to the file at the specified path, creating the file if it does not exist.Path path = Paths.get("output.txt");
List<String> lines = Arrays.asList("line1", "line2", "line3");
Files.writeString(path, lines);
Files.writeString(path, lines)
does not exist. Files.writeString
expects a single String
as the second argument, not a List<String>
.Path path = Paths.get("output.txt");
List<String> lines = Arrays.asList("line1", "line2", "line3");
Files.writeLines(path, lines);
Files.writeLines(path, lines)
does not exist. There is no such method in the Files
class.Path path = Paths.get("output.txt");
List<String> lines = Arrays.asList("line1", "line2", "line3");
Files.write(path, lines, StandardOpenOption.READ);
StandardOpenOption.READ
is not a valid option for writing files. It is used for reading files.7. The correct answer is B.
Explanation:
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
attrs.lastModifiedTime();
attrs.lastModifiedTime()
retrieves the last modified time of the file, not the creation time.BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
attrs.creationTime();
attrs.creationTime()
retrieves the creation time of the file, which is the correct method from BasicFileAttributes
for this purpose.BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
attrs.lastAccessTime();
attrs.lastAccessTime()
retrieves the last access time of the file, not the creation time.BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
attrs.size();
attrs.size()
retrieves the size of the file, not the creation time.8. The correct answer is D.
Explanation:
Path start = Paths.get("start_directory");
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.SKIP_SUBTREE;
}
});
FileVisitResult.SKIP_SUBTREE
will skip the traversal of the entire subtree, not allowing the complete traversal of the directory tree.Path start = Paths.get("start_directory");
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
throw new IOException("Error visiting file");
}
});
IOException
inside visitFile
will stop the traversal due to an unhandled exception.Path start = Paths.get("start_directory");
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("Visited file: " + file);
return FileVisitResult.TERMINATE;
}
});
FileVisitResult.TERMINATE
will stop the traversal after visiting the first file, not allowing the complete traversal of the directory tree.Path start = Paths.get("start_directory");
Files.walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("Visited file: " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
Files.walkFileTree
with SimpleFileVisitor
, specifying no special FileVisitOption
and setting the maximum depth to Integer.MAX_VALUE
, ensuring full traversal of the directory tree. Additionally, it correctly handles directory pre-visit, file visit, file visit failure, and directory post-visit events.9. The correct answer is B.
Explanation:
A)
class Animal implements Serializable {
private static final long serialVersionUID = 1L;
private String species;
private int age;
public Animal(String species, int age) {
this.species = species;
this.age = age;
}
}
Animal animal = new Animal("Lion", 5);
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("animal.ser"))) {
ois.writeObject(animal);
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream
is used for deserialization (reading objects from a stream), not serialization. It should be ObjectOutputStream
.B)
class Animal implements Serializable {
private static final long serialVersionUID = 1L;
private String species;
private int age;
public Animal(String species, int age) {
this.species = species;
this.age = age;
}
}
Animal animal = new Animal("Lion", 5);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("animal.ser"))) {
oos.writeObject(animal);
} catch (IOException e) {
e.printStackTrace();
}
ObjectOutputStream
is used to serialize an object to a file, which is what this code snippet does correctly.C)
class Animal {
private String species;
private int age;
public Animal(String species, int age) {
this.species = species;
this.age = age;
}
}
Animal animal = new Animal("Lion", 5);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("animal.ser"))) {
oos.writeObject(animal);
} catch (IOException e) {
e.printStackTrace();
}
Animal
class does not implement Serializable
, so it cannot be serialized using ObjectOutputStream
.D)
class Animal implements Serializable {
private static final long serialVersionUID = 1L;
private String species;
private int age;
public Animal(String species, int age) {
this.species = species;
this.age = age;
}
}
Animal animal = new Animal("Lion", 5);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("animal.ser"))) {
bos.write(animal);
} catch (IOException e) {
e.printStackTrace();
}
BufferedOutputStream
cannot be used to write objects directly; ObjectOutputStream
should be used for serialization.Do you like what you read? Would you consider?
Do you have a problem or something to say?