Introduction to Algorithms, Programming, and Compilers

Help Questions

AP Computer Science A › Introduction to Algorithms, Programming, and Compilers

Questions 1 - 10
1

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?​

It initializes the object’s balance field to a starting value

It copies methods from a parent class even when none is specified

It converts the balance into an integer to avoid decimal values

It hides all methods so they cannot be invoked using dot notation

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.

2

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?​

A local parameter receiving the value 25.0 for this method call

A method name that replaces deposit during compilation

A public field that permanently stores 25.0 for all BankAccount objects

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.

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?​

It restricts direct access so changes happen through methods like deposit

It prevents any method in the class from reading or updating the balance

It makes balance shared across all objects to keep values synchronized

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.

4

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?​

A field runs code when called, while a method stores numbers for later use

A field is always public, while a method must always be private in Java

A field is inherited automatically, while a method cannot be invoked at all

A field stores the account’s state, while a method performs actions on that state

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.

5

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?​

It creates a new class named deposit and assigns it to variable a

It calls deposit on object a, passing 25.0 as the method argument

It directly changes the private field balance from outside the object

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.

6

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?​

They are copied into the method’s parameter variables when the call is made

They replace the method name, so dot notation is no longer needed

They are stored as public fields so other objects can reuse them later

They automatically convert the object’s fields into new data types

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.

7

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?​

To convert the balance into true or false instead of storing a number

To report whether the balance update succeeded under the method’s rules

To ensure parameters can only be passed once during the program run

To expose the private balance so other classes can read it directly

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.

8

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?

They each maintain their own balance, updated only through their methods

They share one balance field, so any deposit updates both objects equally

They automatically merge into one object when their starting values match

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.

9

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 causes the method to run only once, preventing future withdrawals

It makes the balance field public so other classes can update it freely

It enforces rules before updating the private balance inside the object

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.

10

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?

It forces Java to store the balance in a different data type at runtime.

It prevents outside code from changing the balance without using the class’s methods.

It allows any other class to update the balance without calling a method.

It guarantees the balance can only increase, never decrease, in any method.

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.

Page 1 of 2