Introduction to Algorithms, Programming, and Compilers

Help Questions

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

Questions 1 - 10
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?​

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 guarantees every withdrawal succeeds, even if the balance is too low

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.

2

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 makes balance shared across all objects to keep values synchronized

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

It restricts direct access so changes happen through methods like deposit

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.

3

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 rule that forces balance to become exactly 25.0 after the call

A public field that permanently stores 25.0 for all BankAccount objects

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.

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 stores the account’s state, while a method performs actions on that state

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

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

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

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

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?

To allow Java to automatically rename fields during compilation

To make all objects share one balance for easier tracking

To prevent invalid updates that could break account rules

To ensure methods can be called only from inside the same method

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.

6

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?

Both a1 and a2 change because deposit updates the class

Only a1 changes because the method is invoked on a1

Only a2 changes because both objects share one balance field

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.

7

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?

It provides controlled read access without exposing the field directly

It allows outside code to modify balance without any restrictions

It replaces the need to create BankAccount objects in the program

It automatically deposits money whenever the balance is requested

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.

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 can access and modify each other’s private fields without methods

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

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

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 directly changes the private field balance from outside the object

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

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.

10

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 converts the balance into an integer to avoid decimal values

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

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

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.

Page 1 of 2