Junior — Middle
Can you give an example of a situation where an error occurs in a program?
sobes.tech AI
Answer from AI
An error in a program occurs when an unexpected event happens that disrupts the normal execution of the code. For example, division by zero, accessing an uninitialized variable, or going out of array bounds.
An example in Java:
int[] numbers = {1, 2, 3};
int value = numbers[5]; // ArrayIndexOutOfBoundsException
Here, attempting to access the element at index 5 causes an error because the array only contains 3 elements (indices 0, 1, 2). This error results in an exception during program execution.