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

  • Last post

    I don't plan to delete my LJ (I paid for permanent status, dammit), but this will be the last post. I don't plan to read it anymore, either…

  • Unemployed

    Turns out my insurance is cut off at midnight tonight, not the end of the month. In a way, that's a good thing. Now I'll move my appointment…

  • Home from the cruise, off to Sunnyvale

    A week off, but tomorrow I head to the home office for a week there.

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 2 comments