AP Computer Science a Quiz: Introduction To Algorithms Programming And Compilers
20 questions · exam conditions
0:00
Introduction To Algorithms Programming And CompilersQuestion 1 of 20

A bank account program models each account as an object with encapsulated state (balance) and methods to change it. The withdraw method checks rules before updating the private field:

public boolean withdraw(double amount) {
  if (amount <= balance) {
    balance -= amount;
    return true;
  }
  return false;
}

Based on the passage, what is the main benefit of calling withdraw instead of changing balance directly?​

It guarantees every withdrawal succeeds, even if the balance is too low
It enforces rules before updating the private balance inside the object
It makes the balance field public so other classes can update it freely
It causes the method to run only once, preventing future withdrawals
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Introduction To Algorithms Programming And Compilers

Practice Introduction To Algorithms Programming And Compilers in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Introduction To Algorithms Programming And Compilers, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

A bank account program models each account as an object with encapsulated state (balance) and methods to change it. The withdraw method checks rules before updating the private field:

public boolean withdraw(double amount) {
  if (amount <= balance) {
    balance -= amount;
    return true;
  }
  return false;
}

Based on the passage, what is the main benefit of calling withdraw instead of changing balance directly?​

  1. It guarantees every withdrawal succeeds, even if the balance is too low
  2. It enforces rules before updating the private balance inside the object (correct answer)
  3. It makes the balance field public so other classes can update it freely
  4. It causes the method to run only once, preventing future withdrawals

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on controlled access through methods in context. The concept of controlled access involves using methods to enforce business rules before modifying an object's state. In object-oriented programming, this is crucial for maintaining data consistency and preventing invalid states. Choice B is correct because it accurately reflects the withdraw method's role as described in the passage, demonstrating understanding of how methods can validate conditions before updating private fields. Choice A is incorrect because it misrepresents the method's purpose - the code clearly shows withdrawals can fail if the balance is insufficient. To help students: Encourage practice through coding exercises that focus on writing methods with validation logic. Use examples from real-world applications to illustrate how methods protect data integrity, like password validation before account access. Watch for: students confusing validation with guaranteed success or thinking methods always perform their intended action.

Question 2

A student writes a program to calculate 10 / x, where x is an integer input by the user. The program compiles successfully. When the program is run and the user enters 2, it works correctly. When the user enters 0, the program terminates with an ArithmeticException. This exception is an example of what type of error?

  1. A syntax error
  2. A run-time error (correct answer)
  3. A logic error not causing a crash
  4. A compilation error

Explanation: The error (division by zero) occurs during the program's execution based on a specific input value, causing it to crash. This is a classic example of a run-time error. It is not a syntax error because the code was valid. While it's a logic flaw to not prevent this, the resulting crash makes 'run-time error' the most accurate classification.

Question 3

A bank account program stores balance inside each object and uses methods to update it. The balance field is private, so other classes cannot access it directly:

private double balance;

Based on the passage, why is making balance private consistent with encapsulation?​

  1. It prevents any method in the class from reading or updating the balance
  2. It restricts direct access so changes happen through methods like deposit (correct answer)
  3. It makes balance shared across all objects to keep values synchronized
  4. It forces Java to inherit balance from Object rather than store it locally

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on access modifiers and encapsulation in context. The concept of private access modifiers involves restricting direct access to fields from outside the class, forcing interaction through public methods. In object-oriented programming, this is crucial for maintaining control over how data is accessed and modified. Choice B is correct because it accurately reflects the purpose of private fields as described in the passage, demonstrating understanding that encapsulation requires controlled access through methods. Choice A is incorrect because it misunderstands scope - private fields can be accessed by methods within the same class. To help students: Encourage practice through coding exercises that focus on using private fields with public methods. Use examples from real-world applications to illustrate how private fields protect data, like keeping passwords private while providing a checkPassword method. Watch for: students thinking private means completely inaccessible or confusing private with static.

Question 4

Which of the following best defines an algorithm in the context of computer science?

  1. A high-level programming language used to write instructions for a computer.
  2. A finite, step-by-step set of instructions designed to solve a problem or perform a computation. (correct answer)
  3. A hardware component within a computer that is responsible for executing program instructions.
  4. A software application that translates source code into machine-readable code all at once.

Explanation: An algorithm is a well-defined, step-by-step procedure for solving a problem or accomplishing a task. Choice A describes a programming language. Choice C describes a central processing unit (CPU). Choice D describes a compiler.

Question 5

A program successfully compiles and begins to run. However, during execution, it attempts to access a file that does not exist, causing the program to terminate abnormally. What is this type of error called?

  1. A syntax error
  2. A logic error
  3. A compilation error
  4. A run-time error (correct answer)

