AP Computer Science A • Unit 4.7

Wrapper Classes

Detailed answer explanations for the Unit 4.7 multiple-choice practice questions.

Q1A
Q2C
Q3B
Q4B
Q5D
Q6A
Q7C
Q8B
Q9D
Q10C
Q11B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Which statement about Integer and Double is true?

ABoth are immutable classes in java.lang.
BBoth are primitive types.
CBoth require a java.util import.
DBoth are mutable objects.
Correct answer: A. Integer and Double are object types that belong to java.lang, and both are immutable once created.
Explanation

A wrapper class represents a primitive value as an object, so Integer wraps an int and Double wraps a double. Immutable means the value stored inside a wrapper object cannot change after the object is created; a wrapper variable can only be reassigned to a different wrapper object. Because both classes live in java.lang, no import statement is needed to use them.

  • B is not correct: These are the object forms of primitives, not primitives themselves; the primitive partners are int and double.
  • C is not correct: The section places both classes in java.lang, which is exactly why the code in this topic uses them with no import line at all.
  • D is not correct: Immutability is the point of these classes: once a wrapper object holds a value, that stored value cannot be changed.
Q2

What is the return type of Integer.parseInt("27")?

AString
BInteger
Cint
Ddouble
Correct answer: C. The AP Java Quick Reference lists Integer.parseInt(String s) as returning the primitive int.
Explanation

Parsing interprets the characters in a string as a number. The two parsing methods in this topic each hand back a primitive, not an object and not a string: Integer.parseInt gives an int and Double.parseDouble gives a double.

  • A is not correct: The string is the input to the method, not the output; parsing turns those characters into a numeric value.
  • B is not correct: The method returns the primitive int; a value of wrapper type would only appear afterward through autoboxing.
  • D is not correct: double is what Double.parseDouble(String s) returns, not what the Integer parsing method returns.
Q3

Which statement relies on autoboxing?

Adouble d = 6.5;
BDouble d = 6.5;
CString d = "6.5";
Dint d = 6;
Correct answer: B. A primitive double literal is assigned to a variable of wrapper type Double, which is exactly when autoboxing happens.
Explanation

Autoboxing converts a primitive to its corresponding wrapper object, and it occurs when a primitive is assigned to a wrapper variable or passed to a wrapper parameter. The way to spot it is to read the required type on the left of the assignment: a wrapper type there indicates autoboxing.

  • A is not correct: Both sides are the primitive double, so no wrapper object is involved and no conversion is needed.
  • C is not correct: The declared type is String and the value is text, so this is neither a primitive nor a wrapper conversion.
  • D is not correct: The declared type is the primitive int receiving an int value, so nothing is boxed into an object.
Q4

What occurs in recordWeight(4.75) when the parameter type is Double?

AThe call fails because 4.75 is a primitive.
BJava autoboxes 4.75 into a Double.
CJava parses 4.75 as a string.
DJava unboxes the argument before the call.
Correct answer: B. A primitive double argument is passed to a Double parameter, so Java autoboxes it into the wrapper object.
Explanation

Autoboxing applies to method arguments the same way it applies to assignments. The required type at the call site is the parameter type, so a wrapper parameter receiving a primitive argument triggers the automatic conversion from primitive to wrapper object.

  • A is not correct: Java converts automatically here rather than rejecting the call, which is the whole purpose of autoboxing at a wrapper parameter.
  • C is not correct: Parsing takes the characters of a string, and 4.75 is a numeric value rather than text.
  • D is not correct: Unboxing runs the other direction, from a wrapper object to a primitive, and the parameter type here is the wrapper Double.
Q5

Assume that Double duration = 3.0; has executed. Which statement unboxes duration?

ADouble copy = duration;
BString text = "3.0";
CDouble other = 4.0;
Ddouble hours = duration;
Correct answer: D. The wrapper duration is assigned to a variable of primitive type double, which unboxes it.
Explanation

Unboxing converts a wrapper object to its primitive value, and it occurs when a wrapper is assigned to a primitive variable or passed to a primitive parameter. Read the required type of the target: a primitive type indicates unboxing.

  • A is not correct: The target type is the wrapper Double, so this simply copies a reference and no primitive value is produced.
  • B is not correct: This declares a String from text and never involves duration or any wrapper conversion.
  • C is not correct: This autoboxes a new double literal into a different wrapper and leaves duration untouched.
Q6

An Integer bonus is passed to a method whose parameter type is int. What occurs?

AJava unboxes bonus to an int.
BJava autoboxes bonus again.
CJava parses bonus as text.
DThe call cannot compile because the types differ.
Correct answer: A. The parameter type is the primitive int, so the Integer argument is unboxed to its primitive value.
Explanation

