What happens when you run a program that creates an array of ints and then sets a value beyond its length?

Single Choice
Views 31

Answer:

If a program tries to access an element beyond the array’s size, Java will throw an ArrayIndexOutOfBoundsException.

Example:


public class Example {
    public static void main(String[] args) {
        int[] ints = new int[50];
        ints[60] = 12345;  // Error: index 60 doesn’t exist
    }
}

Output:


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at Example.main(Example.java:4)

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.