Home

Tutoring

Subjects

Live Classes

Study Coach

Essay Review

On-Demand Courses

Colleges

Games

Opening subject page...

Loading your content

← Back to quizzes

AP Computer Science a

AP Computer Science a Quiz: Wrapper Classes

Practice Wrapper Classes in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Wrapper Classes, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

Question 1

Based on the example code, collections require objects, so Integer wraps primitive scores: ```java ArrayList scores = new ArrayList<>(); Integer bonus = 4; scores.add(bonus);

  1. ArrayList scores = new ArrayList();
  2. ArrayList scores = new ArrayList<>();
  3. ArrayList scores = new ArrayList();
  4. ArrayList scores = new IntegerList<>();
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide object representations of primitive types, and proper syntax is crucial when declaring collections that use them. In this scenario, the code demonstrates creating an ArrayList that stores Integer objects and adding an Integer wrapper object to it. Choice B is correct because it shows the proper generic syntax for declaring an ArrayList of Integer objects, using the diamond operator <> on the right side which is valid Java 7+ syntax. Choice A is incorrect because ArrayList cannot use primitive types like int as type parameters - it must use the wrapper class Integer. To help students: Emphasize that generics in Java require reference types (objects), not primitives, which is why we use Integer instead of int. Practice writing declarations for different wrapper class collections and explain why primitive types cannot be used directly in angle brackets.

Question 2

In the given program, an ArrayList stores score objects, so it uses Integer rather than primitive int.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        // Add scores using the Integer wrapper type
        scores.add(Integer.valueOf(30));
        scores.add(40); // autoboxing
        System.out.println(scores.get(0));
    }
}

Which of the following is a correct use of the Integer wrapper class in the code?

  1. scores.add(Integer.valueOf(30));
  2. scores.add(int.valueOf(30));
  3. scores.add(new int(30));
  4. scores.add(Integer.value(30));
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide object representations of primitive types, and the Integer class specifically wraps int values with methods like valueOf() for explicit object creation. In this scenario, the code demonstrates both explicit wrapper creation using Integer.valueOf(30) and implicit autoboxing with scores.add(40). Choice A is correct because Integer.valueOf(30) is the proper static method to create an Integer object from a primitive int value. Choice B is incorrect because int is a primitive type and doesn't have methods like valueOf() - only wrapper classes have such methods. To help students: Practice using different ways to create wrapper objects (constructor, valueOf, autoboxing) and understand when each is appropriate. Emphasize that valueOf() is preferred over constructors for efficiency, as it may reuse cached Integer objects for common values.

Question 3

In the given program, collections store objects, so scores use wrappers not primitives.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        // Add primitive int values; autoboxing wraps them as Integer
        scores.add(10);
        scores.add(25);
        int total = scores.get(0) + scores.get(1); // unboxing for addition
        System.out.println(total);
    }
}

How does autoboxing work in the given example?

  1. It converts each int into an Integer when calling add.
  2. It converts each Integer into an int when calling add.
  3. It stores int values directly because ArrayList accepts primitives.
  4. It requires calling Integer.box(10) before add works.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide a way to use primitive data types (int, double, etc.) as objects, which are necessary for data collections like ArrayLists that can only store objects. In this scenario, when scores.add(10) is called, Java automatically converts the primitive int value 10 into an Integer object through autoboxing. Choice A is correct because it accurately explains that autoboxing converts each int into an Integer when calling add, allowing primitive values to be stored in the ArrayList. Choice B is incorrect because it reverses the process - autoboxing converts primitives to objects, not objects to primitives. To help students: Practice tracing through code that uses autoboxing and unboxing, emphasizing when conversions happen automatically. Create examples showing the difference between primitive arrays and ArrayLists to reinforce why wrapper classes are needed.

Question 4

Based on the example code, collections store objects, so scores use Integer wrappers; Java boxes and unboxes as needed.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(4);
        Integer y = scores.get(0);
        int z = y; // unboxing
        System.out.println(z);
    }
}

How does autoboxing work in the given example?

  1. It converts y into int z automatically during assignment.
  2. It converts z into Integer y automatically during assignment.
  3. It converts scores into int z automatically during printing.
  4. It requires Integer.parseInt(y) to assign into z.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java support automatic conversion between primitive types and their object equivalents, with unboxing being the conversion from wrapper objects to primitives. In this scenario, when int z = y is executed, Java automatically unboxes the Integer object y into a primitive int value that can be assigned to z. Choice A is correct because it accurately describes that unboxing converts the Integer object y into the primitive int z automatically during the assignment. Choice D is incorrect because Integer.parseInt() is used to convert String to int, not Integer to int - unboxing handles Integer to int conversion automatically. To help students: Create examples showing the difference between unboxing (Integer to int) and parsing (String to int). Practice identifying implicit conversions in code and understanding when Java performs them automatically versus when explicit conversion is needed.

Question 5

In the given program, collections store objects, so Integer wraps ints for an ArrayList: ```java ArrayList scores = new ArrayList<>(); scores.add(9); // autoboxing int s = scores.get(0); // unboxing

  1. Java turns 9 into an Integer when calling add
  2. Java turns Integer into 9 when calling add
  3. Java stores 9 as a char in the ArrayList
  4. Java requires casting: scores.add((Integer) 9)
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide a way to use primitive data types as objects, with autoboxing automatically converting primitives to their wrapper equivalents when needed. In this scenario, when the primitive int literal 9 is passed to scores.add(), Java automatically creates an Integer object containing the value 9 through autoboxing. Choice A is correct because it accurately describes that Java converts the primitive int 9 into an Integer object when calling add. Choice D is incorrect because explicit casting is not required - Java handles the conversion automatically through autoboxing. To help students: Demonstrate that autoboxing eliminates the need for manual conversion code like Integer.valueOf() or explicit casting. Show examples of both the old way (pre-Java 5) and the modern autoboxing approach to highlight how Java simplifies working with collections.

Question 6

Based on the example code, collections require object types, so the score list uses Integer wrappers while math uses int.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(1);
        scores.add(2);
        int x = scores.get(0); // unboxing
        scores.set(1, x + 5);  // boxing
        System.out.println(scores.get(1));
    }
}

How does autoboxing work in the given example?

  1. It boxes x + 5 into an Integer when calling set.
  2. It boxes scores into an Integer when calling get.
  3. It turns Integer into double to support addition.
  4. It requires new Integer(x + 5) or set fails to compile.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java enable seamless conversion between primitives and objects through autoboxing and unboxing, allowing natural syntax when working with collections. In this scenario, the code demonstrates both unboxing (when scores.get(0) returns an Integer that's converted to int x) and autoboxing (when x + 5 produces an int that's converted to Integer for the set method). Choice A is correct because it accurately describes that the expression x + 5 results in a primitive int value that is automatically boxed into an Integer object when passed to the set method. Choice D is incorrect because modern Java (5+) doesn't require explicit Integer construction - autoboxing handles this automatically. To help students: Create flowcharts showing the conversion process during method calls. Practice identifying which operations trigger autoboxing versus unboxing, emphasizing that Java handles these conversions transparently.

Question 7

Based on the example code, collections require objects, so Integer wraps an int: ```java ArrayList scores = new ArrayList<>(); scores.add(7); // autoboxing scores.add(12);

  1. They prevent scores from changing after insertion
  2. They allow int values to be stored in ArrayList
  3. They make the ArrayList store primitive int directly
  4. They automatically sort scores as they are added
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide a way to use primitive data types as objects, which is essential because collections like ArrayList can only store object references, not primitive values. In this scenario, the ArrayList is declared with Integer as its type parameter, allowing it to store wrapped versions of int values through autoboxing when primitives 7 and 12 are added. Choice B is correct because it accurately explains that wrapper classes allow int values to be stored in ArrayList by converting them to Integer objects. Choice C is incorrect because ArrayLists cannot store primitive types directly - they require objects, which is why wrapper classes exist. To help students: Create visual diagrams showing the conversion from primitive to wrapper object when adding to collections. Emphasize that collections like ArrayList require objects, making wrapper classes essential for storing primitive data types in these structures.

Question 8

In the given program, an ArrayList stores objects, so scores use Integer wrappers instead of primitive int values.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        // Wrapper objects can be stored; primitives cannot be generic types
        scores.add(11);
        scores.add(22);
        System.out.println(scores);
    }
}

What is the purpose of using wrapper classes in this scenario?

  1. They allow generics like ArrayList to store score values.
  2. They let ArrayList accept int as a type parameter directly.
  3. They force scores to print in binary format by default.
  4. They eliminate the need for method calls like add and get.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java are essential for using primitive types with generics because Java's type system requires generic type parameters to be reference types (objects), not primitive types. In this scenario, ArrayList uses the Integer wrapper class because ArrayList would be a compilation error - generics cannot use primitive types directly. Choice A is correct because it accurately explains that wrapper classes enable generics like ArrayList to store primitive-like values by providing object representations. Choice B is incorrect because Java's generics system fundamentally cannot accept primitive types as type parameters - this is why wrapper classes exist. To help students: Show compilation errors that occur when attempting to use primitives with generics. Create examples demonstrating how wrapper classes enable advanced features like storing null values and using collection methods that require objects.

Question 9

Based on the example code, collections store objects, so scores use Integer objects while arithmetic uses primitives.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(5);
        scores.add(9);
        int sum = scores.get(0) + scores.get(1); // unboxing
        System.out.println(sum);
    }
}

How does autoboxing work in the given example?

  1. It wraps 5 and 9 into Integer objects during add.
  2. It unwraps Integer objects into int values during add.
  3. It changes ArrayList into an int[] automatically at runtime.
  4. It requires casting: (Integer) 5 before calling add.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java enable primitive types to be used where objects are required, and autoboxing is the automatic conversion of primitives to their corresponding wrapper objects. In this scenario, when scores.add(5) and scores.add(9) are called, Java automatically converts these primitive int values into Integer objects before storing them in the ArrayList. Choice A is correct because it accurately describes that autoboxing wraps 5 and 9 into Integer objects during the add operation. Choice B is incorrect because it describes unboxing (which happens during the arithmetic operation) rather than autoboxing (which happens during add). To help students: Create side-by-side comparisons of code with and without autoboxing to show how Java simplifies the syntax. Practice identifying where autoboxing occurs (primitive to object) versus unboxing (object to primitive) in different contexts.

Question 10

Based on the example code, data collections require object types, so scores use wrappers instead of primitives.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        // Integer wrapper stores score objects in the list
        scores.add(7);
        scores.add(13);
        System.out.println(scores.size());
    }
}

What is the purpose of using wrapper classes in this scenario?

  1. They make int variables mutable for updates in the list.
  2. They allow primitive values to be stored as objects in ArrayList.
  3. They automatically sort scores when elements are added.
  4. They prevent any unboxing during arithmetic operations.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide a way to use primitive data types as objects, which is essential because collections like ArrayList can only store object references, not primitive values. In this scenario, the ArrayList declaration requires Integer objects, so when scores.add(7) is called, the primitive int 7 is automatically wrapped into an Integer object. Choice B is correct because it accurately states that wrapper classes allow primitive values to be stored as objects in ArrayList, which is the fundamental purpose of wrapper classes in collections. Choice A is incorrect because wrapper classes don't make primitives mutable - Integer objects are actually immutable. To help students: Use visual diagrams showing the difference between primitive storage and object storage in memory. Practice identifying situations where wrapper classes are required versus optional, emphasizing that generic collections always need wrapper classes.

Question 11

Based on the example code, collections store objects, so the score list uses Integer wrappers, not primitive int.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(3);
        scores.add(6);
        Integer first = scores.get(0);
        System.out.println(first + 1); // unboxing for addition
    }
}

How does autoboxing work in the given example?

  1. It converts the ArrayList into an Integer automatically.
  2. It converts 3 and 6 into Integer objects when added to scores.
  3. It converts Integer objects into Strings when printing.
  4. It requires calling scores.autobox(3) before add.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide object equivalents for primitive types, and autoboxing is the automatic conversion process that occurs when a primitive is used where an object is expected. In this scenario, when scores.add(3) and scores.add(6) are executed, Java automatically converts these primitive int values 3 and 6 into Integer objects through autoboxing before storing them in the ArrayList. Choice B is correct because it accurately describes that autoboxing converts 3 and 6 into Integer objects when they are added to the scores ArrayList. Choice A is incorrect because autoboxing doesn't convert the ArrayList itself - it converts individual primitive values into wrapper objects. To help students: Trace through the code line by line, identifying each point where autoboxing or unboxing occurs. Create exercises where students predict whether a conversion will happen automatically or require explicit code.

Question 12

In the given program, an ArrayList stores score objects, so it uses Integer wrapper types instead of primitive int.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(100);
        scores.add(50);
        int diff = scores.get(0) - scores.get(1); // unboxing
        System.out.println(diff);
    }
}

What is the purpose of using wrapper classes in this scenario?

  1. They allow storing scores as objects in an ArrayList.
  2. They replace the need for ArrayList by using primitive arrays.
  3. They prevent null values by forcing default int initialization.
  4. They automatically clamp scores to a valid range.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java bridge the gap between primitive types and object-oriented programming by providing object representations of primitives, which is essential for using primitives with Java's collection framework. In this scenario, the ArrayList stores Integer objects representing scores of 100 and 50, and when arithmetic operations are performed, these Integer objects are automatically unboxed to primitive int values. Choice A is correct because it identifies the fundamental purpose of wrapper classes: allowing primitive values to be stored as objects in collections like ArrayList. Choice B is incorrect because wrapper classes don't replace ArrayLists - they enable ArrayLists to work with primitive-like data. To help students: Compare and contrast primitive arrays (int[]) with ArrayList to show different use cases. Emphasize that wrapper classes provide the flexibility to use primitives in object-only contexts while maintaining type safety.

Question 13

In the given program, an ArrayList requires objects, so scores use Integer wrappers instead of primitive int values.

import java.util.ArrayList;

public class ScoreTracker {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(12);
        scores.add(8);
        int last = scores.get(1); // unboxing to int
        System.out.println(last);
    }
}

What is the purpose of using wrapper classes in this scenario?

  1. They let ArrayList store primitive int values directly.
  2. They allow int data to be stored as Integer objects in a list.
  3. They speed up printing by avoiding method calls.
  4. They guarantee scores cannot be changed once added.
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java serve as object representations of primitive types, enabling primitives to be used in contexts that require objects, such as generic collections. In this scenario, ArrayList cannot directly store primitive int values because generics work only with reference types, so the Integer wrapper class is used to wrap the int values 12 and 8. Choice B is correct because it accurately explains that wrapper classes allow int data to be stored as Integer objects in a list, which is their primary purpose in collections. Choice A is incorrect because ArrayList cannot store primitive values directly - it requires objects, which is why wrapper classes exist. To help students: Use concrete examples showing compilation errors when trying to use primitives with generics. Demonstrate the relationship between each primitive type and its corresponding wrapper class, emphasizing that this object-based storage enables additional functionality.

Question 14

Based on the example code, collections require objects, so Integer wraps primitive scores: ```java ArrayList scores = new ArrayList<>(); Integer scoreObj = Integer.valueOf(11); scores.add(scoreObj);

  1. Integer scoreObj = Integer.valueOf(11);
  2. int scoreObj = int.valueOf(11);
  3. Integer scoreObj = new int(11);
  4. Integer scoreObj = Integer.value(11);
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide methods to explicitly create wrapper objects from primitive values, with Integer.valueOf() being the preferred factory method for creating Integer instances. In this scenario, the code demonstrates the explicit creation of an Integer object using the valueOf method before adding it to the ArrayList. Choice A is correct because it shows the proper syntax for using Integer.valueOf() to create an Integer object from the primitive int 11. Choice B is incorrect because primitive types like int don't have methods - valueOf is a static method of the Integer wrapper class. To help students: Explain that while autoboxing often handles conversions automatically, knowing explicit methods like valueOf() is important for understanding what happens behind the scenes. Discuss when explicit wrapper object creation might be preferred over relying on autoboxing.

Question 15

Based on the example code, collections require objects, so Integer wraps primitive scores: ```java ArrayList scores = new ArrayList<>(); int points = 8; scores.add(points);

  1. They let the ArrayList hold primitive int values
  2. They let the ArrayList hold Integer objects for ints
  3. They force scores to be stored as Strings
  4. They eliminate the need for an ArrayList entirely
Explanation: This question tests AP Computer Science A skills: understanding and applying wrapper classes in Java. Wrapper classes in Java provide object representations of primitive data types, which is necessary because collections like ArrayList can only store object references, not primitive values. In this scenario, when the primitive int 'points' is added to the ArrayList, it is automatically converted to an Integer object that can be stored in the collection. Choice B is correct because it accurately states that wrapper classes allow the ArrayList to hold Integer objects that represent int values. Choice A is incorrect because it suggests the ArrayList holds primitive values directly, which is impossible - the ArrayList holds Integer objects that wrap the primitive values. To help students: Clarify the distinction between storing primitives directly (impossible in collections) versus storing wrapper objects that contain primitive values. Use memory diagrams to show how Integer objects are stored in the ArrayList, not raw int values.