Given the code fragment:
What is the result, if the file salesreport.dat does not exist?
A.
Compilation fails only at line 6
B.
Compilation fails only at line 13
C.
Compilation fails at line 6 and 13
D.
Class java.io.IOException
E.
Class java.io.FileNotFoundException
Explanation:
Compilation works fine.
There will be a runtime error at line 6 (as the file salesreport.dat) does not exist.
This will be caught as an IOException at line 17.
It’s like that:
public static void main(String[] args) {
try(BufferedReader br = new BufferedReader(null)){
br = new BufferedReader(null);
} catch (IOException e) {
e.printStackTrace();
}
}
And the result is: B)
because all resources in the try() are automatically final. So another assignment failed
After run coding, it shows
class java.io.FileNotFoundException
So the answer should be E
Did you really tried to code and compile the code? I tried and it cannot be compiled. So the answer should be B.
B. The resource br of a try-with-resources statement cannot be assigned.
B. The resource br of a try-with-resources statement cannot be assigned
B
B. The resource br of a try-with-resources statement cannot be assigned
The correct Answer is B.
Tested in Eclipse :
Compile Fails at line 13 : The resource br of a try-with-resources statement cannot be assigned
B
Compilation fails at line 13:
The resource br of a try-with-resources statement cannot be assigned
b