Method calls trigger the same automatic conversions as assignments. Because the required type at the parameter is a primitive, Java takes the value out of the wrapper object and passes that int.

  • B is not correct: The argument is already a wrapper object, and the parameter needs a primitive, so the conversion goes the opposite direction.
  • C is not correct: Parsing works on the characters in a string, and bonus holds a numeric wrapper value rather than text.
  • D is not correct: Java converts between a primitive and its wrapper automatically in method calls, so this pairing compiles.
Q7

A double is assigned to a Double variable. That wrapper is then assigned to a double variable. Which conversions occur?

AUnboxing, then autoboxing
BParsing, then autoboxing
CAutoboxing, then unboxing
DNo automatic conversions
Correct answer: C. Assigning a double into a Double variable autoboxes, and assigning that wrapper into a double variable then unboxes.
Explanation

Each assignment is judged on its own required type. The first target is a wrapper type, so the primitive is boxed into an object; the second target is a primitive type, so the value comes back out of the object. That is the same round trip the section shows for Double d = 4.2; followed by double k = d;.

  • A is not correct: This reverses the order: the first assignment has a wrapper target, and boxing must happen before there is any wrapper object to unbox.
  • B is not correct: Parsing applies to numeric strings, and no string appears in either of these two assignments.
  • D is not correct: Both assignments mix a primitive type with its wrapper type, which is precisely when Java converts automatically.
Q8

Which call returns the primitive double represented by "-6.5"?

AInteger.parseInt("-6.5")
BDouble.parseDouble("-6.5")
CDouble("-6.5")
DparseDouble("-6.5")
Correct answer: B. Double.parseDouble(String s) is the Quick Reference method that returns a signed primitive double, and the optional leading minus sign is part of the numeric text it accepts.
Explanation

The class name in front of the method tells you which primitive comes back: Integer.parseInt returns an int and Double.parseDouble returns a double. Because a leading minus sign belongs to signed numeric text, a string such as "-6.5" parses to a negative value.

  • A is not correct: This is the parsing method that returns an int, so it is the wrong choice for text meant to become a double.
  • C is not correct: The Quick Reference provides Double.parseDouble(String s); this form is not that named parsing method.
  • D is not correct: The method name is written without the Double class in front of it, so it is not the Double.parseDouble call the section provides.
Q9

Consider the following code segment. What is printed?

String text = "-18";
int change = Integer.parseInt(text);
System.out.println(change);
A18
B0
CNothing; a negative string cannot be parsed.
D-18
Correct answer: D. Integer.parseInt reads the leading minus sign as part of the signed numeric text, so change holds -18.
Explanation

Parsing interprets the characters of the string as a number, and an optional leading minus sign is part of signed numeric text. The section's own example parses "-16" and prints -16, which is the same pattern.

  • A is not correct: This drops the sign, but the minus sign is part of the numeric text and is carried into the returned int.
  • B is not correct: The characters do form a valid signed number, so the method returns that value rather than falling back to zero.
  • C is not correct: Signed numeric strings are exactly what these methods handle, as the section's "-16" example shows.
Q10

Consider the following code segment. What is the result of running this code segment?

String aText = "-2";
String bText = "4.5";
int a = Integer.parseInt(aText);
double b = Double.parseDouble(bText);
System.out.println(a + b);
AIt prints -6.5.
BIt prints 4.5.
CIt prints 2.5.
DThe code does not compile.
Correct answer: C. a parses to -2 and b parses to 4.5, so the sum printed is 2.5.
Explanation

Each parsing method returns a primitive of its own kind: Integer.parseInt returns an int and Double.parseDouble returns a double. The leading minus sign is part of the signed numeric text, so the first value is negative and the addition of the two numeric values gives 2.5.

  • A is not correct: This treats the two values as if both were negative, but only aText carries a leading minus sign.
  • B is not correct: This ignores a entirely; both parsed values take part in the addition.
  • D is not correct: Both calls are the parsing methods the Quick Reference provides, and each is given a string, so the segment compiles.
Q11

Consider the following code segment. What is printed?

Double rate = 1.5;
Double previous = rate;
rate = 2.0;
System.out.println(previous);
A2.0
B1.5
C3.5
DNothing is printed; the code does not compile.
Correct answer: B. Wrapper objects are immutable, so reassigning rate makes it refer to a different Double object while previous still refers to the one holding 1.5.
Explanation

After a wrapper object is created, its stored value cannot change. An assignment to a wrapper variable therefore changes only which object that one variable refers to. This is the same behavior the section demonstrates with Integer score and Integer saved, where saved keeps printing the original value.

  • A is not correct: This assumes the reassignment reached inside the shared object, but the stored value of an existing wrapper object cannot change.
  • C is not correct: Nothing in the segment adds the two values together; each assignment simply points a variable at a wrapper object.
  • D is not correct: Assigning a double literal to a Double variable is ordinary autoboxing, so the segment compiles and prints.