What will be displayed and what will be printed to a test.out text file as a result of the following code execution?

package question;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class TestOutput {
    public static void main(String[] args) throws IOException {
        PrintStream out = new PrintStream(
                   new BufferedOutputStream(new FileOutputStream("test.out")));
        PrintStream console = System.out;
        System.setOut(console);

        System.out.println("FIRST OUTPUT");

        System.setOut(out);

        System.out.println("SECOND OUTPUT");

        out.close();

        System.out.println("THIRD OUTPUT");
    }
}
Explanation
This question deals with a standard output redirection. With FIRST OUTPUT it is redirected to console. With SECOND OUTPUT it is redirected to file stream. THIRD OUTPUT will be printed out neither in console nor in file since the out stream has been closed in the previous line.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes