AP Computer Science A • Unit 1.9

Method Signatures

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

Q1D
Q2A
Q3C
Q4B
Q5C
Q6B
Q7A
Q8A
Q9D
Q10C
Q11D
Q12B
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

Consider the following method header. Which of the following is the method signature?

public static int combine(String s, int n)
Acombine
Bint combine(String, int)
Ccombine(String s, int n)
Dcombine(String, int)
Correct answer: D. A signature is the method name together with the ordered list of parameter types, which gives combine(String, int).
Explanation

A method header carries more than the signature. The signature keeps only the method name and the parameter types in order, and it drops the access modifier, static, the return type, and the parameter names.

  • A is not correct: the method name alone leaves out the ordered list of parameter types, which is the other half of a signature.
  • B is not correct: the return type int belongs to the method header, and the return type is never part of a method signature.
  • C is not correct: parameter names such as s and n are not part of a signature, which lists only the parameter types.
Q2

Consider the following documented method. Which call correctly stores the method's result?

/** Returns the larger of two values. */
public static int larger(int a, int b)
Aint result = larger(7, 4);
Blarger();
Cint result = larger("7", 4);
Dlarger(7);
Correct answer: A. The method returns an int, and this call supplies two int arguments that match its parameter list.
Explanation

A non-void method returns a value that the caller can store, print, or use in a larger expression. The call must also match the parameter list, so two int arguments are required, in the order the header declares them.

  • B is not correct: larger declares two parameters, so a call with no arguments does not supply the required number of arguments.
  • C is not correct: the first argument "7" is a String, which is not a type compatible with an int parameter.
  • D is not correct: the call supplies only one argument while the method expects two, so the number of arguments does not match.
Q3

Consider the following method and call. Why does the call not compile?

public static void display(int v) {
    System.out.println(v);
}
int result = display(5);
AThe argument has the wrong type.
BThe method must have two parameters.
CA void call produces no value to store.
DA method call cannot appear in an assignment.
Correct answer: C. display is declared void, so it sends nothing back and there is no value for result to store.
Explanation

A void method performs an action but returns no value, so its call normally stands alone as a statement. Trying to store, print, or use its result does not compile, because there is no result to use.

  • A is not correct: the argument 5 is an int and the parameter v is an int, so the type is compatible.
  • B is not correct: the number of parameters is not the problem; the method declares one parameter and the call supplies one argument.
  • D is not correct: a method call may appear in an assignment whenever the method returns a value, and a non-void result can be stored.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

public static void first() {
    System.out.print("A");
}
System.out.print("B");
first();
System.out.print("C");
AABC
BBAC
CBCA
DACB
Correct answer: B. The call to first runs between the two print statements, so B is printed, then A, then C.
Explanation

A method call temporarily interrupts sequential execution. Java transfers control to the called method, runs the statements inside it, and then returns control to the statement immediately after the call. A method does not run just because it exists.

  • A is not correct: the method body does not run where it is written; it runs only at the point where the call appears.
  • C is not correct: control returns to the statement right after the call, so the printing inside first happens before C is printed.
  • D is not correct: the call to first comes after the statement that prints B, so B is the first character printed.
Q5

Consider the following overloaded method headers. Which call uses show(String, int)?

public static void show(int n)
public static void show(String text)
public static void show(String text, int n)
Ashow(8);
Bshow("Eight");
Cshow("Score", 8);
Dshow(8, "Score");
Correct answer: C. Only this call supplies a String followed by an int, which is the parameter list that signature declares.
Explanation

Overloaded methods share a name and differ in their signatures. Java selects one by matching the method name first, then comparing the number, order, and compatible types of the arguments against each available parameter list.

  • A is not correct: a single int argument matches the overload that declares one int parameter, not the two-parameter version beginning with a String.
  • B is not correct: a single String argument matches the overload with one String parameter, so only one of the two required arguments is supplied.
  • D is not correct: the two types appear in the reverse order, and order is part of the match, so this call does not use show(String, int).
Q6

Consider the following code segment. What is printed as a result of executing the code segment?

public static void change(int n) {
    n += 5;
}
int total = 9;
change(total);
System.out.println(total);
A5
B9
C14
DThe code does not compile.
Correct answer: B. The parameter receives a copy of total, so changing the parameter leaves total storing 9.
Explanation

Java uses call by value. A parameter receives a copy of its argument, and for a primitive that copy is the value itself, so a method cannot change the caller's primitive variable.

  • A is not correct: 5 is the amount added inside the method, not the value that the caller's variable holds afterwards.
  • C is not correct: the addition happens on the copied parameter n, so the caller's variable total is never updated to that sum.
  • D is not correct: the call supplies one int argument for one int parameter, so it matches the parameter list and compiles.
Q7

What is the main benefit of procedural abstraction?

