Java OCA OCP Practice Question 2255

Question

Consider the following code segment:

OutputStream os = new FileOutputStream("log.txt");
System.setErr(new PrintStream(os)); // SET SYSTEM.ERR
System.err.println("Error");

Which one of the following statements is true regarding this code segment?.

  • A. the line with comment set System.err will not compile and will result in a compiler error.
  • B. the line with comment set System.err will result in throwing a runtime exception since System.err cannot be programmatically redirected.
  • C. the program will print the text "error" in console since System.err by default sends the output to console.
  • d. this code segment redirects the System.err to the log.txt file and will write the text "error" to that file.


d.

Note

note that you can redirect the System.err programmatically using the setErr() method.

System.err is of type PrintStream, and the System.setErr() method takes a PrintStream as an argument.

Once the error stream is set, all writes to System.err will be redirected to it.

hence, this program will create log.txt with the text "error" in it.




PreviousNext

Related