PrepGo

AP Computer Science A Practice Quiz: Anatomy of a Class

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

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

Question 1 of 9

What is the primary function of the `private` keyword when applied to a class member?

All Questions (9)

What is the primary function of the `private` keyword when applied to a class member?

A) It allows access from any class in the same project.

B) It restricts access to the declaring class only.

C) It makes the member accessible only to subclasses.

D) It allows access from classes outside the declaring class.

Correct Answer: B

The provided content explicitly states, 'The keyword private restricts access to the declaring class...'

Which of the following best defines the concept of data encapsulation?

A) A technique to make all class data public for easy access.

B) A technique where the implementation details of a class are kept hidden from external classes.

C) A method for ensuring that a class can have multiple constructors.

D) The process of naming a class and its methods clearly.

Correct Answer: B

The content defines data encapsulation as 'a technique in which the implementation details of a class are kept hidden from external classes.'

According to good programming practice for data encapsulation, how should the instance variables of a class be declared?

A) As `public`, to allow direct manipulation from other classes.

B) As `private`, unless the class specification states otherwise.

C) Without any access modifier, to use the default visibility.

D) As `public` for data and `private` for methods.

Correct Answer: B

The content states, '...it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.'

A developer wants to create a method in a `Car` class that can be called by an object of a different class, `Driver`. Which access modifier should the developer use for this method?

A) private

B) internal

C) public

D) hidden

Correct Answer: C

The content specifies that 'the keyword public allows access from classes outside the declaring class,' which is necessary for the `Driver` class to access a method in the `Car` class.

What is the primary reason for keeping access to a class's attributes internal?

A) To improve the compilation speed of the program.

B) To allow any external class to modify the attributes directly.

C) To accomplish data encapsulation.

D) To reduce the number of methods required in the class.

Correct Answer: C

The provided text directly states, 'Access to attributes should be kept internal to the class in order to accomplish encapsulation.'

A programmer is creating a `Book` class with an instance variable for the number of pages, `pageCount`. To correctly apply the principle of data encapsulation, which of the following is the most appropriate declaration?

A) public int pageCount;

B) private int pageCount;

C) public Book(int pageCount) {}

D) private void setPageCount() {}

Correct Answer: B

To achieve data encapsulation, the content advises that instance variables (attributes) should be designated as `private`. This hides the implementation detail (`pageCount`) from external classes.

Which of the following design choices best exemplifies the principles of access constraints and data encapsulation described in the text?

A) A class with public instance variables and private methods.

B) A class with private instance variables and public methods to access and modify that data.

C) A class where all variables, constructors, and methods are declared public.

D) A class where all variables, constructors, and methods are declared private.

Correct Answer: B

This choice aligns with the principles of making instance variables `private` to hide them (encapsulation) and providing `public` methods to allow controlled access from outside the class.

The use of the `private` and `public` keywords allows a developer to control what aspect of classes, data, constructors, and methods?

A) Their memory allocation.

B) Their performance and speed.

C) Their access and visibility.

D) Their data type.

Correct Answer: C

The first point of the content is to 'Develop code to designate access and visibility constraints to classes, data, constructors, and methods.' The keywords `private` and `public` are the tools for this.

Why is designating instance variables as `private` considered a cornerstone of achieving data encapsulation?

A) Because it forces all data to be constant and unchangeable.

B) Because it allows any part of the program to inspect the variables for debugging.

C) Because it prevents external classes from directly accessing and modifying the internal state of an object, which is the definition of hiding implementation details.

D) Because it makes the code shorter and easier to write.

Correct Answer: C

Data encapsulation is about hiding implementation details. By making instance variables `private`, you restrict access to them, forcing external classes to use public methods (if provided). This hides the internal state and is the core mechanism for encapsulation as described in the text.