Explanation: The error occurs during the execution ('run-time') of the program, after it has been successfully compiled. This is the definition of a run-time error. It is not a syntax or compilation error because the code was grammatically valid. While it could be caused by a logic flaw, 'run-time error' is the most specific term for a crash during execution.

Question 6

A program is written to find the smallest number in a list of integers. The programmer initializes a variable minVal to 1000, assuming all numbers in the list will be smaller. The program fails if the list contains a number greater than or equal to 1000. This is an example of a:

  1. Run-time error, because the program fails under certain conditions.
  2. Syntax error, because the variable minVal was not initialized correctly.
  3. Logic error, because the algorithm's assumption is flawed for a set of valid inputs. (correct answer)
  4. Compilation error, because the compiler cannot handle large numbers in this context.

Explanation: The program compiles and runs, but the algorithm itself is flawed because it makes an incorrect assumption about the range of input data. This leads to incorrect results for certain valid inputs, which is a logic error. It is not a run-time error as the program does not necessarily crash. It is not a syntax error as the code is grammatically correct.

Question 7

In an object-oriented bank account program, methods are behaviors that operate on an object's stored data. For example, deposit updates the private balance:

public void deposit(double amount) {
  balance += amount;
}

Refer to the example in the text, what does amount represent when a.deposit(25.0) is called?​

  1. A local parameter receiving the value 25.0 for this method call (correct answer)
  2. A public field that permanently stores 25.0 for all BankAccount objects
  3. A method name that replaces deposit during compilation
  4. A rule that forces balance to become exactly 25.0 after the call

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on method parameters in context. The concept of method parameters involves variables that receive values when a method is called, existing only during that method's execution. In object-oriented programming, this is crucial for allowing methods to work with different input values. Choice A is correct because it accurately reflects the role of 'amount' as described in the passage, demonstrating understanding that 25.0 is passed as a parameter value to the deposit method. Choice B is incorrect because it confuses parameters with fields - parameters are temporary variables, not permanent storage. To help students: Encourage practice through coding exercises that focus on tracing parameter values through method calls. Use examples from real-world applications to illustrate how parameters allow methods to be flexible, like a print method accepting different messages. Watch for: students confusing parameters with instance fields or thinking parameter values persist after the method completes.

Question 8

In the bank account scenario, objects store data (fields) and methods operate on that data. A field represents state, while a method represents behavior:

private double balance;
public void deposit(double amount) { balance += amount; }

Based on the passage, which statement best distinguishes a field from a method in this class?​

  1. A field stores the account's state, while a method performs actions on that state (correct answer)
  2. A field runs code when called, while a method stores numbers for later use
  3. A field is inherited automatically, while a method cannot be invoked at all
  4. A field is always public, while a method must always be private in Java

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on distinguishing fields from methods in context. The concept of fields versus methods involves understanding that fields store an object's state (data) while methods define its behavior (actions). In object-oriented programming, this is crucial for properly structuring classes with appropriate data and operations. Choice A is correct because it accurately reflects the distinction as described in the passage, demonstrating understanding that balance is a field storing state while deposit is a method performing actions. Choice B is incorrect because it reverses the definitions - fields store data, methods contain executable code. To help students: Encourage practice through coding exercises that focus on identifying and creating both fields and methods. Use examples from real-world applications to illustrate the distinction, like a Car class with fields for speed/fuel and methods for accelerate/brake. Watch for: students confusing the syntax or thinking methods can store persistent data like fields.

Question 9

A bank program uses a BankAccount class where balance is private, and methods enforce rules. For example, deposit ignores negative values, and withdraw checks available funds before subtracting. This design keeps the object's state consistent by restricting direct field access. Based on the passage, what is a key reason to restrict direct access to balance?

  1. To prevent invalid updates that could break account rules (correct answer)
  2. To ensure methods can be called only from inside the same method
  3. To make all objects share one balance for easier tracking
  4. To allow Java to automatically rename fields during compilation

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on data validation through encapsulation in context. The concept of data validation involves using methods to enforce business rules and maintain object consistency by preventing invalid state changes. In object-oriented programming, this is crucial for ensuring objects always remain in valid states that make sense for the problem domain. Choice A is correct because it accurately reflects the validation benefit as described in the passage, demonstrating understanding of how methods like deposit and withdraw enforce rules to prevent invalid updates. Choice C is incorrect because it suggests making all objects share data, which would violate object independence and make validation impossible. To help students: Encourage practice through coding exercises that implement validation logic in methods. Use examples from real-world applications to illustrate how validation prevents errors, like preventing negative balances in bank accounts. Watch for: students thinking encapsulation is only about hiding data rather than also about maintaining validity.

