AP Computer Science A Unit 4B: Wrapper Classes and ArrayList Practice Questions

Practice questions for AP Computer Science A Unit 4B on wrapper classes, autoboxing, and ArrayList in Java, with explanations for every answer.

71 questions · AP Computer Science A · by Study Foxy

Study these questions free

Questions

  1. Why do wrapper classes like Integer and Double exist?

    • So primitive values can be used where objects are required
    • To make arithmetic on int and double values run faster
    • To convert numbers into binary
    • To replace the int and double types entirely
  2. What is autoboxing?

    • Automatically converting a primitive to its wrapper object
    • Automatically converting a wrapper object back to a primitive
    • Packing several variables into one array
    • Closing a Scanner automatically
  3. What is unboxing?

    • Converting a wrapper object to its primitive value
    • Converting a primitive value into a wrapper object
    • Removing an element from an ArrayList
    • Reading a value from a file
  4. What is printed? Integer a = 7; int b = a + 3; System.out.println(b);

    • 10
    • 73
    • 7
    • A compile error
  5. What happens when this runs? Integer a = null; int b = a;

    • A NullPointerException is thrown
    • b is set to 0
    • b is set to null
    • The code does not compile
  6. Why does this code fail to compile? ArrayList<int> list = new ArrayList<int>();

    • ArrayList needs a reference type such as Integer
    • ArrayList must be given a starting size
    • int lists must be created with new int[]
    • The variable name list is reserved
  7. What is printed? System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);

    • true
    • false
    • An exception
    • A compile error
  8. What is printed? double d = Double.parseDouble("3.5"); System.out.println(d * 2);

    • 7.0
    • 3.53.5
    • 7
    • 6.0
  9. An ArrayList<Integer> can store int values because of autoboxing.

    True or false

  10. Integer and int are exactly the same type.

    True or false

  11. What is the wrapper class for the primitive type double?

  12. What is the wrapper class for the primitive type int?

  13. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); System.out.println(list);

    • [10, 20, 30]
    • 10 20 30
    • [10 20 30]
    • {10, 20, 30}
  14. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(1, 15); System.out.println(list);

    • [10, 15, 20, 30]
    • [15, 10, 20, 30]
    • [10, 20, 15, 30]
    • [10, 15, 30]
  15. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); int x = list.remove(1); System.out.println(x + " " + list);

    • 20 [10, 30]
    • 10 [20, 30]
    • 1 [10, 30]
    • 20 [10, 20, 30]
  16. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); int old = list.set(2, 99); System.out.println(old + " " + list);

    • 30 [10, 20, 99]
    • 99 [10, 20, 99]
    • 30 [10, 99, 30]
    • 2 [10, 20, 99]
  17. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); System.out.println(list.size() + list.get(2));

    • 33
    • 330
    • 4
    • 32
  18. What happens when this runs? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); System.out.println(list.get(3));

    • An IndexOutOfBoundsException is thrown
    • 30 is printed
    • null is printed
    • 0 is printed
  19. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(3, 40); System.out.println(list.size());

    • 4
    • 3
    • An exception
    • 40
  20. What is printed? ArrayList<String> w = new ArrayList<String>(); w.add("b"); w.add(0, "a"); w.add("c"); w.remove(1); System.out.println(w);

    • [a, c]
    • [b, c]
    • [a, b]
    • [a, b, c]
  21. What is printed? ArrayList<Integer> n = new ArrayList<Integer>(); n.add(5); n.add(7); n.remove(0); n.add(0, 9); n.set(1, n.get(0) + 1); System.out.println(n);

    • [9, 10]
    • [9, 7]
    • [10, 9]
    • [5, 10]
  22. What is printed? ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); ArrayList<Integer> b = a; b.add(2); System.out.println(a.size());

    • 2
    • 1
    • 0
    • 3
  23. What is the main advantage of an ArrayList over an array?

    • Its size can change
    • It can store primitive values such as int directly without any wrapper class
    • Its elements are always sorted
    • It uses square brackets for indexes
  24. Which call adds "x" to the end of an ArrayList<String> named s?

    • s.add("x");
    • s.append("x");
    • s[s.size()] = "x";
    • s.insert("x");
  25. How do you get the number of elements in an ArrayList named list?

    • list.size()
    • list.length
    • list.length()
    • list.count()
  26. ArrayList's remove(int index) returns the element that was removed.

    True or false

  27. After list.add(0, x), every element that was already in the list moves to the next higher index.

    True or false

  28. ArrayList elements are accessed with square brackets, like list[0].

    True or false

  29. An ArrayList<Integer> nums is [4, 8, 15]. After nums.add(1, 6), what does nums.get(2) return?

  30. An ArrayList<String> has 5 elements. After two calls to remove(0), what does size() return?

  31. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); int sum = 0; for (int i = 0; i < list.size(); i++) { sum += list.get(i); } System.out.println(sum);

    • 60
    • 30
    • 102030
    • 3
  32. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); for (int x : list) { System.out.print(x / 10); }

    • 123
    • 102030
    • 6
    • 1 2 3
  33. What is printed? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); for (int i = list.size() - 1; i >= 0; i--) { System.out.print(list.get(i) + " "); }

    • 30 20 10
    • 10 20 30
    • 30 20
    • 20 10
  34. What is printed? ArrayList<Integer> v = new ArrayList<Integer>(); v.add(2); v.add(4); v.add(5); v.add(6); for (int i = 0; i < v.size(); i++) { if (v.get(i) % 2 == 0) { v.remove(i); } } System.out.println(v);

    • [4, 5]
    • [5]
    • [2, 4, 5, 6]
    • [4, 5, 6]
  35. What is printed? ArrayList<Integer> v = new ArrayList<Integer>(); v.add(2); v.add(4); v.add(5); v.add(6); for (int i = v.size() - 1; i >= 0; i--) { if (v.get(i) % 2 == 0) { v.remove(i); } } System.out.println(v);

    • [5]
    • [4, 5]
    • [2, 4, 6]
    • []
  36. What happens when this runs? ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); for (Integer x : list) { if (x == 10) { list.remove(0); } }

    • A ConcurrentModificationException is thrown
    • The first element is removed and the loop finishes
    • Nothing changes
    • The code does not compile
  37. What is printed? ArrayList<String> w = new ArrayList<String>(); w.add("apple"); w.add("kiwi"); w.add("fig"); String longest = w.get(0); for (String s : w) { if (s.length() > longest.length()) { longest = s; } } System.out.println(longest);

    • apple
    • kiwi
    • fig
    • 5
  38. Why is it risky to remove elements from an ArrayList while traversing it forward with i++?

    • Elements shift left, so the next one gets skipped
    • ArrayList refuses to remove elements while a loop is running
    • Removing elements changes their values
    • It makes the list longer
  39. Which loop is safest for removing all elements that meet a condition?

    • An indexed loop that runs backward from the end
    • An enhanced for loop over the list
    • A forward indexed loop with i++ on every pass
    • A loop that runs from 0 to the list's original size
  40. You may add or remove elements of an ArrayList inside an enhanced for loop that traverses it.

    True or false

  41. An enhanced for loop can be used to read every element of an ArrayList.

    True or false

  42. ArrayList<Integer> a holds [3, 6, 9]. int t = 0; for (int x : a) { t += x; } What is t?

  43. What is printed? ArrayList<Integer> a = new ArrayList<Integer>(); a.add(3); a.add(9); a.add(4); int maxIdx = 0; for (int i = 1; i < a.size(); i++) { if (a.get(i) > a.get(maxIdx)) { maxIdx = i; } } System.out.println(maxIdx);

    • 1
    • 9
    • 2
    • 0
  44. What is printed? ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(3); a.add(7); int v = 5; int i = 0; while (i < a.size() && a.get(i) < v) { i++; } a.add(i, v); System.out.println(a);

    • [1, 3, 5, 7]
    • [1, 3, 7, 5]
    • [5, 1, 3, 7]
    • [1, 5, 3, 7]
  45. What is printed? ArrayList<String> src = new ArrayList<String>(); src.add("a"); src.add("b"); src.add("a"); src.add("c"); ArrayList<String> out = new ArrayList<String>(); for (String s : src) { boolean found = false; for (String t : out) { if (t.equals(s)) { found = true; } } if (!found) { out.add(s); } } System.out.println(out);

    • [a, b, c]
    • [a, b, a, c]
    • [b, c]
    • [c, b, a]
  46. What is printed? ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(2); a.add(3); ArrayList<Integer> r = new ArrayList<Integer>(); for (int x : a) { r.add(0, x); } System.out.println(r);

    • [3, 2, 1]
    • [1, 2, 3]
    • [3, 1, 2]
    • [1, 3, 2]
  47. What is printed? ArrayList<Integer> a = new ArrayList<Integer>(); a.add(8); a.add(3); a.add(5); int t = a.get(0); a.set(0, a.get(a.size() - 1)); a.set(a.size() - 1, t); System.out.println(a);

    • [5, 3, 8]
    • [8, 3, 5]
    • [5, 3, 5]
    • [3, 8, 5]
  48. What is printed? ArrayList<String> names = new ArrayList<String>(); names.add("Lee"); names.add("Kai"); names.add("Lou"); int count = 0; for (String n : names) { if (n.substring(0, 1).equals("L")) { count++; } } System.out.println(count);

    • 2
    • 3
    • 1
    • 0
  49. What is printed? ArrayList<Integer> a = new ArrayList<Integer>(); for (int i = 1; i <= 5; i++) { a.add(i * i); } System.out.println(a.get(a.size() - 1) - a.get(0));

    • 24
    • 25
    • 4
    • 5
  50. Which algorithm removes all occurrences of a value from an ArrayList correctly?

    • Loop from the end to the start and remove matches
    • Loop from the start with i++ and remove matches
    • Use an enhanced for loop and remove matches
    • Call remove(value) once

Showing 50 of 71. Open the quiz to study them all.