AP Computer Science A • Unit 3.6

Methods: Passing and Returning References of an Object

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

Q1A
Q2B
Q3B
Q4C
Q5A
Q6B
Q7C
Q8A
Q9B
Q10C
Q11B
Q12A
Q13B
Q14C
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

A method receives a WellnessProfile object as a parameter and calls setRiskLevel(5) on that parameter. What can happen after the method finishes?

AThe caller may observe that the same object's risk level changed.
BThe caller's variable is replaced with a new variable name.
CThe object automatically becomes a primitive value.
DThe class definition is copied into the method.
Correct answer: A. The parameter refers to the caller's object, so a change made through it is visible afterward.
Explanation

Calling a mutator on a parameter reaches the object itself, not a copy of it. This is the central difference between passing an object and passing a primitive, and the rest of this topic follows from it.

  • B is not correct: variable names are fixed when they are declared and are never replaced at run time.
  • C is not correct: passing an object to a method does not change what kind of value it is.
  • D is not correct: a class is defined once, and a call does not copy the definition anywhere.
Q2

Which statement best explains Java's behavior when an object reference is passed to a method?

AJava passes the original variable itself.
BJava copies the reference value, so the parameter may refer to the same object.
CJava copies every instance variable into a new object automatically.
DJava prevents methods from changing object state.
Correct answer: B. What is copied is the reference, so two variables end up pointing at one object.
Explanation

This one sentence explains both halves of the topic. Because the reference is copied, changing the object works; because only the reference is copied, reassigning the parameter does not reach the caller.

  • A is not correct: the caller's variable is never handed over, which is why reassigning the parameter has no effect outside the method.
  • C is not correct: no copy of the object is made; a separate object appears only when the code explicitly creates one.
  • D is not correct: methods change object state routinely, and the first question is an example.
Q3

A method executes profile = new WellnessProfile(0); where profile is an object parameter. What is true after the method returns?

AThe caller's variable now refers to the new object.
BThe caller's variable still refers to its original object.
CThe original object automatically becomes null.
DEvery alias is redirected to the new object.
Correct answer: B. Reassigning the parameter changes only the method's own copy of the reference.
Explanation

The parameter and the caller's variable start out pointing at the same object, but they remain separate variables. Pointing one of them somewhere else leaves the other exactly where it was, and the parameter disappears when the method ends.

  • A is not correct: the caller's variable was never handed to the method, so nothing inside it can redirect that variable.
  • C is not correct: the original object is still referred to by the caller and is unaffected.
  • D is not correct: each reference variable is assigned individually; changing one does not move the others.
Q4

A specification does not ask a method to change its mutable object parameter. Which choice follows good programming practice?

AChange the object anyway so the method has a visible effect.
BReassign the caller's variable to a different object.
CAvoid mutating the parameter object.
DMake all of the object's private data public.
Correct answer: C. A method should do what its specification asks and no more, especially when a change would be visible to the caller.
Explanation

Because a change made through a parameter reaches the caller's object, an unrequested change is a surprise the caller has no way to anticipate. Restraint here is what makes a method safe to call.

  • A is not correct: a visible effect the specification never asked for is precisely the problem, not a benefit.
  • B is not correct: a method cannot reassign the caller's variable, and attempting it does not follow the specification either.
  • D is not correct: exposing private data removes the class's control over its own state and has nothing to do with the request.
Q5

Inside a class named StudentNote, a method receives another StudentNote parameter named other. Which access is allowed?

AAccessing other's private instance variables, because other has the same class type
BAccessing private instance variables of any object from any class
CAccessing other's data only through public methods, even inside StudentNote
DAccessing private instance variables only on the current object
Correct answer: A. Private access is granted to the class, so code inside StudentNote can reach the private members of any StudentNote.
Explanation

The boundary that private draws is around the class, not around an individual object. This is what makes it possible to write a method that compares two objects of the same type by looking at their stored values directly.

  • B is not correct: this removes the boundary entirely, which would leave private access meaning nothing.
  • C is not correct: going through public methods is required from outside the class, not from within it.
  • D is not correct: this draws the boundary around one object, which is narrower than the rule actually is.
Q6

A method returns a WellnessProfile variable. What value is copied back to the caller?

AA complete copy of the object
BThe reference value
CA separate copy of every instance variable
DThe method's parameter list
Correct answer: B. Returning an object hands back the reference, exactly as passing one does.
Explanation

The same rule governs both directions of travel. A returned reference points at the object the method was working with, so the caller can now reach that very object.

  • A is not correct: a separate object is produced only when the code explicitly creates one.
  • C is not correct: instance variables are not copied out one by one when a reference is returned.
  • D is not correct: the parameter list describes what the method receives, not what it gives back.
Q7

If two variables refer to the same object, what is this situation called?

ACasting
BOverloading
CAliasing
DShort-circuiting
Correct answer: C. Two references to one object are aliases of each other.
Explanation

Aliasing is what makes a change made through one variable visible through the other. Passing an object to a method creates an alias, and so does returning one.

  • A is not correct: casting converts a value from one type to another.
  • B is not correct: overloading is having several methods share a name with different parameter lists.
  • D is not correct: short-circuiting describes how some boolean expressions stop evaluating early.
Q8

