Java OCA OCP Practice Question 1922

Question

What is the output of the following application?

1:  package mypkg; 
2:  import java.nio.file.*; 
3:  public class Main { 
4:     public boolean findHome() { 
5:        Path oftenTraveled = Paths.get("/a1/a2/a3.txt"); 
6:        Path lessTraveled = Paths.get("/a1/a2/a4/../."); 
7:        lessTraveled.resolve("a3.txt"); 
8:        return oftenTraveled.equals(lessTraveled.normalize()); 
9:     } //w  w w  .j  av a 2s .  com
10:    public static void main(String... emerald) { 
11:       System.out.print("AM I HOME? " 
12:             +(new Main().findHome() ? "yes" : " no")); 
13:    } 
14: } 
  • A. AM I HOME? no
  • B. AM I HOME? yes
  • C. The class does not compile.
  • D. The class compiles but throws an exception at runtime.


A.

Note

The program compiles and runs without issue, making Options C and D incorrect.

Like String instances, Path instances are immutable.

The resolve() operation on line 7 has no impact on the lessTraveled variable.

Since one Path ends with /a3.txt and the other does not, they are not equivalent in terms of equals(), making Option A the correct answer.

If lines 6 and 7 were combined, such that the result of the resolve() operation was stored in the lessTraveled variable, then normalize() would reduce lessTraveled to a Path value that is equivalent to oftenTraveled, making Option B the correct answer.




PreviousNext

Related