Multiple choice technology programming languages

What is the output for the following lines of code? import java.io.*; public class FileHandling { public static void main (String[] arg) throws Exception { File f = new File ("ll.txt"); FileOutputStream s = new FileOutputStream(f); PrintStream p = new PrintStream (s); p.println("hi"); } }

  1. A. It would fail to compile

  2. B. It would generate a run-time error on execution.

  3. C. It would execute, but ask the user to specify the directory in which to create ll.txt.

  4. D. It would execute without an error and print "hi" to a file called ll.txt

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

FileOutputStream creates the file if it doesn't exist and opens it for writing. PrintStream wraps this output stream and provides convenient methods like println. The code successfully creates ll.txt and writes hi to it. No exceptions occur because the throws Exception clause handles any IOException.