A method returns a private mutable object directly. What can the caller do through the returned reference?

AMutate that same object if public mutator methods are available
BReassign the method's private instance variable directly
CTurn the object into an independent primitive copy
DAccess every private instance variable directly
Correct answer: A. The caller now holds a reference to the very object the class was keeping private, so any public mutator on it is available.
Explanation

Declaring the instance variable private stops outside code from replacing the reference, but returning that reference hands over the object anyway. This is why a class that means to protect a mutable field returns a copy rather than the field itself.

  • B is not correct: the private instance variable itself is still out of reach; only the object it refers to has been exposed.
  • C is not correct: receiving a reference does not convert an object into a primitive.
  • D is not correct: the returned object's own private members remain private to its class.
Q9

The following method and code segment appear in the same class. What is printed?

public void award(Badge b) {
    b.setPoints(b.getPoints() + 2);
}

Badge b = new Badge(6);
award(b);
System.out.println(b.getPoints());
A6
B8
C0
DNothing is printed; the code does not compile.
Correct answer: B. The mutator is called on the caller's own object, so the change is still there after the method returns.
Explanation

The parameter and the caller's variable are aliases, so setPoints reaches the one object both of them refer to. Compare this trace with the next question, where the only difference is what the method does with its parameter.

  • A is not correct: 6 is the starting value, which the mutator replaces.
  • C is not correct: nothing in the segment sets the value to zero.
  • D is not correct: passing an object to a matching parameter type is valid, so the code compiles.
Q10

The following method and code segment appear in the same class. What is printed?

public void resetLocal(Badge badge) {
    badge = new Badge(0);
}

Badge b = new Badge(6);
resetLocal(b);
System.out.println(b.getPoints());
A0
Bnull
C6
DNothing is printed; the code does not compile.
Correct answer: C. The method points its own parameter at a new object and never touches the caller's object.
Explanation

This is the contrast with the previous question. Calling a mutator reaches the shared object; assigning to the parameter only redirects the method's local copy of the reference. The newly created Badge is discarded when the method ends.

  • A is not correct: 0 is the value of the new object, which the caller never sees.
  • B is not correct: the caller's variable still refers to a valid object, so it is not null.
  • D is not correct: reassigning a parameter is legal, so the code compiles and runs.
Q11

The following methods appear inside the Locker class. Why is the call other.code() allowed?

private int code;
private int code() {
    return code;
}

public boolean same(Locker other) {
    return code() == other.code();
}
APrivate methods may be called from every class.
Bother has the same type as the enclosing class.
CThe method returns a boolean.
Dcode is automatically static.
Correct answer: B. The call sits inside Locker, and other is a Locker, so its private members are reachable here.
Explanation

This applies the earlier access rule to a private method rather than a private variable, and the rule is the same: private access belongs to the class. Written in any other class, this same line would be rejected.

  • A is not correct: a private method is reachable only from inside its own class.
  • C is not correct: the return type of same has no bearing on what it is allowed to call.
  • D is not correct: nothing here is static, and being static would not grant access to private members anyway.
Q12

The following method and code segment appear in the same class. What is printed?

public Pass pick(Pass a, Pass b) {
    if (a.getDay() >= b.getDay()) {
        return a;
    }
    return b;
}

Pass p1 = new Pass(2);
Pass p2 = new Pass(5);
Pass chosen = pick(p1, p2);
chosen.setDay(9);
System.out.println(p2.getDay());
A9
B5
C2
D0
Correct answer: A. The method returns the second object, so chosen and p2 are aliases and the mutator changes the object both of them name.
Explanation

Two steps have to be traced in order. First the comparison decides which reference comes back, and here the first object's day is not at least the second's, so the method returns the second. Then chosen holds a reference to that object, and changing it through chosen is the same as changing it through p2.

  • B is not correct: 5 is the starting value, which the mutator replaces because the alias reaches this object.
  • C is not correct: 2 belongs to the first object, which the method did not return.
  • D is not correct: both objects were given a starting value, so neither holds zero.
Q13

Which assignment can make two variables refer to the same object?

Ab == a;
Bb = a;
Cb.equals(a);
Dnew = a;
Correct answer: B. Assigning one reference variable to another copies the reference, so both then point at the same object.
Explanation

This is how an alias is created in a single statement. The three wrong options are all comparisons or invalid code; none of them changes what either variable refers to.

  • A is not correct: == compares two references rather than assigning one to the other.
  • C is not correct: equals compares the objects and returns a result; it changes neither variable.
  • D is not correct: new is a keyword and cannot be used as a variable name.
Q14

Code in a different class receives a WellnessProfile parameter whose riskLevel variable is private. How should it read the risk level?

AUse profile.riskLevel.
BChange riskLevel from private to local.
CCall a public method such as profile.getRiskLevel().
DRead the value only by reassigning the parameter.
Correct answer: C. From outside the class, a private value is reachable only through a public method the class provides.
Explanation

This is the other side of the access rule tested earlier. Inside WellnessProfile the private variable could be named directly; from a different class the accessor is the only way in.

  • A is not correct: naming a private variable from another class is rejected at compile time.
  • B is not correct: a local variable exists only inside a method, so this would not give the object a value to report at all.
  • D is not correct: reassigning the parameter points it at a different object instead of reading anything.