zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What is the output of the following code?

java
class Animal {
 public void makeSound() {
 System.out.println("Generic animal sound");
 }
}
class Dog extends Animal {
 @Override
 public void makeSound() {
 System.out.println("Woof!");
 }
}
Animal myAnimal = new Dog();
myAnimal.makeSound();

Woof!

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is the output of the following code?

java
class Animal {
 public void makeSound() {
 System.out.println("Generic animal sound");
 }
}
class Dog extends Animal {
 @Override
 public void makeSound() {
 System.out.println("Woof!");
 }
}
Animal myAnimal = new Dog();
myAnimal.makeSound();

Woof!

Identify the error in the following code:

java
class Animal {
 private String name;
 public Animal(String name) {
 this.name = name;
 }
}
class Dog extends Animal {
 public Dog(String breed) {
 this.breed = breed;
 }
}

The Dog class constructor does not call the superclass constructor, and it attempts to access a non-existent 'breed' variable. It should call super(name) and accept the name parameter.

What is the output of the following code?

java
class Vehicle {
 int speed = 0;
 public void accelerate() {
 speed += 10;
 }
}
class Car extends Vehicle {
 @Override
 public void accelerate() {
 super.accelerate();
 speed += 20;
 }
}
Car myCar = new Car();
myCar.accelerate();
System.out.println(myCar.speed);

30

Identify the error in the following code:

java
class Parent {
 public void display() {
 System.out.println("Parent class");
 }
}
class Child extends Parent {
 public int display() {
 System.out.println("Child class");
 return 0;
 }
}

The Child class's display() method has a different return type (int) than the Parent class's display() method (void), making it an invalid override.

What is the output of the following code?

java
class A {
 public String toString() {
 return "Class A";
 }
}
class B extends A {
 public String toString() {
 return "Class B";
 }
}

A obj = new B();
System.out.println(obj.toString());

Class B

Identify the error in the following code:

java
class Animal {
 public void eat() {
 System.out.println("Animal eats");
 }
}
class Dog extends Animal {
 private void eat() {
 System.out.println("Dog eats");
 }
}

The Dog class's eat() method is private, making it not an override but a new method, and potentially inaccessible.

What is the output of the following code?

java
class Parent {
 public void method() {
 System.out.println("Parent");
 }
}
class Child extends Parent {
 public void method(int x) {
 System.out.println("Child");
 }
}
Parent p = new Child();
p.method();

Parent

Identify the error in the following code:

java
class Vehicle {}
class Car extends Vehicle {}

Car c = new Vehicle();

Cannot assign a Vehicle object to a Car reference. A superclass cannot be assigned to a subclass reference.

What is the output of the following code?

java
class Base {
 public void print() {
 System.out.println("Base");
 }
}
class Derived extends Base {
 public void print() {
 System.out.println("Derived");
 }
}
Base b = new Derived();
b.print();

Derived

Identify the error in the following code:

java
class Animal {
 public Animal(String name) {
 this.name = name;
 }
 String name;
}
class Dog extends Animal {
 public void bark() {
 System.out.println("Woof!");
 }
}

Dog class must call the superclass constructor Animal(String name) using super(name) in its constructor.

What are the differences between inheritance and polymorphism?

Inheritance: Establishes an IS-A relationship, allowing subclasses to inherit properties and behaviors. Polymorphism: Allows objects of different classes to be treated as objects of a common type.

What are the differences between method overriding and method overloading?

Overriding: Subclass provides a specific implementation of a method already defined in its superclass. Overloading: Defining multiple methods in the same class with the same name but different parameters.

What are the differences between a superclass and a subclass?

Superclass: A general class that other classes inherit from. Subclass: A specialized class that inherits properties and behaviors from a superclass.

What are the differences between Dynamic Typing and Static Typing?

Dynamic Typing: The method a computer executes on an object is determined by the run-time type of the object, not the declared type. Static Typing: The method a computer executes on an object is determined by the declared type of the object.

What are the differences between 'extends' and 'super' keywords?

'extends': Used to inherit from a superclass. 'super': Used to call the constructor or methods of the superclass from within the subclass.

What are the differences between an Interface and an Abstract Class?

Interface: Specifies a contract for classes to implement. Abstract Class: Provides a partial implementation and can contain both abstract and concrete methods.

What are the differences between Composition and Inheritance?

Composition: Building complex objects by combining simpler objects. Inheritance: Creating new classes based on existing classes, inheriting their properties and behaviors.

What are the differences between overriding and hiding a method?

Overriding: A subclass method with the same signature as a superclass method. Hiding: A subclass method with the same signature as a static superclass method.

What are the differences between early and late binding?

Early Binding: Method call is resolved at compile time. Late Binding: Method call is resolved at runtime (used in polymorphism).

What are the differences between IS-A and HAS-A relationships?

IS-A: Represents inheritance (e.g., a Dog IS-A Animal). HAS-A: Represents composition (e.g., a Car HAS-A Engine).

What is the main benefit of inheritance?

Reduces code duplication and makes code more maintainable by allowing subclasses to specialize superclass behavior without rewriting code.

Why are constructors not inherited?

Constructors are specific to the class and responsible for initializing the object's state, which may vary between superclasses and subclasses.

What determines the method executed during polymorphism?

The run-time type of the object, not the declared type, determines which method is executed.

Why override the toString() method?

To provide a human-readable string representation of an object's state, instead of the default memory reference.

What is the primary goal of Object-Oriented Programming?

To achieve abstraction, reduce code duplication, and increase code maintainability.

How does inheritance model real-world relationships?

By allowing classes to inherit properties and behaviors from more general classes, mirroring the structure of real-world objects and categories.

What is the significance of the Object class in Java?

It serves as the root of the class hierarchy, providing default implementations for essential methods and ensuring all objects share a common ancestor.

What is Dynamic Typing?

The method a computer executes on an object is determined by the run-time type of the object, not the declared type.

What is Static Typing?

The method a computer executes on an object is determined by the declared type of the object.

What is the benefit of using the super keyword?

The super keyword can be used to access the implementation in the Vehicle class, and then adds additional code to enable the car's cruise control system.