Question 10

A program creates two separate BankAccount objects, each with its own private balance:

BankAccount a1 = new BankAccount("A100", 50.0);
BankAccount a2 = new BankAccount("B200", 10.0);
a1.deposit(20.0);

Objects encapsulate their own data, and invoking a method affects the object it is called on. Refer to the example in the text, which balance changes after the deposit call?

  1. Only a1 changes because the method is invoked on a1 (correct answer)
  2. Only a2 changes because both objects share one balance field
  3. Both a1 and a2 change because deposit updates the class
  4. Neither changes because parameters prevent instance variables updating

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on object independence in context. The concept of object independence involves each object maintaining its own state, and method calls affecting only the specific object they're invoked on. In object-oriented programming, this is crucial for objects to represent separate entities that don't interfere with each other. Choice A is correct because it accurately reflects object independence as described in the passage, demonstrating understanding of how a1.deposit() affects only a1's balance. Choice C is incorrect because it suggests methods affect all objects of a class simultaneously, which would violate the principle of object independence. To help students: Encourage practice through coding exercises that create multiple objects and invoke methods on specific ones. Use examples from real-world applications to illustrate how one person's bank transaction doesn't affect other accounts. Watch for: students thinking method calls affect all objects of a class or confusing instance methods with static methods.

Question 11

A bank class keeps balance private and provides a getter:

private double balance;
public double getBalance() { return balance; }

Encapsulation packages data with methods and controls direct access to fields. Based on the passage, why is getBalance useful in an encapsulated design?

  1. It provides controlled read access without exposing the field directly (correct answer)
  2. It allows outside code to modify balance without any restrictions
  3. It automatically deposits money whenever the balance is requested
  4. It replaces the need to create BankAccount objects in the program

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on getter methods in context. The concept of getter methods involves providing controlled read-only access to private fields without allowing direct modification. In object-oriented programming, this is crucial for maintaining encapsulation while still allowing necessary data access. Choice A is correct because it accurately reflects the getter's purpose as described in the passage, demonstrating understanding of how getBalance() provides read access without exposing the field for modification. Choice B is incorrect because it contradicts the fundamental purpose of getters - they provide read access, not write access. To help students: Encourage practice through coding exercises that implement getters and setters with appropriate access control. Use examples from real-world applications to illustrate how read-only access is useful, like checking account balance at an ATM. Watch for: students confusing getters with setters or thinking that returning a value allows external modification.

Question 12

A bank account program uses objects to keep each account's data separate. Each BankAccount object has its own private balance, and methods change that balance:

BankAccount x = new BankAccount(100.0);
BankAccount y = new BankAccount(100.0);
x.deposit(10.0);

Based on the passage, what does encapsulation help ensure about x and y?

  1. They share one balance field, so any deposit updates both objects equally
  2. They each maintain their own balance, updated only through their methods (correct answer)
  3. They automatically merge into one object when their starting values match
  4. They can access and modify each other's private fields without methods

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on object independence through encapsulation in context. The concept of object independence means each object maintains its own separate state, even when created from the same class. In object-oriented programming, this is crucial for modeling real-world entities that need to maintain distinct identities and data. Choice B is correct because it accurately reflects encapsulation's role as described in the passage, demonstrating understanding that each BankAccount object has its own private balance field. Choice A is incorrect because it represents a fundamental misunderstanding - objects don't share instance fields; each object has its own copy. To help students: Encourage practice through coding exercises that focus on creating multiple objects and modifying them independently. Use examples from real-world applications to illustrate how objects maintain separate state, like multiple bank accounts for different customers. Watch for: students confusing instance fields with static fields or thinking objects of the same class share data.

Question 13

In the bank account example, methods are invoked on an object using dot notation. For instance:

BankAccount a = new BankAccount(100.0);
a.deposit(25.0);

Refer to the example in the text, what does the expression a.deposit(25.0) do?​

  1. It calls deposit on object a, passing 25.0 as the method argument (correct answer)
  2. It creates a new class named deposit and assigns it to variable a
  3. It directly changes the private field balance from outside the object
  4. It invokes deposit once for every BankAccount object currently in memory

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on method invocation syntax in context. The concept of dot notation involves using the object reference, followed by a dot, then the method name and arguments to invoke a method on a specific object. In object-oriented programming, this is crucial for directing method calls to the correct object instance. Choice A is correct because it accurately reflects the method invocation as described in the passage, demonstrating understanding of how dot notation works with object 'a' calling deposit with argument 25.0. Choice C is incorrect because it violates encapsulation - the code shows deposit is a method that modifies the private field internally, not direct field access. To help students: Encourage practice through coding exercises that focus on method invocation syntax with different objects. Use examples from real-world applications to illustrate how dot notation directs actions to specific objects, like different remote controls for different devices. Watch for: students confusing method calls with variable assignments or thinking dot notation provides direct field access.

