AP Computer Science A • Unit 1.10

Calling Class Methods

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

Q1A
Q2D
Q3C
Q4C
Q5B
Q6B
Q7A
Q8A
Q9C
Q10D
Q11B
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 code segment. Assume the statements after the method appear in another method in the same class. What is printed as a result of executing the code segment?

public static int addOne(int n) {
    return n + 1;
}
int points = 8;
addOne(points);
System.out.println(points);
A8
B9
C0
DThe code does not compile.
Correct answer: A. Passing a primitive copies its value, so the call cannot change points, which still prints as 8.
Explanation

For a primitive argument, changing the method's copied parameter does not change the caller's variable. The call also stands alone as a statement, so the value it returns is discarded and the variable keeps the value it already had.

  • B is not correct: the method does return 9, but that returned value is used as a standalone statement and discarded, so points is unchanged.
  • C is not correct: nothing in the code segment assigns 0 to points, which is given the value 8 before the call and never reassigned.
  • D is not correct: the call matches the parameter list of addOne, and a non-void call may legally be used as a standalone statement.
Q2

Consider the following methods. What is printed as a result of executing main?

public static int addOne(int n) {
    return n + 1;
}
public static void main(String[] args) {
    int x = 4;
    int y = addOne(x);
    System.out.println(x + " " + y);
}
A4 4
B5 4
C5 5
D4 5
Correct answer: D. The parameter is a copy, so x stays 4 while y receives the returned value 5.
Explanation

For a primitive argument, changing the method's copied parameter does not change the caller's variable. The returned value is a separate result, and the assignment stores it in y, leaving x with the value it was given.

  • A is not correct: y is assigned the value that addOne returns, so it does not end up holding the same value as x.
  • B is not correct: the two values are reversed here; the call leaves x unchanged and y holds the value the method returned.
  • C is not correct: this would require the method to change x as well, but the argument is copied into the parameter and the caller's variable is left alone.
Q3

Consider the following methods. What is printed as a result of executing main?

public static int tri(int x) {
    return 3 * x;
}
public static int add(int a, int b) {
    return a + b;
}
public static void main(String[] args) {
    System.out.println(add(tri(2), tri(3)));
}
A5
B9
C15
D18
Correct answer: C. Java evaluates the inner calls first, so add receives 6 and 9 and returns 15.
Explanation

A returned value can become the argument to another call. Java evaluates the inner calls before the outer one, and the two values that come back are passed to add, whose own returned value is printed.

  • A is not correct: this adds 2 and 3 directly, but each value is passed to tri before add receives anything.
  • B is not correct: this is only what one of the inner calls returns, and add still has to combine both returned values.
  • D is not correct: this would require the inner calls to return larger values; tri returns 6 and 9, which add to 15.
Q4

Consider the following methods. What is printed as a result of executing main?

public static int sq(int x) {
    return x * x;
}
public static void main(String[] args) {
    int a = 3;
    int b = 4;
    System.out.println(sq(a) + sq(b));
}
A7
B14
C25
D49
Correct answer: C. Each call is evaluated before the addition, so the two returned values are added and 25 is printed.
Explanation

Java evaluates the inner calls first and replaces each one with the value it returns. Only then does the addition run, so the sum is formed from the two returned values rather than from the arguments supplied to the calls.

  • A is not correct: this adds 3 and 4 without calling sq, but each value is passed to the method before the addition.
  • B is not correct: this doubles each argument instead of multiplying it by itself, which is not what sq returns for the value it receives.
  • D is not correct: this adds the two values first and squares the sum, but each call is evaluated separately and only the returned values are added.
Q5

Consider this method header. The call appears in the same class. Which of the following is a valid call to the method?

public static double average(int t, int n)
Aaverage(10)
Baverage(10, 2)
Caverage("10", 2)
Daverage(10.5, 2.0)
Correct answer: B. The header declares two int parameters, so a valid call supplies two whole-number arguments in that order.
Explanation

The method call must provide arguments that match the parameter list. Both parameters are int, and the return type double describes the value that comes back rather than the values the call has to supply.

  • A is not correct: this supplies one argument for two parameters, so the number of arguments does not line up with the header.
  • C is not correct: "10" is text rather than an int, so the type of the first argument does not match the first parameter.
  • D is not correct: 10.5 and 2.0 are not int values, so the types of the arguments do not match the parameters.
Q6

Consider the following code segment. Assume the statements after the method appear in another method in the same class. What is printed as a result of executing the code segment?

public static int square(int x) {
    return x * x;
}
int result = square(6) / 2;
System.out.println(result);
A9
B18
C36
D3
Correct answer: B. Java first gets 36 back from square(6), and only then divides by 2, giving 18.
Explanation

