PrepGo

AP Computer Science A Practice Quiz: Methods: How to Write Them

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

What is the primary purpose of writing methods within a class, according to the provided content?

All Questions (10)

What is the primary purpose of writing methods within a class, according to the provided content?

A) To store primitive data for an object.

B) To define the behaviors of an object.

C) To create new objects of the class.

D) To declare the class's instance variables.

Correct Answer: B

The content states that methods are developed to 'define behaviors of an object'. Storing data is the role of instance variables, and creating objects is typically done via a constructor.

Which of the following method headers correctly defines a non-void method that is intended to return a boolean value?

A) public void checkStatus()

B) public boolean checkStatus()

C) public return boolean checkStatus()

D) public checkStatus() boolean

Correct Answer: B

A non-void method's header must include the return type (in this case, 'boolean') in place of the keyword 'void'. The return type precedes the method name.

What is the main function of an accessor method?

A) To modify the value of an instance variable.

B) To allow other classes to obtain a copy of an instance variable's value.

C) To perform a calculation without returning a value.

D) To initialize an object's state when it is created.

Correct Answer: B

The content explicitly states, 'An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables.' Methods that modify values are typically called mutator methods.

Consider the following code segment. What is printed to the console? public class NumberManipulator { public void attemptChange(int val) { val = 50; } } // In another class's main method: NumberManipulator nm = new NumberManipulator(); int myNum = 10; nm.attemptChange(myNum); System.out.println(myNum);

A) 10

B) 50

C) 60

D) The code will produce a compile-time error.

Correct Answer: A

When the primitive value 'myNum' is passed to the 'attemptChange' method, the parameter 'val' receives a copy of its value. The change to 'val' inside the method does not affect the original 'myNum' variable.

A method designed to provide the current balance of a BankAccount object is defined with the header `public double getBalance()`. What type of method is this?

A) A void method

B) A constructor

C) An accessor method

D) A static method

Correct Answer: C

This is a non-void method that allows other objects to obtain the value of an instance variable (the balance). This fits the definition of an accessor method.

Consider the following method and code that calls it. What is the value of `x` after the code segment has executed? public void swap(int a, int b) { int temp = a; a = b; b = temp; } // Code segment in another method: int x = 5; int y = 10; swap(x, y);

A) 5

B) 10

C) 0

D) The value is unpredictable.

Correct Answer: A

Because primitive types are passed by value, the 'swap' method receives copies of the values of x and y. The method successfully swaps its local copies, 'a' and 'b', but the original variables 'x' and 'y' in the calling scope remain unchanged.

What key characteristic distinguishes a non-void method from a void method in its header?

A) The use of the `public` keyword.

B) The presence of parameters in parentheses.

C) The inclusion of a specific return type instead of the keyword `void`.

D) The method name must start with 'get'.

Correct Answer: C

The content states that a non-void method's header 'includes the return type in place of the keyword void'. This is the fundamental difference in their declarations.

Consider the `Calculator` class. What is the output of the `main` method in the `TestCalc` class? // In Calculator.java public class Calculator { public int addTen(int num) { num = num + 10; return num; } } // In TestCalc.java public class TestCalc { public static void main(String[] args) { Calculator calc = new Calculator(); int val = 20; calc.addTen(val); System.out.println(val); } }

A) 10

B) 20

C) 30

D) The code will not compile because the return value is not used.

Correct Answer: B

Even though the `addTen` method returns a new value (30), the calling code does not assign this returned value back to the `val` variable. Because `val` is a primitive, the original variable is unaffected by the operations inside the method. Therefore, `val` remains 20.

If a method is called with a primitive argument, what is true about the method's corresponding parameter?

A) The parameter is a reference to the original argument's memory location.

B) The parameter is initialized with a copy of the argument's value.

C) The parameter must have a different data type than the argument.

D) Any change to the parameter will also change the argument.

Correct Answer: B

This directly tests the rule from the provided content: 'When an argument is a primitive value, the parameter is initialized with a copy of that value.' This is the principle of pass-by-value.

A `Player` class is defined below. What is printed when the `main` method is executed? public class Player { private int health; public Player(int h) { this.health = h; } public int getHealth() { return this.health; } public void takeDamage(int damageAmount) { damageAmount = damageAmount / 2; // Damage is halved this.health -= damageAmount; } } // In another class's main method: Player p1 = new Player(100); int attackPower = 40; p1.takeDamage(attackPower); System.out.println(p1.getHealth() + " " + attackPower);

A) 80 40

B) 80 20

C) 60 40

D) 60 20

Correct Answer: A

The primitive `attackPower` (40) is passed to `takeDamage`. The parameter `damageAmount` gets a copy (40). Inside the method, `damageAmount` is changed to 20 (40 / 2). The player's health is then updated: `100 - 20 = 80`. After the method call, the original `attackPower` variable is unchanged because it's a primitive passed by value. The output is the new health (80) and the original `attackPower` (40).