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

  1. Which of the following describes the process of a program's execution by a computer?
    Answer: The central processing unit (CPU) follows the machine-level instructions generated from the compiled program. Program execution is the process where the computer's hardware, specifically the CPU, carries out the low-level machine instructions that were produced by the compiler from the original source code. Choice A describes an interpreter. Choice B is an obsolete process. Choice D misstates the role of a compiler.
  2. Given the Student class above, which statement best explains why the following code produces a compilation error: Student s = new Student("John"); s.updateAge(20);
    Answer: The constructor requires exactly two parameters, but only one string argument is provided, violating method signature matching rules The constructor Student(String n, int a) requires exactly two parameters (name and age), but the call new Student("John") provides only one. This violates method signature matching - the compiler cannot find a constructor that matches the provided arguments. Choice B incorrectly focuses on field access rather than constructor parameters. Choice C misunderstands the error source. Choice D mentions default constructors but the issue is parameter count mismatch, not missing default constructor.
  3. Consider the following method call: String result = text.substring(2, 5).toUpperCase().replace('L', 'X'); where text contains "hello world". What programming concept does this demonstrate, and what is the final value of result?
    Answer: Method chaining with immutable objects; result equals "XXO" because each method returns a new string object This demonstrates method chaining where each method returns a new String object (strings are immutable in Java). text.substring(2,5) returns "llo", toUpperCase() returns "LLO", and replace('L','X') returns "XXO". Choice A has the correct result but incorrectly states strings are mutable. Choice C incorrectly describes this as sequential calls and gives wrong result. Choice D mischaracterizes strings as primitives and gives wrong result.
  4. 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?​
    Answer: To report whether the balance update succeeded under the method's rules 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.
  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?
    Answer: A run-time error 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.
  6. 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?​
    Answer: It initializes the object's balance field to a starting value 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.
  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?​
    Answer: A local parameter receiving the value 25.0 for this method call 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.
  8. A programmer writes a program that successfully compiles without any issues. What can be concluded with certainty from this fact?
    Answer: The program is free of syntax errors but may contain logic or run-time errors. 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).
  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?​
    Answer: It calls deposit on object a, passing 25.0 as the method argument 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. 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?
    Answer: Only a1 changes because the method is invoked on a1 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.
  11. Which of the following correctly describes the roles in the code development process?
    Answer: A programmer writes source code, and a compiler translates it for execution. This statement accurately describes the relationship: the human programmer writes the high-level source code, and the compiler tool translates that code into a lower-level format that the computer can run. The other options misrepresent the functions of these components.
  12. 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?
    Answer: Each BankAccount object has separate instance variable values 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.
  13. What is the correct sequence of events in a typical Java programming workflow?
    Answer: Write source code, compile the code, then execute the resulting program. The standard process is to first write the human-readable source code in a file, then use a compiler to translate it into machine-executable bytecode, and finally execute that bytecode to run the program.
  14. An AP-level bank account class uses objects and methods: objects store state (like balance), and methods update it using parameters. Encapsulation keeps fields private so only methods can modify them. Example: <pre>BankAccount e = new BankAccount(40.0); e.deposit(5.0); e.withdraw(10.0); </pre> Based on the passage, which statement best describes the relationship between an object and its methods?
    Answer: Methods define actions that operate on an object's stored data when called on that object. This question tests AP Computer Science A skills: understanding objects and methods, specifically focusing on the fundamental relationship between objects and methods in context. The concept of object-method relationships involves methods being behaviors that operate on an object's state when invoked on that specific object. In object-oriented programming, this is crucial for implementing the core principle that objects combine data and the operations on that data. Choice A is correct because it accurately reflects how methods like deposit and withdraw operate on the specific object's balance when called. Choice B is incorrect because it reverses the roles - methods don't store data, fields do, and methods perform actions. To help students: Encourage practice through coding exercises that focus on tracing how method calls affect object state. Use examples from real-world applications to illustrate how objects model entities with both attributes (fields) and behaviors (methods). Watch for: students confusing the roles of fields and methods or thinking methods affect multiple objects simultaneously.
  15. 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?
    Answer: It provides controlled read access without exposing the field directly 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.
  16. What is the primary function of a compiler in the process of creating an executable program?
    Answer: To translate source code written in a high-level language into a lower-level form that a computer can execute. The compiler's main role is to translate human-readable source code into a machine-executable format, such as bytecode for the Java Virtual Machine. Choice A describes an IDE's editor. Choice B describes a debugger. Choice D is incorrect as compilers detect syntax errors, not and do not correct logic errors.
  17. Given the binary search algorithm above, when implementing step 2a "Calculate middle index" in Java, which approach correctly avoids potential integer overflow while maintaining algorithm correctness?
    Answer: int middle = left + (right - left) / 2; because it prevents overflow by avoiding large intermediate sums Choice B avoids integer overflow by computing the offset (right-left)/2 and adding it to left, preventing the large intermediate sum (left+right) that could overflow. Choice A can overflow when left+right exceeds Integer.MAX_VALUE. Choice C uses floating-point but still has the overflow problem in (left+right). Choice D uses bit shifting but doesn't solve the overflow issue and introduces unnecessary complexity with unsigned operations.
  18. What is a key advantage of using an Integrated Development Environment (IDE) over a simple text editor for writing code?
    Answer: An IDE bundles tools like a compiler and debugger with a code editor, streamlining the development process. The 'Integrated' in IDE refers to the integration of multiple development tools like a source code editor, compiler, and debugger into a single application, which is its main advantage. A is false; any text editor can save files with any extension. C is an overstatement of AI code-generation capabilities. D is false; an IDE cannot prevent logic or run-time errors.
  19. A programmer attempts to compile a Java program and receives a message that a required semicolon is missing on a specific line. What type of error has occurred?
    Answer: Syntax error A missing semicolon violates the grammatical rules (syntax) of the Java language. Such errors are detected by the compiler before the program can run and are known as syntax errors.
  20. Which of the following scenarios best illustrates a logic error?
    Answer: A game is supposed to award 100 points for hitting a target, but due to a typo in the code, it awards 1000 points instead. This is a logic error because the program compiles and runs without crashing, but its behavior is incorrect according to the specification. Choices A and D are syntax errors. Choice B is a run-time error.

Start this quiz