Java OCA OCP Practice Question 246

Question

Given:

1. public class Main {
2.     // insert code here
3.     int x = 0;
4.     System.out.println(7/x);
5.   }
6. }

And given the following four code fragments:

I.   public static void main(String[] args) {
II.  public static void main(String[] args) throws Exception {
III. public static void main(String[] args) throws IOException {
IV.  public static void main(String[] args) throws RuntimeException {

If the four fragments are inserted independently at line 2, which are true?

Choose all that apply.

  • A. All four will compile and execute without exception
  • B. All four will compile and execute and throw an exception
  • C. Some, but not all, will compile and execute without exception
  • D. Some, but not all, will compile and execute and throw an exception
  • E. When considering fragments II, III, and IV, of those that will compile, adding a try/catch block around line 4 will cause compilation to fail


D is correct.

Note

A, B, and C are incorrect.

To throw an IOException, you have to import the java.io package or declare the exception with a fully qualified name.

E is incorrect because it's okay both to handle and declare an exception.




PreviousNext

Related