Free AP CSA Object-Oriented Programming Questions and Answers — Questions and Answers
Question 1: What is the correct way to create an object from the following class?
- Car myCar = new Car();
- Car myCar = new Car("Red"); (Correct answer)
- Car myCar = Car("Red");
- Car myCar = new Car(String color);
Correct answer: Car myCar = new Car("Red");
To create an object in Java, you use the `new` keyword followed by a call to the class's constructor. The `Car` class has a constructor `Car(String color)` that requires a `String` argument. Therefore, `Car myCar = new Car("Red");` correctly instantiates a `Car` object, passing "Red" as the color, and assigns it to the `myCar` variable.
Question 2: How does encapsulation improve code quality?
- By making all class variables public.
- By hiding the implementation details of a class. (Correct answer)
- By using getters and setters to control access to variables. (Correct answer)
- By ensuring all methods are static.
Correct answer: By hiding the implementation details of a class.
Encapsulation is a core OOP principle that enhances code quality by bundling data and methods within a class and controlling access to its internal state. It primarily achieves this by hiding the implementation details of a class (e.g., making variables private) and providing public methods (getters and setters) for controlled interaction. This protects data integrity, reduces complexity, and makes code more modular and maintainable.
Question 3: What is the output of the following code?
- Some sound
- Bark (Correct answer)
- Error: Animal cannot refer to Dog.
- Both Some sound and Bark
Correct answer: Bark
This code demonstrates polymorphism and method overriding. An `Animal` reference `myAnimal` is assigned a `Dog` object. When `myAnimal.makeSound()` is called, Java's runtime polymorphism ensures that the specific `makeSound()` method implemented in the `Dog` class (the actual object type) is invoked, rather than the `Animal` class's method. Therefore, the output will be "Bark".
Question 4: Which of the following statements are true about method overloading and overriding?
- Overloading occurs within the same class. (Correct answer)
- Overriding requires inheritance. (Correct answer)
- Overloaded methods must have different method names.
- Overridden methods must have the same method signature. (Correct answer)
Correct answer: Overloading occurs within the same class.
Method overloading occurs within the same class when multiple methods share the same name but have different parameter lists (different method signatures). Method overriding, on the other hand, requires inheritance, where a subclass provides a specific implementation for a method already defined in its superclass. For a method to be overridden, it must have the exact same method signature (name, return type, and parameter list) as the method in the superclass.
Question 5: What is true about abstract classes in Java?
- Abstract classes can have both abstract and non-abstract methods. (Correct answer)
- An abstract class cannot be instantiated. (Correct answer)
- An abstract class must have at least one abstract method.
- A class that extends an abstract class must implement all its abstract methods. (Correct answer)
Correct answer: Abstract classes can have both abstract and non-abstract methods.
Abstract classes in Java serve as blueprints for other classes and cannot be instantiated directly. They can contain a combination of abstract methods (declared without an implementation) and concrete methods (with full implementations). Any concrete class that extends an abstract class must provide implementations for all the abstract methods inherited from the abstract class, or it must also be declared abstract itself.
What is the correct way to create an object from the following class?