Question 14

In the bank account scenario, constructors initialize an object's data when it is created. The class stores balance as a private field, and methods are invoked on a specific object:

public BankAccount(double start) {
  balance = start;
}
BankAccount c = new BankAccount(200.0);

Based on the passage, what does the constructor primarily do for each new BankAccount object?​

  1. It hides all methods so they cannot be invoked using dot notation
  2. It initializes the object's balance field to a starting value (correct answer)
  3. It copies methods from a parent class even when none is specified
  4. It converts the balance into an integer to avoid decimal values

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on constructor functionality in context. The concept of constructors involves special methods that initialize an object's state when it's created. In object-oriented programming, this is crucial for ensuring objects start with valid, meaningful values. Choice B is correct because it accurately reflects the constructor's role as described in the passage, demonstrating understanding of how the BankAccount constructor sets the initial balance. Choice C is incorrect because it confuses constructors with inheritance - constructors initialize fields, they don't copy methods from parent classes. To help students: Encourage practice through coding exercises that focus on writing constructors with different parameters. Use examples from real-world applications to illustrate how constructors establish initial state, like setting a student's name and ID when creating a Student object. Watch for: students confusing constructors with regular methods or thinking constructors are related to inheritance.

Question 15

A bank account program uses methods to modify an object's internal state. The withdraw method returns a boolean indicating whether the update occurred:

boolean ok = a.withdraw(80.0);

Based on the passage, why might withdraw return a boolean value?​

  1. To report whether the balance update succeeded under the method's rules (correct answer)
  2. To convert the balance into true or false instead of storing a number
  3. To expose the private balance so other classes can read it directly
  4. To ensure parameters can only be passed once during the program run

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on method return values in context. The concept of boolean return values involves methods communicating success or failure of an operation back to the caller. In object-oriented programming, this is crucial for allowing calling code to respond appropriately to different outcomes. Choice A is correct because it accurately reflects the withdraw method's return value as described in the passage, demonstrating understanding that the boolean indicates whether the withdrawal succeeded based on the balance check. Choice B is incorrect because it confuses return values with data storage - the method returns a status indicator, not a conversion of the balance. To help students: Encourage practice through coding exercises that focus on methods with meaningful return values. Use examples from real-world applications to illustrate how return values communicate outcomes, like a login method returning true/false. Watch for: students thinking return values change the object's state or confusing return types with field types.

Question 16

In an object-oriented bank program, objects store state and methods operate on that state. A constructor initializes the object's fields when it is created. Example:

public class BankAccount {
  private double balance;
  public BankAccount(double start) { balance = start; }
}

BankAccount d = new BankAccount(75.0);

Based on the passage, what is the constructor's main role in creating object d?

  1. It initializes the new object's fields using the provided starting value. (correct answer)
  2. It permanently prevents any method from changing the balance afterward.
  3. It copies methods from other classes so the account can deposit money.
  4. It converts the object into a parameter so methods can be called without dots.

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on constructors in context. The concept of constructors involves special methods that initialize an object's fields when the object is created, setting up its initial state. In object-oriented programming, this is crucial for ensuring objects start with valid, meaningful values rather than default or random data. Choice A is correct because it accurately reflects how the constructor uses the parameter start to initialize the balance field to 75.0. Choice B is incorrect because constructors initialize state but don't prevent future modifications - that's controlled by access modifiers and method logic. To help students: Encourage practice through coding exercises that focus on writing constructors with different parameters and initializing multiple fields. Use examples from real-world applications to illustrate how constructors ensure objects are properly set up before use. Watch for: students confusing constructors with regular methods or thinking constructors control future behavior rather than initial setup.

Question 17

A bank app models each account as an object. The BankAccount class defines private data and public methods. Encapsulation means code outside the class cannot directly change balance. Example:

public class BankAccount {
  private double balance;
  public void deposit(double amount) { balance += amount; }
}