AIt lets callers use a method by knowing what it does without knowing its internal steps.
BIt requires every method to expose its implementation.
CIt makes all methods return the same type.
DIt prevents methods from receiving arguments.
Correct answer: A. Procedural abstraction means a method can be used through its documented behavior, without knowing the steps inside its body.
Explanation

A method is a named block of code that runs only when it is called. Methods divide a large program into smaller, reusable actions and calculations, and the caller relies on the documentation and the method header instead of recreating the work.

  • B is not correct: procedural abstraction works the other way around, letting a caller ignore the internal steps rather than requiring them to be exposed.
  • C is not correct: return types are not made uniform; a void method returns nothing while a non-void method returns a value of its declared type.
  • D is not correct: methods still receive arguments, and the caller sends values in through the parameter list while ignoring how they are used.
Q8

Documentation says that displayScore “displays a label followed by a score.” Its signature is displayScore(String, int). Which call matches the documentation and signature?

AdisplayScore("Final", 95);
BdisplayScore(95, "Final");
CdisplayScore("Final");
DdisplayScore("Final", 95, 100);
Correct answer: A. The documentation identifies the method, and the signature shows a String label first and an int score second.
Explanation

Documentation and a signature answer different questions. Documentation identifies the required behavior, and the signature then confirms the method name and the number, order, and types of the arguments that the call must supply.

  • B is not correct: arguments are matched by position, so 95 would reach the String parameter and the types would not be compatible.
  • C is not correct: the signature declares two parameter types, so a call with one argument does not supply the required number of arguments.
  • D is not correct: three arguments are too many for a signature that declares one String parameter and one int parameter.
Q9

Which of the following does a method signature contain?

AThe return type
BThe access modifier
CThe parameter names
DThe method name and the ordered list of parameter types
Correct answer: D. A method signature consists of the method name and the ordered list of parameter types, and nothing else.
Explanation

The remaining parts of a header are declaration details. The access modifier, the keyword static, the return type, and the parameter names all belong to the header, and none of them distinguishes one method from another.

  • A is not correct: the return type belongs to the method header, which is why two methods cannot be distinguished only by return type.
  • B is not correct: the access modifier is part of the header, like static, and it never appears in the method signature.
  • C is not correct: parameter names are not part of a signature, which is why changing only a parameter name leaves the signature the same.
Q10

When Java matches a call's arguments to the parameter list, which of the following is NOT checked?

AThe number of arguments
BThe order of the arguments
CThe meaning of the argument names
DThe type of each argument
Correct answer: C. Java checks the number, order, and types of the arguments, but never what the argument names appear to mean.
Explanation

Arguments are matched to parameters by position. Reversing two arguments of the same type still compiles and prints the wrong thing, because Java has no way to know that the programmer meant them the other way around.

  • A is not correct: the number of arguments is checked, so a call with too few or too many arguments does not match the parameter list.
  • B is not correct: the order is checked, which is why two parameter type lists in a different order are two different signatures.
  • D is not correct: each argument type is checked for compatibility, which is how an int argument is allowed for a double parameter.
Q11

Consider the following method and call. Which statement about the call is true?

public static int pointsLeft(int g, int e) {
    return g - e;
}
int needed = pointsLeft(72, 100);
AIt does not compile because the arguments are in the wrong order.
B72 is copied into e, and 100 is copied into g.
CJava matches each argument to a parameter by the meaning of its name.
DIt compiles; 72 is copied into g, and 100 is copied into e.
Correct answer: D. Two int arguments match the parameter list, and each value binds by position rather than by the meaning of its name.
Explanation

A call must match the parameter list in number, order, and compatible types, and this call does. The first argument is copied into the first parameter and the second into the second, even when the names suggest the opposite intent.

  • A is not correct: both arguments are int values and the count is right, so the call matches the signature and does compile.
  • B is not correct: the binding runs in the other direction, because the first argument goes to the first parameter and the second to the second.
  • C is not correct: Java never uses the meaning of a name; it copies each argument into the parameter that occupies the same position.
Q12

Two methods have the same name. Which difference alone makes them overloaded?

ADifferent return types
BDifferent parameter lists
CDifferent parameter names
DDifferent access modifiers
Correct answer: B. Overloaded methods share a name and differ in their signatures, and a signature is built from the ordered parameter types.
Explanation

A signature is the method name and the ordered list of parameter types, so overloaded methods must differ in that list, by number, order, or type. Everything else in the header leaves the signature unchanged.

  • A is not correct: the return type is not part of a signature, so two such methods cannot both be used as overloaded methods in one class.
  • C is not correct: parameter names do not distinguish overloaded methods, so two headers of that kind still have one and the same signature.
  • D is not correct: access modifiers belong to the header rather than the signature, so changing one does not produce a different method.