Skip to main content

Java Topic Practice

ArrayList and Wrapper Classes

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

For practice use only.

ArrayList and Wrapper Classes MCQ Practice

ArrayList and Wrapper Classes Java practice with 15 questions, code tracing, and a worked explanation for every answer.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - ArrayList and Wrapper Classes: track the final printed value.

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> nums = new ArrayList<Integer>();
    nums.add(3);
    nums.add(7);
    nums.add(3);
    nums.add(9);
    nums.remove(1);
    System.out.print(nums);
  }
}

All 15 arraylist and wrapper classes questions

Work through the interactive quiz above first. This is the full arraylist and wrapper classes question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - ArrayList and Wrapper Classes: track the final printed value.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(3);
        nums.add(7);
        nums.add(3);
        nums.add(9);
        nums.remove(1);
        System.out.print(nums);
      }
    }
    • [7, 3, 9]
    • The code does not compile.
    • [3, 3, 9]
    • [3, 7, 3, 9]
    Show worked solution

    Correct answer: [3, 3, 9]

    With an int argument, remove is treated as remove-by-index, so position 1 is removed rather than the value 1.

  2. 2.Code Tracing 12 - ArrayList and Wrapper Classes: follow the variable updates.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(5);
        nums.add(9);
        nums.add(5);
        nums.add(11);
        nums.remove(Integer.valueOf(5));
        System.out.print(nums);
      }
    }
    • [5, 9, 11]
    • [5, 9, 5, 11]
    • [9, 11]
    • [9, 5, 11]
    Show worked solution

    Correct answer: [9, 5, 11]

    Wrapping the argument as an Integer selects remove-by-value, which deletes the first element equal to it.

  3. 3.Code Tracing 28 - ArrayList and Wrapper Classes: trace the branch and loop path.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(8);
        nums.add(8);
        nums.add(3);
        nums.add(11);
        int total = 0;
        for (Integer v : nums) {
          total += v;
        }
        System.out.print(total);
      }
    }
    • 4
    • 22
    • 8
    • 30
    Show worked solution

    Correct answer: 30

    Each Integer is auto-unboxed to an int when it takes part in arithmetic, so the sum works without an explicit conversion.

  4. 4.Code Tracing 39 - ArrayList and Wrapper Classes: watch the index changes.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        Integer a = 10;
        Integer b = 10;
        System.out.print(a.equals(b) + ":" + (a.intValue() == b.intValue()));
      }
    }
    • true:true
    • false:true
    • true:false
    • false:false
    Show worked solution

    Correct answer: true:true

    equals compares the wrapped values, and intValue unwraps to primitives where == compares numbers directly. Both report equal here.

  5. 5.Code Tracing 50 - ArrayList and Wrapper Classes: evaluate the state change step by step.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(12);
        nums.add(null);
        int value = nums.get(1);
        System.out.print(value);
      }
    }
    • (no output)
    • A runtime exception is thrown.
    • 0
    • null
    Show worked solution

    Correct answer: A runtime exception is thrown.

    An Integer list can hold null, but unboxing that null into an int throws NullPointerException.

  6. 6.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(100);
    l.add(100);
    boolean a = l.get(0) == l.get(1);
    boolean b = l.get(0).equals(l.get(1));
    System.out.print(a + " " + b);
    • false true
    • true true
    • true false
    • false false
    Show worked solution

    Correct answer: true true

    Small Integer values are cached, so the reference comparison happens to succeed here. Larger values would not.

  7. 7.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(1000);
    l.add(1000);
    boolean a = l.get(0) == l.get(1);
    boolean b = l.get(0).equals(l.get(1));
    System.out.print(a + " " + b);
    • true true
    • false false
    • false true
    • true false
    Show worked solution

    Correct answer: false true

    Beyond the cached range each boxing creates a separate object, so only equals is reliable.

  8. 8.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(7);
    int a = l.get(0);
    Integer b = l.get(0);
    System.out.print(a + b);
    • 7
    • 77
    • The code does not compile.
    • 14
    Show worked solution

    Correct answer: 14

    Unboxing lets an Integer take part in ordinary int arithmetic.

  9. 9.What is printed by the following code segment?

    ArrayList<Double> l = new ArrayList<Double>();
    l.add(1.5);
    l.add(2.5);
    double s = 0;
    for (double v : l) { s += v; }
    System.out.print(s);
    • 4.0
    • 4
    • 3.0
    • 1.52.5
    Show worked solution

    Correct answer: 4.0

    The enhanced loop variable may be declared as a primitive because unboxing is automatic.

  10. 10.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(3);
    l.add(1);
    l.add(2);
    l.remove(Integer.valueOf(1));
    System.out.print(l);
    • [3, 1]
    • [3, 2]
    • [1, 2]
    • [3, 2, 1]
    Show worked solution

    Correct answer: [3, 2]

    Wrapping the argument selects the by-value overload rather than the by-index one.

  11. 11.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(3);
    l.add(1);
    l.add(2);
    l.remove(1);
    System.out.print(l);
    • [3, 1]
    • [1, 2]
    • [3, 2]
    • [3, 2, 1]
    Show worked solution

    Correct answer: [3, 2]

    An int argument removes by index, which here removes the same element by coincidence.

  12. 12.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(5);
    Integer x = l.get(0);
    x = x + 1;
    System.out.print(l.get(0) + " " + x);
    • 6 6
    • 5 5
    • 6 5
    • 5 6
    Show worked solution

    Correct answer: 5 6

    Integer values are immutable, so reassigning the local leaves the stored element unchanged.

  13. 13.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 1; i <= 3; i++) { l.add(i); }
    int p = 1;
    for (Integer v : l) { p *= v; }
    System.out.print(p);
    • 6
    • 3
    • 123
    • 0
    Show worked solution

    Correct answer: 6

    Each Integer unboxes for the multiplication, giving the product of 1, 2, and 3.

  14. 14.Which declaration compiles?

    • ArrayList<int> l = new ArrayList<int>();
    • ArrayList<Integer> l = new ArrayList<Integer>();
    • ArrayList<Integer> l = new ArrayList<int>();
    • ArrayList<double> l = new ArrayList<Double>();
    Show worked solution

    Correct answer: ArrayList<Integer> l = new ArrayList<Integer>();

    A type parameter must be a class, so the wrapper class is required on both sides.

  15. 15.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(2);
    l.add(4);
    System.out.print(l.contains(2) + " " + l.indexOf(4));
    System.out.print(" " + l.contains(3));
    • true 0 false
    • false 1 false
    • true 1 false
    • true 1 true
    Show worked solution

    Correct answer: true 1 false

    Both contains and indexOf use equals, so they match on value rather than on reference.