Difficulty: Easy
Correct Answer: An exception is thrown at runtime
Explanation:
Introduction / Context: Java supports jagged arrays, where only the outer dimension may be allocated initially. Inner arrays default to null until explicitly created. Attempting to index into a null inner array causes a NullPointerException at runtime.
Given Data / Assumptions:
Concept / Approach: theDogs[2] is null; dereferencing it with [0] triggers NullPointerException before any call to toString() can occur. Compilation succeeds because the types are correct; failure occurs at runtime.
Step-by-Step Solution:
Allocate outer array → inner rows are null.Attempt index [2] → returns null reference for Dog[].Apply [0] to null → NullPointerException.Program terminates with an exception; no valid string is printed.Verification / Alternative check: Fix by allocating inner arrays: theDogs[2] = new Dog[1]; theDogs[2][0] = new Dog(); System.out.println(theDogs[2][0]);
Why Other Options Are Wrong:
Common Pitfalls: Assuming both dimensions are allocated together; forgetting to new each inner row for jagged arrays.
Final Answer: An exception is thrown at runtime
Discussion & Comments