Multiple choice technology

What is the result of compiling and running the following code, assuming that the file bb.txt does not exist? class WriterTest{ public static void main(String[] args) throws IOException{ Writer w=new BufferedWriter(new FileWriter(“bb.txt”)); // Line1 w.write(1); // Line2 w.close(); } }

  1. Compiler error

  2. FileNotFoundException thrown at line 1

  3. None of these

  4. .IOException thrown at line 2

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

FileWriter creates the file 'bb.txt' if it doesn't exist, so no FileNotFoundException occurs at line 1. However, BufferedWriter.write(int) writes a single character (the low 16 bits of the integer), not the string '1'. At line 2, w.write(1) writes a character with codepoint 1 (not printable), which is valid. The program compiles and runs without error; no exception is thrown. The correct answer is 'None of these' because the program completes successfully.