AP Computer Science A • Unit 3.5

Methods: How to Write Them

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

Q1A
Q2D
Q3D
Q4C
Q5B
Q6C
Q7A
Q8C
Q9C
Q10A
Q11D
Q12B
Q13C
Q14A
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 part of a method header tells what type of value the method gives back?

AThe return type
BThe method name
CThe access modifier
DThe parameter list
Correct answer: A. The return type is the part of the header that states what kind of value comes back to the caller.
Explanation

A method header carries four pieces of information, and each answers a different question: who may call it, what it gives back, what it is called, and what it needs. The return type answers the second of those.

  • B is not correct: the name identifies the method so it can be called; it says nothing about the type of the result.
  • C is not correct: the access modifier states where the method may be called from.
  • D is not correct: the parameter list describes the values the method receives, which is the opposite direction of travel.
Q2

What must a non-void method normally do?

AUse void in its header
BReach the end without returning a value
COnly print the result
DReturn a value compatible with its return type
Correct answer: D. A declared return type is a promise, and the method keeps it by returning a value that fits that type.
Explanation

The header and the body have to agree. Once a method declares a return type other than void, the compiler requires that every path through the body ends by returning a value the caller can use as that type.

  • A is not correct: a header that uses void describes a method that returns nothing, which is the other kind.
  • B is not correct: reaching the end without returning a value is exactly the mistake the return type rules out.
  • C is not correct: printing displays a value to the screen; it does not hand a value back to the caller.
Q3

Which method header has one int parameter and returns a boolean?

Apublic int isPassing(boolean score)
Bpublic void isPassing(int score)
Cpublic boolean int isPassing(score)
Dpublic boolean isPassing(int score)
Correct answer: D. boolean sits before the name as the return type, and int score is the single parameter.
Explanation

Reading a header from left to right settles this quickly: the type just before the name is what the method returns, and each entry inside the parentheses is a type followed by a parameter name.

  • A is not correct: it reverses the two types, returning an int and taking a boolean.
  • B is not correct: the parameter is right, but void means the method returns nothing.
  • C is not correct: two types appear before the name, and the parameter is missing its type.
Q4

Which statement correctly compares accessors and mutators?

AAn accessor changes object state; a mutator reports it.
BBoth must return the same type of value.
CAn accessor reports object state; a mutator changes it.
DBoth must be declared void.
Correct answer: C. An accessor reports what the object holds; a mutator changes what it holds.
Explanation

The two names describe the direction of the work. Because an accessor has to hand a value back, it is non-void; because a mutator's job is the change it makes, it often returns nothing.

  • A is not correct: it swaps the two roles.
  • B is not correct: the two kinds of method exist for different purposes, so there is no reason for their return types to match.
  • D is not correct: an accessor cannot be void, because a method that returns nothing has no way to report anything.
Q5

Which statement is valid inside a method declared public int getScore()?

Areturn;
Breturn score;
Creturn "score";
Dpublic int score;
Correct answer: B. The header promises an int, so the return statement must supply a value of that type.
Explanation

Every return statement is checked against the declared return type. Once the header names int, a bare return and a String literal are both rejected for the same underlying reason: neither one is an int.

  • A is not correct: return; supplies no value, which suits a void method rather than this one.
  • C is not correct: the quotation marks make this a String, which does not match the declared int.
  • D is not correct: this is a variable declaration, not a statement that returns anything.
Q6

Why are parameters useful?

AThey determine a method's return type.
BThey allow calls to ignore required argument types.
CThey allow a method to receive values from the caller.
DThey make instance variables publicly accessible.
Correct answer: C. A parameter is how a value travels from the caller into the method.
Explanation

Parameters and return values carry information in opposite directions: a parameter brings a value in, and a return value sends one back out. Because of parameters, one method body can do its work on many different values.

  • A is not correct: the return type is declared separately in the header and does not follow from the parameters.
  • B is not correct: a parameter's declared type is exactly what the argument in each call has to match.
  • D is not correct: the visibility of an instance variable is set by its own access designation, not by any parameter.
Q7

What happens when a return statement executes in a method?

AThe method ends and control returns to the caller.
BThe method restarts with new parameter values.
COnly the next sequential statement is skipped.
DEvery method in the class ends.
Correct answer: A. A return ends the method immediately and hands control back to the line that called it.
Explanation

Nothing after a return in that method runs. This is why an early return inside an if can stand in for an else: the code below it is reached only when the condition was false.

  • B is not correct: a method runs again only when it is called again, and return is not a call.
  • C is not correct: return leaves the method entirely rather than skipping one statement.
  • D is not correct: only the method containing the return ends; any method waiting for its result continues.
Q8

Which mistake would cause a compile-time error?

