PrepGo

AP Computer Science A Practice Quiz: Documentation with Comments

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 role of comments in a computer program?

All Questions (10)

What is the primary role of comments in a computer program?

A) To provide instructions for the compiler to execute.

B) To be displayed to the end-user when the program runs.

C) To describe the functionality and use of code for programmers.

D) To improve the execution speed of the program.

Correct Answer: C

According to the provided content, comments are written 'for both the original programmer and other programmers to understand the code and its functionality.' They are ignored by the compiler and not executed.

How does a compiler handle comments found in the source code?

A) It converts them into executable machine code.

B) It flags them as syntax errors.

C) It ignores them completely.

D) It executes them as special commands.

Correct Answer: C

The text explicitly states that comments 'are ignored by the compiler and are not executed when the program is run.'

Which of the following best defines a precondition for a method?

A) A condition that must be true after the method has finished executing.

B) A check that the method performs internally to validate its own logic.

C) A condition that must be true just prior to the execution of the method.

D) A description of the value that the method will return.

Correct Answer: C

The provided content defines a precondition as 'a condition that must be true just prior to the execution of a method in order for it to behave as expected.'

What is the purpose of a postcondition for a method?

A) To ensure certain conditions are met before the method is called.

B) To describe the outcome of the method's execution, such as its return value or object state.

C) To provide a summary of the method's code for the compiler.

D) To list the potential errors that the method might encounter during execution.

Correct Answer: B

The text states that a postcondition 'is a condition that must always be true after the execution of a method' and that it 'describe[s] the outcome of the execution in terms of what is being returned or the current value of the attributes of an object.'

A method `calculateSquareRoot(double num)` is designed to find the square root of a number. Which of the following is the most appropriate precondition for this method?

A) The method will return a non-negative number.

B) The input `num` must be a non-negative number (>= 0).

C) The method must successfully calculate the square root.

D) The input `num` must be an integer.

Correct Answer: B

A precondition is a condition that must be true *before* a method is executed. To calculate a real square root, the input number cannot be negative. Therefore, a precondition is that `num` must be non-negative. Option A describes a postcondition, not a precondition.

A method `addToCart(Item item)` adds a product to a shopping cart object. Which of the following best describes a postcondition for this method?

A) The `item` to be added must not be null.

B) The shopping cart must have space available.

C) The shopping cart's item count has increased by one, and the cart now contains the `item`.

D) The method will be called with a valid `Item` object.

Correct Answer: C

A postcondition describes the state of the object *after* the method has executed. After successfully adding an item, the cart's state should reflect that change—specifically, the item count increases and the new item is present. Options A, B, and D describe potential preconditions.

According to the provided text, what is a method's responsibility regarding its stated preconditions?

A) The method must check its preconditions and return an error if they are not met.

B) The method is not expected to check if its preconditions are satisfied.

C) The method must correct any input that does not meet the preconditions.

D) The method must log a warning if preconditions are not satisfied.

Correct Answer: B

The content explicitly states, 'There is no expectation that the method will check to ensure preconditions are satisfied.' This implies the responsibility for meeting preconditions lies with the code that calls the method.

Who is the intended audience for comments, preconditions, and postconditions?

A) The compiler and the computer's processor.

B) The end-user of the software.

C) The original programmer and other programmers who will read the code.

D) The quality assurance and testing team exclusively.

Correct Answer: C

The text states that comments are written for 'the original programmer and other programmers to understand the code.' Preconditions and postconditions are forms of documentation that serve the same purpose: helping programmers understand how to use a method correctly and what to expect from it.

Which statement accurately distinguishes between preconditions and postconditions?

A) Preconditions describe the return value, while postconditions describe the input parameters.

B) Preconditions are checked by the compiler, while postconditions are checked at runtime.

C) Preconditions must be true before a method runs for it to work as expected, while postconditions must be true after it finishes.

D) Preconditions are required for all methods, while postconditions are optional.

Correct Answer: C

This option correctly summarizes the core difference based on the provided definitions. A precondition is a condition 'prior to the execution,' and a postcondition is a condition 'after the execution.'

What is the effect of comments on the final, running program?

A) They are displayed in a console window for debugging.

B) They have no effect because they are not executed.

C) They can slow down the program if there are too many.

D) They are converted into help documentation for the user.

Correct Answer: B

The provided text states that comments 'are ignored by the compiler and are not executed when the program is run.' Therefore, they have no impact on the behavior or performance of the final executed program.