Introduction to Algorithms, Programming, and Compilers
Help Questions
AP Computer Science A › Introduction to Algorithms, Programming, and Compilers
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
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.
A developer is testing a program by running it with various inputs and checking if the outputs are correct. What type of error is this process primarily designed to uncover?
Logic errors
Syntax errors
Network errors
Hardware errors
Explanation
Testing with specific inputs to verify the output is the main method for detecting logic errors, which are flaws in the program's algorithm or implementation. Syntax errors would have been caught by the compiler, and hardware/network errors are typically external to the program's code.
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?
A run-time error
A syntax error
A compilation error
A logic error not causing a crash
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.
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 forces Java to inherit balance from Object rather than store it locally
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
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.
A programmer writes a program that successfully compiles without any issues. What can be concluded with certainty from this fact?
The program is free of run-time errors but may contain syntax or logic errors.
The program is free of all errors and will run correctly for all possible inputs.
The program is free of logic errors but may contain syntax or run-time errors.
The program is free of syntax errors but may contain logic or run-time errors.
Explanation
Successful compilation only guarantees that the code adheres to the programming language's syntax rules. It does not guarantee that the logic is correct or that no errors will occur during execution (run-time errors).
Which of the following best defines an algorithm in the context of computer science?
A finite, step-by-step set of instructions designed to solve a problem or perform a computation.
A hardware component within a computer that is responsible for executing program instructions.
A high-level programming language used to write instructions for a computer.
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.
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?
A run-time error
A compilation error
A syntax error
A logic error
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.
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:
Logic error, because the algorithm's assumption is flawed for a set of valid inputs.
Syntax error, because the variable minVal was not initialized correctly.
Run-time error, because the program fails under certain conditions.
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.
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 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
A local parameter receiving the value 25.0 for this method 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.
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.