What is the result?

Given:
<code>
import java.io.*;
public class SampleClass {
public static void main(String[] args) throws IOException {
try {
String dirName = args[0];
File dir = new File(dirName);
File.createTempFile(“temp”, “log”, dir);
} catch (NullPointerException | IOException e) {
e = new IOException(“Error while creating temp file”);
throw e;
}
}
}
</code>

What is the result?

Given:
<code>
import java.io.*;
public class SampleClass {
public static void main(String[] args) throws IOException {
try {
String dirName = args[0];
File dir = new File(dirName);
File.createTempFile(“temp”, “log”, dir);
} catch (NullPointerException | IOException e) {
e = new IOException(“Error while creating temp file”);
throw e;
}
}
}
</code>

What is the result?

A.
Compilation fails.

B.
An IOException with the default message is thrown at runtime.

C.
An IOException with the message Error while creating temp file is thrown at runtime.

D.
A temporary file is created in the specified directory.

Explanation:
The multi-catch parameter e may not be assigned. The compilation will fail at line:
e = new IOException(“Error while creating temp file”);



Leave a Reply 0

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