Java OCA OCP Practice Question 2866

Question

What is the result of executing the following code? (Choose all that apply.)

import java.io.Console;
import java.io.Writer;

public class Main {

   public static void main(String[] args) throws Exception {
      String line;/*from   w w  w .j a  v  a 2s  .  c  o  m*/
      Console c = System.console();
      Writer w = c.writer();
      if ((line = c.readLine()) != null)
         w.append(line);
      w.flush();

   }
}
  • A. The code runs without error but prints nothing.
  • B. The code prints what was entered by the user.
  • C. An ArrayIndexOutOfBoundsException might be thrown.
  • D. A NullPointerException might be thrown.
  • E. An IOException might be thrown.
  • F. The code does not compile.


B, D, E.

Note

This is correct code for reading a line from the console and writing it back out to the console, making option B correct.

Options D and E are also correct.

If no console is available, a NullPointerException is thrown.

The append() method throws an IOException.




PreviousNext

Related