AA void method changes an instance variable.
BA method receives a primitive parameter.
CA non-void method has a path with no return value.
DA non-void method returns a compatible value.
Correct answer: C. If any path through the body can reach the end without returning, the promise in the header is broken and the code does not compile.
Explanation

The compiler examines every route through the method, not just the obvious one. A branch that is easy to overlook, such as the case where an if condition is false, is the usual source of this error.

  • A is not correct: changing an instance variable is what a mutator does, and a void return type suits it.
  • B is not correct: receiving a primitive parameter is ordinary and correct.
  • D is not correct: this describes a method that keeps its promise, which is what the rule requires.
Q9

Which method call correctly matches public int add(int a, int b)?

Aadd();
Badd("3", "4");
Cadd(3, 4);
Dadd(3);
Correct answer: C. The header declares two int parameters, so the call supplies two int arguments.
Explanation

A call has to match the header in both number and type. Checking those two things separately catches every wrong option here.

  • A is not correct: it supplies no arguments for two declared parameters.
  • B is not correct: the count is right but the quotation marks make these String values rather than int values.
  • D is not correct: the type is right but only one of the two required arguments is supplied.
Q10

Which statement correctly uses the value returned by getScore()?

Aint score = getScore();
Bpublic getScore();
CgetScore();
Dreturn;
Correct answer: A. The returned value is stored in a variable, so the program can still use it afterward.
Explanation

A returned value has to be captured or used at the moment it arrives. Assigning it to a variable is the most common way to keep it; passing it straight into another expression works too. What does not work is letting it go unused.

  • B is not correct: an access designation belongs to a declaration, not to a call.
  • C is not correct: the call runs but the returned value is discarded, so nothing uses it.
  • D is not correct: this returns from the current method instead of using the value getScore() produced.
Q11

For the object referenced by c, count is initially 4. What is printed?

public void add(int amount) {
    count += amount;
}

public int getCount() {
    return count;
}

c.add(3);
c.add(2);
System.out.println(c.getCount());
A4
B5
C7
D9
Correct answer: D. Both calls change the same object's count, so the two increases accumulate on top of the starting value.
Explanation

This pairs a mutator with an accessor. add changes the instance variable and returns nothing; getCount changes nothing and reports the current value. Because the instance variable belongs to the object rather than to a single call, the effect of the first call is still present during the second.

  • A is not correct: 4 is the starting value, which would be printed only if neither call had changed it.
  • B is not correct: 5 does not correspond to the starting value combined with either increase.
  • C is not correct: 7 is the value after the first call only, which treats the second call as having no effect.
Q12

The following methods appear in the same class. What is printed when test() is called?

public void adjust(int amount) {
    amount += 5;
}

public void test() {
    int score = 7;
    adjust(score);
    System.out.println(score);
}
A5
B7
C12
DThe code does not compile.
Correct answer: B. amount receives a copy of the value, so changing it inside adjust leaves score untouched.
Explanation

A primitive argument is copied into the parameter when the call is made. From that point the parameter is a separate variable that happens to start with the same value, so any change to it ends when the method ends. A method that needs to hand a new value back has to return it.

  • A is not correct: 5 is the amount added inside adjust, not the value of score.
  • C is not correct: 12 is the value amount holds inside adjust, which is discarded when that method ends.
  • D is not correct: the segment compiles; passing an int variable to an int parameter is valid.
Q13

Examine the following method. What value is returned by update(7)?

public int update(int x) {
    if (x > 5) {
        return x + 2;
    }
    x = x * 3;
    return x;
}
A7
B21
C9
D27
Correct answer: C. 7 > 5 is true, so the method returns immediately and the statements below it never run.
Explanation

This is the early-return shape. Because a return ends the method, the lines after the if act as the alternative branch without any else being written. Tracing such a method means stopping at the first return that executes.

  • A is not correct: 7 is the argument itself, unchanged, but the method adds to it before returning.
  • B is not correct: 21 comes from the multiplication below the if, which is never reached for this argument.
  • D is not correct: 27 would require both branches to run, but only one of them can.
Q14

The following method is available. What is printed by the statement shown?

public int combine(int a, int b) {
    return a * 10 + b;
}

System.out.println(combine(3, 7));
A37
B73
C10
DThe code does not compile.
Correct answer: A. a receives 3 and b receives 7, so the expression produces 37.
Explanation

Arguments are matched to parameters by position, not by value. The first argument fills the first parameter, and multiplication is evaluated before addition, so the first argument is the one that ends up in the tens place.

  • B is not correct: 73 would result from matching the arguments to the parameters in the wrong order.
  • C is not correct: 10 is the literal in the expression rather than the value the expression produces.
  • D is not correct: the call matches the header in number and type, so it compiles.