King Rat (gkr) wrote,
King Rat
gkr

Java Pre-assessment

So, I'm taking an online course in Java because I've never programmed in it before. In the section on exceptions, the pretest has this question:
Select the code in which an ArrayIndexOutOfBoundsException occurs and is properly caught.
  1. try {
      int intArray = new int[5];
      arr[7] = 5;
      System.out.println(arr[7]);
    } finally {
      System.out.println("Finally Caught");
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Caught");
    }
  2. try {
      int intArray = new int[5];
      arr[7] = 5;
      System.out.println(arr[7]);
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Caught");
    } finally {
      System.out.println("Finally Caught");
    }
  3. int intArray = new int[5];
    arr[7] = 5;
    System.out.println(arr[7]);
     catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Caught");
    } finally {
      System.out.println("Finally Caught");
    }
  4. try {
      int intArray = new int[7];
      arr[5] = 5;
      System.out.println(arr[7]);
      System.out.println("Finally Caught");
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Caught");
    }

Select the correct answer.

A
0(0.0%)
B
1(50.0%)
C
0(0.0%)
D
1(50.0%)

For those who aren't programmers, it's a trick question. I've highlighted in red the key to the trick.

The answer according to the test is B. The real answer is none of the above. Their proofreaders missed the fact that the variable name used is incorrect. There will be no exception because it won't run in the first place, much less run and throw an exception.

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 2 comments