Java OCA OCP Practice Question 1957

Question

What is the result of executing the following program?

Assume the files referenced in the application both exist and are fully accessible within the file system.

package mypkg; //from  w ww. ja v  a 2s  . co  m
import static java.nio.file.StandardCopyOption.*; 
import static java.nio.file.Files.*; 
import java.io.*; 
import java.nio.file.*; 
public class Main { 
   public void main(String[] items) throws Exception { 
      final Path s = new File("apples.zip").toPath(); 
      final Path t = FileSystems.getDefault().getPath("oranges.zip"); 
      copy(s,t,REPLACE_EXISTING);  // q1 
      copy(Files.newBufferedReader(t),t,ATOMIC_MOVE);  // q2 
   } 
} 
  • A. Line q1 does not compile.
  • B. Line q1 produces an exception at runtime.
  • C. Line q2 does not compile.
  • D. Line q2 produces an exception at runtime.


C.

Note

The first copy() method call on line q1 compiles without issue because it matches the signature of a copy() method in Files.

It also does not throw an exception because the REPLACE_EXISTING option is used and we are told the file is fully accessible within the file system.

The second copy() method on line q2 does not compile.

There is a version of Files.copy() that takes an InputStream, followed by a Path and a list of copy options.

Because BufferedReader does not inherit InputStream, though, there is no matching copy() method and the code does not compile.

Option C is the correct answer.




PreviousNext

Related