What is the result when the result.txt file already exists in c:\student?

Given the code fragment:

public class IsContentSame {

public static boolean isContentSame() throws IOException {

Path p1=Paths.get(“D:\\faculty\\report.txt”);

Path p2=Paths.get(“C:\\student\\report.txt”);

Files.copy(p1,p2,StandardCopyOption.REPLACE_EXISTING,StandardCopyOption.COPY_ATTRI BUTES,LinkOption.NOFOLLOW_LINKS);

if(Files.isSameFile(p1,p2)) {

return true;

} else {

return false;

}

}

public static void main(String[] args) {

try {

boolean flag = isContentSame();

if(flag)

System.out.println(“Equal”);

else

System.out.println(“Not equal”);

} catch (IOException e) {

System.err.println(“Caught IOException: ” + e.getMessage());

}

}

}

What is the result when the result.txt file already exists in c:\student?

Given the code fragment:

public class IsContentSame {

public static boolean isContentSame() throws IOException {

Path p1=Paths.get(“D:\\faculty\\report.txt”);

Path p2=Paths.get(“C:\\student\\report.txt”);

Files.copy(p1,p2,StandardCopyOption.REPLACE_EXISTING,StandardCopyOption.COPY_ATTRI BUTES,LinkOption.NOFOLLOW_LINKS);

if(Files.isSameFile(p1,p2)) {

return true;

} else {

return false;

}

}

public static void main(String[] args) {

try {

boolean flag = isContentSame();

if(flag)

System.out.println(“Equal”);

else

System.out.println(“Not equal”);

} catch (IOException e) {

System.err.println(“Caught IOException: ” + e.getMessage());

}

}

}

What is the result when the result.txt file already exists in c:\student?

A.
The program replaces the file contents and the file’s attributes and prints Equal.

B.
The program replaces the file contents as well as the file attributes and prints Not equal.

C.
An unsupportedoperationException is thrown at runtime.

D.
The program replaces only the file attributes and prints Not equal.

Explanation:
Assuming there is a fileD:\\faculty\\report.txtthen this file will be copied and will be replacing
C:\\student\\report.txt.



Leave a Reply 8

Your email address will not be published. Required fields are marked *


Boris

Boris

The correct answer is B

Francesco

Francesco

The correct answer is B because the isSameFile checks if two paths points to the same file!

Ray

Ray

Yes, answer is B.
If both Path objects are equal then this method returns true without checking if the file exists.

Eloi

Eloi

B is the correct answer

pavan

pavan

If moved then correct ans would be A
but it is copy so

Correct Ans : B

marvin

marvin

B – tested: prints “Not equal”