A returned value can be used inside a larger expression. The call is evaluated first, and the value that comes back takes its place in the expression, so the division works on the returned result rather than on the argument.

  • A is not correct: this divides the argument 6 by 2 before the squaring, but Java evaluates the call first and divides the value it returns.
  • C is not correct: 36 is the value returned by square(6), and the statement then divides that returned value by 2 before storing it.
  • D is not correct: this is 6 divided by 2, which ignores the multiplication the method performs before the division takes place.
Q7

Consider this method header. The call appears in the same class. Which of the following is a valid use of the method?

public static void repeat(String s)
Arepeat("AP");
BString out = repeat("AP");
Cint out = repeat("AP");
Drepeat("A") + "B";
Correct answer: A. Because repeat is a void method, the call is used as a statement rather than as a value.
Explanation

A void method performs an action but does not return a value to store. The call still has to supply arguments that match the parameter list, and here one String argument lines up with the single parameter.

  • B is not correct: a void call produces no value, so there is nothing for the String variable out to receive from it.
  • C is not correct: the method returns no value at all, and even a returned value would have to be used in a type-compatible context.
  • D is not correct: a void call cannot be used in an expression, because it produces no value to combine with another operand.
Q8

Assume Tools.addBonus(int s, int b) returns an int. Which call matches the method header?

ATools.addBonus(10, 3)
BTools.addBonus(10)
CTools.addBonus("10", 3)
DTools.addBonus(10, 3, 1)
Correct answer: A. The header declares two int parameters, and this call supplies two int arguments in the same order.
Explanation

The method call must provide arguments that match the parameter list. The number, order, and types of the arguments have to line up with the parameters, and the call begins with the class name Tools because the method is defined in another class.

  • B is not correct: this supplies one argument for a header that declares two parameters, so the number of arguments does not line up.
  • C is not correct: "10" is text rather than an int, so the type of the first argument does not match the first parameter.
  • D is not correct: this supplies three arguments for two parameters, so the number of arguments again fails to match the parameter list.
Q9

Assume Num.square(int n) returns an int. Which change corrects this statement?

int area = Num.square("6");
ARemove the argument.
BAdd a second argument.
CReplace "6" with 6.
DDeclare area as a String.
Correct answer: C. The parameter is an int, so Num.square("6") passes the wrong type and the argument must become 6.
Explanation

The method call must provide arguments that match the parameter list in number, order, and type. Num.square declares one int parameter, so replacing "6" with 6 fixes the call, and the returned int then fits area.

  • A is not correct: removing the argument leaves the single int parameter with nothing to receive, so the number of arguments would then be wrong.
  • B is not correct: adding a second argument gives the call more arguments than Num.square has parameters, and the type of "6" is still wrong.
  • D is not correct: the method returns an int, so declaring area as a String adds a second mismatch and leaves the argument wrong.
Q10

Suppose the static method twice is defined in a class named Calc. Which call correctly uses the class name?

Atwice.Calc(8)
BCalc.twice
Cnew Calc.twice(8)
DCalc.twice(8)
Correct answer: D. The calling pattern is ClassName.methodName(arguments), so the class name comes first and the argument follows in parentheses.
Explanation

A class method belongs to the class itself, so it can be called without first creating an object of that class. The class name comes before the dot, the method name after it, and the parentheses hold the arguments.

  • A is not correct: the two names are reversed here, putting the method name where the class name belongs and the class name after the dot.
  • B is not correct: the parentheses and the argument are missing, so this names the method rather than calling it with an argument.
  • C is not correct: new creates an object, but a class method belongs to the class and needs no object before it is called.
Q11

Assume Tools.doubleIt(int n) returns an int. Which statement correctly stores the returned value?

ATools.doubleIt(7);
Bint value = Tools.doubleIt(7);
Cvoid value = Tools.doubleIt(7);
DString value = Tools.doubleIt(7);
Correct answer: B. The method returns an int, so its returned value can be stored in a variable declared as int.
Explanation

A non-void method returns a value that can be stored, printed, used in an expression, or passed to another method. A returned value must be used in a type-compatible context, and int matches the type the method returns.

  • A is not correct: a non-void call used as a standalone statement discards its returned value, so nothing is kept for later use.
  • C is not correct: void describes a method that returns no value, so it is not a type that a variable can be declared with.
  • D is not correct: a returned value must be used in a type-compatible context, and an int does not fit a String variable.
Q12

Which statement about a static method defined in the same class is correct?

AIt must always be called using an object.
BThe class name may be omitted when the method is called.
CIt cannot return a value.
DIt cannot take parameters.
Correct answer: B. When a static method is called from within the class that defines it, the class name may be omitted.
Explanation

Calls such as Math.sqrt(81) include the class name because they are made from outside that class. Inside Helper, the calls to printLine() use the simple method name, and the empty parentheses supply no arguments.

  • A is not correct: a class method belongs to the class itself, so it can be called without first creating an object of that class.
  • C is not correct: a static method may return a value, since square is declared static and returns an int.
  • D is not correct: a static method may take parameters, since square has one int parameter named number.