Sail E0 Webinar
Question

import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}


and given that all methods of class FileOutputStream, including close(), throw an



IOException, which of these is true?


Options:
A .  This program will compile successfully.
B .  This program fails to compile due to an error at line 4.
C .  This program fails to compile due to an error at line 6.
D .  This program fails to compile due to an error at line 18.
Answer: Option D

Any method (in this case, the main() method) that throws a checked exception (in this case, 

out.close() ) must be called within a try clause, or the method must declare that it throws the 

exception. Either main() must declare that it throws an exception, or the call to out.close() in 

the finally block must fall inside a (in this case nested) try-catch block.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers