📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    A programmer is trying to debug an issue where an

    `ArrayList` (or equivalent dynamic array) in their Java/Python code unexpectedly throws an  `IndexOutOfBoundsException` during an iteration. Which debugging technique would be most effective to pinpoint the exact cause? 
    A Setting a breakpoint at the start of the program and running it. Correct Answer Incorrect Answer
    B Using print statements to display the list size and current index inside the loop. Correct Answer Incorrect Answer
    C Commenting out the entire loop to see if the error disappears. Correct Answer Incorrect Answer
    D Changing the `ArrayList` to a `LinkedList` without further investigation. Correct Answer Incorrect Answer

    Solution

    An `IndexOutOfBoundsException` during iteration strongly suggests an issue with the loop bounds or the index being used. Printing the list size and the current index (`i`) within the loop will immediately show when `i` exceeds or equals the list's valid index range.

    Practice Next
    ask-question