Based on the passage, which statement best explains why balance is declared private?

  1. It prevents outside code from changing the balance without using the class's methods. (correct answer)
  2. It allows any other class to update the balance without calling a method.
  3. It guarantees the balance can only increase, never decrease, in any method.
  4. It forces Java to store the balance in a different data type at runtime.

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on access modifiers and encapsulation in context. The concept of private access modifier involves restricting direct access to fields from outside the class, ensuring data can only be modified through controlled methods. In object-oriented programming, this is crucial for maintaining data integrity and preventing unauthorized or incorrect modifications. Choice A is correct because it accurately reflects how private prevents direct access to balance, requiring use of class methods instead. Choice B is incorrect because it contradicts the fundamental purpose of private - to restrict rather than allow access. To help students: Encourage practice through coding exercises that focus on attempting to access private fields (seeing compiler errors) versus using public methods. Use examples from real-world applications to illustrate how private fields protect sensitive data from corruption. Watch for: students thinking private means the field cannot be changed at all, rather than understanding it controls how changes occur.

Question 18

A bank account object keeps its balance private and provides methods to update it. Method calls use dot notation on a specific object, so different objects can behave independently. Example:

BankAccount a1 = new BankAccount(100.0);
BankAccount a2 = new BankAccount(100.0);
a1.deposit(10.0);

Refer to the example in the text, why does only a1 change after a1.deposit(10.0)?

  1. The method runs on the a1 object, updating only its stored balance. (correct answer)
  2. Java automatically applies every method call to all objects of the same class.
  3. The parameter 10.0 forces both objects to share one balance field.
  4. Encapsulation requires all accounts to always have identical balances.

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on object independence in context. The concept of object independence involves each object maintaining its own separate state (instance variables), so method calls affect only the specific object they're invoked on. In object-oriented programming, this is crucial for allowing multiple objects of the same class to exist with different states. Choice A is correct because it accurately reflects how a1.deposit(10.0) operates only on a1's balance, leaving a2 unchanged. Choice B is incorrect because it suggests methods affect all objects of a class simultaneously, which contradicts fundamental object-oriented principles. To help students: Encourage practice through coding exercises that focus on creating multiple objects and observing how method calls affect only the target object. Use examples from real-world applications to illustrate how each bank account maintains its own balance independently. Watch for: students thinking all objects of a class share state or that method calls propagate to all instances.

Question 19

In a bank program, each BankAccount object stores its own balance and accountNumber as instance variables. Encapsulation keeps balance private, and methods provide controlled access:

public double getBalance() { return balance; }

A method is invoked on a specific object, so different objects can behave independently. Based on the passage, what does it mean that each object stores its own data?

  1. All BankAccount objects share one balance value in common
  2. Each BankAccount object has separate instance variable values (correct answer)
  3. The balance is stored only inside deposit and withdraw methods
  4. The balance is stored in a global variable outside the class

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on instance variables in context. The concept of instance variables involves each object maintaining its own separate copy of data fields declared in the class. In object-oriented programming, this is crucial for objects to represent distinct entities with independent states. Choice B is correct because it accurately reflects instance variable behavior as described in the passage, demonstrating understanding of how each BankAccount object has its own balance and accountNumber. Choice A is incorrect because it describes static/class variables rather than instance variables - sharing one balance would mean all accounts have the same balance. To help students: Encourage practice through coding exercises that create multiple objects and show how each maintains separate data. Use examples from real-world applications to illustrate how different bank accounts must have independent balances. Watch for: students confusing instance variables with static variables or thinking all objects of a class share the same data.

Question 20

In a bank account management program, each BankAccount object stores its own balance as private data, and methods update that data. Method invocation uses dot notation, and parameters are values passed into methods. Example:

BankAccount b = new BankAccount(50.0);
b.deposit(20.0);
b.withdraw(10.0);

Refer to the example in the text, which of the following best describes how parameters are passed to methods?​

  1. They are copied into the method's parameter variables when the call is made (correct answer)
  2. They replace the method name, so dot notation is no longer needed
  3. They automatically convert the object's fields into new data types
  4. They are stored as public fields so other objects can reuse them later

Explanation: This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on parameter passing in context. The concept of parameter passing involves providing values to methods when they are called, which are then copied into the method's parameter variables. In object-oriented programming, this is crucial for allowing methods to work with different values each time they're invoked. Choice A is correct because it accurately reflects parameter passing as described in the passage, demonstrating understanding of how values like 20.0 and 10.0 are passed to deposit and withdraw methods. Choice B is incorrect because it represents a fundamental misunderstanding of method invocation - parameters don't replace method names but are passed as arguments. To help students: Encourage practice through coding exercises that focus on calling methods with different parameter values. Use examples from real-world applications to illustrate how parameters allow methods to be flexible and reusable. Watch for: students confusing parameters with fields or thinking parameters permanently change the method definition.