3 Matching Annotations
  1. Mar 2025
    1. What does the following code do?

      The code creates an ArrayList<integer> called myList and adds the integers 50, 30, and 20 to it.

      It then computes the sum of all the elements in myList using an enhanced for loop and prints it out with the message "Sum of all elements: ".

      The output will be the sum of 50, 30, and 20, which is 100.</integer>

    2. The following code removes a name from a list. Set the found variable to the appropriate true or false values at line 13 and line 20 to make the code work. Run

      To make the code work, we need to correctly set the found variable to true when the name is found and removed, and false when the name is not found after the loop completes.

      Fix: Set found = true; when a match is found and the name is removed. Initially set found = false; before starting the loop, because we assume the name is not found unless we find it.

    3. The following code will throw an IndexOutOfBoundsException. Can you fix it?

      You need to change the loop condition to i < myList.size() to ensure that the loop stops before accessing an index that is out of bounds.