1.Which of the following correctly create Path instances?
Each correct answer represents a complete solution. Choose all that apply.
A) FileSystems.getDefault().getPath(“puma.txt”)
new java.io.File(“tiger.txt”).toPath()
Path.of(Path.of(“.”).toUri())
2.Suppose that the working directory is /weather and the absolute path
/weather/winter/snow.dat represents a file that exists within the file
system. Which of the following lines of code create an object that
represents the file?
Each correct answer represents a complete solution. Choose all that apply.
A) new File(“/weather/winter/snow.dat”)
new File(new File(“/weather/winter”), “snow.dat”)
Path.of(“/weather/winer/snow.dat”).toFile();
3.For which values of path sent to this method would it be possible for
the following code to output Success?
public void removeBadFile(Path path) {
if(Files.isDirectory(path))
System.out.println(Files.deleteIfExists(path)
? “Success”: “Try Again”);
}
A) The code does not compile.
4.Assume that /kang exists as a symbolic link to the directory
/mammal/kangaroo within the file system. Which of the following
statements are correct about this code snippet?
var path = Paths.get(“/kang”);
if(Files.isDirectory(path) && Files.isSymbolicLink(path))
Files.createDirectory(path.resolve(“joey”));
Each correct answer represents a complete solution. Choose all that apply.
A) A new directory may be created.
If the code creates a directory, it will be reachable at /kang/joey.
5.What is the output of the following code?
var p1 = Path.of(“/zoo/./bear”,”../food.txt”);
p1.normalize().relativize(Path.of(“/lion”));
System.out.println(p1);
var p2 = Paths.get(“/zoo/animals/bear/koala/food.txt”);
System.out.println(p2.subpath(1,3).getName(1));
var p3 = Path.of(“/pets/../cat.txt”);
var p4 = Paths.get(“./dog.txt”);
System.out.println(p4.resolve(p3));
Each correct answer represents a part of the solution. Choose all that apply.
A) /zoo/./bear/../food.txt
Bear
/pets/../cat.txt
6.Assuming that the directories and files referenced exist and are not
symbolic links, what is the result of executing the following code
snippet?
var p1 = Path.of(“/lizard”,”.”).resolve(Path.of(“walking.txt”));
var p2 = new File(“/lizard/././actions/../walking.txt”).toPath();
System.out.print(Files.isSameFile(p1,p2));
System.out.print(” “);
System.out.print(p1.equals(p2));
System.out.print(” “);
System.out.print(Files.mismatch(p1,p2));
A) true false -1
7.Assume that monkey.txt is a file that exists in the current working
directory. Which statement about the following code snippet is correct?
Files.move(Path.of(“monkey.txt”), Paths.get(“/animals”),
StandardCopyOption.ATOMIC_MOVE,
LinkOption.NOFOLLOW_LINKS);
A) If the move is successful and another process is monitoring the
file system, it will not see an incomplete file at runtime
8.What are some possible results of executing the following code?
var x = Path.of(“/animals/fluffy/..”);
Files.walk(x.toRealPath().getParent()) // u1
.map(p -> p.toAbsolutePath().toString()) // u2
.filter(s -> s.endsWith(“.java”))
.forEach(System.out::println);
Each correct answer represents a complete solution. Choose all that app
A) It prints some files in the root directory.
Another exception is thrown at runtime
9.Assume that the source instance passed to the following method
represents a file that exists. Also assume that /flip/sounds.txt exists
as a file prior to executing this method. When this method is executed,
which statement correctly copies the file to the path specified by
/flip/sounds.txt?
void copyIntoFlipDirectory(Path source) throws IOException {
var dolphinDir = Path.of(“/flip”);
dolphinDir = Files.createDirectories(dolphinDir);
var n = Paths.get(“sounds.txt”);
____________;
}
A)
Files.copy(source,dolphinDir.resolve(n),StandardCopyOption.REPLACE_EXISTI
NG)
10.Which class would be best to use to read a binary file into a Java
object?
A) ObjectInputStream
11.Assuming that / is the root directory within the file system, which
of the following are true statements?
Each correct answer represents a complete solution. Choose all that apply
A) /home/parrot is an absolute path.
A Reader offers character encoding, making it more useful when
working with String data than an InputStream.
12.Which classes will allow the following code snippet to compile?
var is = new BufferedInputStream(new FileInputStream(“z.txt”));
InputStream wrapper = new __ (is);
try (wrapper) {}
Each correct answer represents a complete solution. Choose all that apply.
A) BufferedInputStream
ObjectInputStream
13.Suppose that you need to read text data from a file and want the
data to be performant on large files. Which of the following java.io
stream classes can be chained together to best achieve this result?
Each correct answer represents a part of the solution. Choose all that apply.
A) BufferedReader
FileReader
14.Assuming that the /fox/food-schedule.csv file exists with the
specified contents, what is the expected output of calling printData()
on it?
/fox/food-schedule.csv
6am,Breakfast
9am,SecondBreakfast
12pm,Lunch
6pm,Dinner
void printData(Path path) throws IOException {
Files.readAllLines(path) // r1
.flatMap(p -> Stream.of(p.split(“,”))) // r2
.map(q -> q.toUpperCase()) // r3
.forEach(System.out::println);
}
A) The code will not compile because of line r2.
15.Assuming zoo-data.txt exists and is not empty, what statements
about the following method are correct?
private void echo() throws IOException {
var o = new FileWriter(“new-zoo.txt”);
try (var f = new FileReader(“zoo-data.txt”);
var b = new BufferedReader(f); o) {
o.write(b.readLine());
}
o.write(“”);
}
Each correct answer represents a complete solution. Choose all that apply.
A) When run, the method creates a new file with one line of text in it.
The method compiles but will produce an exception at runtime.
16.What would be the value of name if the instance of Eagle created in the
main() method were serialized and then deserialized?
import java.io.Serializable;
class Bird {
protected transient String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
public Bird() {
this.name = “Matt”;
}
}
public class Eagle extends Bird implements Serializable {
{ this.name = “Olivia”; }
public Eagle() {
this.name = “Bridget”;
}
public static void main(String[] args) {
var e = new Eagle();
e.name = “Adeline”;
}
}
A) Matt
17.Which of the following are true statements about serialization in
Java?
Each correct answer represents a complete solution. Choose all that apply.
A) I.All non-null instance members of the class must be serializable or
marked transient.
II.The class must implement the Serializable interface.
18.Which of the following fields will be null after an instance of the class
created on line 17 is serialized and then deserialized using
ObjectOutputStream and ObjectInputStream?
import java.io.Serializable;
import java.util.List;
public class Zebra implements Serializable {
private transient String name = “George”;
private static String birthPlace = “Africa”;
private transient Integer age;
List friends = new java.util.ArrayList<>();
private Object stripes = new Object();
{ age = 10;}
public Zebra() {
this.name = “Sophia”;
}
static Zebra writeAndRead(Zebra z) {
// Implementation omitted
}
public static void main(String[] args) {
var zebra = new Zebra();
zebra = writeAndRead(zebra);
}
A) The code compiles but throws an exception at runtime.
19.What are possible results of executing the following code?
public static void main(String[] args) throws IOException {
String line;
var c = System.console();
Writer w = c.writer();
try (w) {
if ((line = c.readLine(“Enter your name: “)) != null)
w.append(line);
w.flush();
}
}
Each correct answer represents a complete solution. Choose all that apply.
A) The code prints what was entered by the use
A NullPointerException may be thrown.
20.Given the following method, which statements are correct?
public void copyFile(File file1, File file2) throws Exception {
var reader = new InputStreamReader(new FileInputStream(file1));
try (var writer = new FileWriter(file2)) {
char[] buffer = new char[10];
while(reader.read(buffer) != -1) {
writer.write(buffer);
// n1
}
}
}
Each correct answer represents a complete solution. Choose all that apply.
A) I.The code compiles and correctly copies the data between some files.
II.If we check file2 on line n1 within the file system after five iterations of
the while loop, it may be empty.
III.This method contains a resource leak.
21.What is the result of executing the following code?
var p = Paths.get(“sloth.schedule”);
var a = Files.readAttributes(p, BasicFileAttributes.class);
Files.mkdir(p.resolve(“.backup”));
if(a.size()>0 && a.isDirectory()) {
a.setTimes(null,null,null);
}
Each correct answer represents a complete solution. Choose all that apply.
A) The code will not compile because of line 6.
The code will not compile because of line 8.
22.Which of the following statements are true?
Each correct answer represents a complete solution. Choose all that apply.
A) I.NIO.2 includes a method to traverse a directory tree.
II.NIO.2 includes methods that are aware of symbolic links.
III.Files.readAttributes() is often more performant since it reads multiple
attributes rather than accessing individual attributes.
23.Assume that reader is a valid stream whose next characters are
PEACOCKS. What is true about the output of the following code
snippet?
var sb = new StringBuilder();
sb.append((char)reader.read());
reader.mark(10);
for(int i=0; i<2; i++) {
sb.append((char)reader.read());
reader.skip(2);
}
reader.reset();
reader.skip(0);
sb.append((char)reader.read());
System.out.println(sb.toString());
A) The code may print PEOE.
24.Assume that /monkeys exists as a directory containing multiple files,
symbolic links, and subdirectories. Which statement about the following
code is correct?
var f = Path.of(“/monkeys”);
try (var m =
Files.find(f, 0, (p,a) -> a.isSymbolicLink())) { // y1
m.map(s -> s.toString())
.collect(Collectors.toList())
.stream()
.filter(s -> s.toString().endsWith(“.txt”)) // y2
.forEach(System.out::println);
}
A) It will print nothing.
Other Links:
Statistics Quiz
Networking Quiz
See other websites for quiz:
Check on QUIZLET
Check on CHEGG