Array Questions

Java Array Output Questions with Answers | Tricky Array Interview Programs

Java Array Output Questions with Answers

Arrays are one of the most heavily tested topics in Java interviews — and also one of the trickiest to master. 🎯

In this post, you'll work through 8 output-based array questions that test your understanding of:

  • Array initialization — default values, null arrays, empty arrays
  • Multidimensional arrays — outer vs inner dimension sizing
  • ArrayIndexOutOfBoundsException — accessing invalid indexes
  • NullPointerException — dereferencing null arrays
  • ArrayStoreException — storing incompatible types
  • Reference type casting — Number[], Object[], primitive arrays

How to use: Read each question, decide the output in your head, then click Show Answer to verify. Don't run the code first — that's cheating! 😄

💡 Important: Many of these questions produce compilation errors or runtime exceptions, not normal output. Distinguishing between them is exactly what interviewers want to see.

Questions 1–5: Array Access & Initialization

Question 1

What is the output?

public class ArrayDemo {
    public static void main(String[] args) {
        int[][] arr = new int[2][];
        System.out.println(arr[0][0]);
    }
}
✅ Show Answer

Runtime Exception: NullPointerException

new int[2][] creates an outer array of length 2, but the inner arrays are null. Accessing arr[0][0] tries to dereference a null inner array → NPE.

💡 Interview Tip: This is a classic — multidimensional array with only the first dimension initialized.

Question 2

What is the output?

public class ArrayDemo {
    public static void main(String[] args) {
        int[] a = null;
        System.out.println(a[0]);
    }
}
✅ Show Answer

Runtime Exception: NullPointerException

a is null, so a[0] throws NPE. Compiles fine — fails at runtime.

Question 3

What is the output?

public class ArrayDemo {
    public static void main(String[] args) {
        int[] a = {};
        System.out.println(a[0]);
    }
}
✅ Show Answer

Runtime Exception: ArrayIndexOutOfBoundsException

{} creates an empty array with length 0. Accessing index 0 is out of bounds (valid range is empty).

Question 4

What is the output?

public class ArrayDemo {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        System.out.println(a[0]);
    }
}
✅ Show Answer

Output: 1

Array is properly initialized with 3 elements. Index 0 holds the value 1.

Question 5

What is the output?

public class ArrayDemo {
    public static void main(String[] args) {
        int[][] arr = new int[][2];
        System.out.println(arr[0][0]);
    }
}
✅ Show Answer

Compilation Error.

In Java, you must specify the size of the first dimension. The inner dimensions may be left blank. So new int[2][] is valid, but new int[][2] is a compile-time error.

Fix: int[][] arr = new int[2][2];

Questions 6–8: Array Type Casting & Compatibility

Question 6

What is the output?

public class Arr {
    public static void main(String[] args) {
        Object[] sArr = new String[3];
        sArr[2] = 10;
    }
}
✅ Show Answer

Runtime Exception: ArrayStoreException

sArr is declared as Object[] but points to a String[]. Storing an Integer (from autoboxing 10) into this array violates the runtime component type (String) → ArrayStoreException.

💡 Why it compiles: At compile time, the reference type is Object[], and Integer is an Object. The check happens at runtime.

Question 7

What is the output?

public class Arr {
    public static void main(String[] args) {
        Number[] sArr = new int[3];
        sArr[0] = 10;
    }
}
✅ Show Answer

Compilation Error.

int is a primitive, and primitives cannot be assigned to Number[]. You cannot mix primitive arrays with reference-type arrays.

Fix: Number[] sArr = new Integer[3];

Question 8

What is the output?

public class Arr {
    public static void main(String[] args) {
        Number[] sArr = new Float[3];
        sArr[0] = 10;
    }
}
✅ Show Answer

Runtime Exception: ArrayStoreException

Number[] sArr = new Float[3] compiles because Float is a subtype of Number. But storing 10 — which autoboxes to Integer — into a Float[] array throws ArrayStoreException at runtime.

💡 Key Rule: The runtime component type of the array must match the type of the element being stored.

Final Thoughts

Congratulations on completing all 8 questions! 🎉

Array questions test subtle aspects of Java that go beyond syntax — object references, runtime type checks, and default initialization. If you can answer these confidently, you're ready for real interview questions on arrays.

🔑 Key Rules to Remember

  1. Multidimensional array declaration: You must specify the size of the first dimension. new int[2][] is valid; new int[][2] is not.
  2. Null arrays throw NPE: Accessing elements of a null array throws NullPointerException.
  3. Empty arrays throw AIOOBE: new int[0] is valid, but accessing any index throws ArrayIndexOutOfBoundsException.
  4. Primitive arrays ≠ reference arrays: int[] cannot be assigned to Number[] or Object[].
  5. ArrayStoreException at runtime: When you store a value whose type doesn't match the runtime component type of the array.
  6. Compile-time check vs runtime check: The reference type is checked at compile time, the actual component type at runtime.

📚 Practice More

Try to modify these programs and predict new outputs. The more you practice, the sharper your Java intuition becomes.

Want the next level? Check out:

  • Java String output-based questions
  • Java collections output-based questions
  • Multithreading tricky programs
  • Exception handling output questions

No comments: