What is another name for the superclass
a) Parent
b) Mother
c) Nephew
d) Child
Parent
What is the subclass of a parent class called
a) Son
b) Daughter
c) Child
d) Dog
Child
What keyword can be used to call the methods of a superclass that may be overridden?
a) Major
b) Superior
c) Super
d) Parent
Super
public class Animal {
//makeSound is void method that prints “Animal sound” }
public class Dog extends Animal {
//makeSound is void method that prints “Bark” }
public class Test {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound(); }
}
What will be printed when the program runs?
Bark
public class Vehicle {
//Constructor signature → Vehicle(int speed)
//display method prints “Speed: ” + speed }
public class Car extends Vehicle {
//Constructor signature → Car(int speed, int horsepower)
//display method calls super.display()
//display method prints “Horsepower: ” + horsepower }
public class Test {
public static void main(String[] args) {
Car myCar = new Car(120, 300);
myCar.display(); } }
What is the output of the code?
Speed: 120
Horsepower: 300
public class Parent{
public void Massive(){
System.out.print(“Massive”);
} }
Create a simple subclass called Child that inherits from the Parent class and includes a void method named low that prints the phrase “Low”
public class Child extends Parent{
public void Low(){
System.out.print(“Low”);
}
}
What is the superclass for all classes in Java?
a) Class
b) Lang
c) Object
d) Util
c) Object
What is not inherited from superclass?
a) Instance variables
b) Constructors
c) Non-static methods
d) Static methods
b) Constructors
Assume S is the subclass of T. Declaring references of type T is MOST useful for which three types of declarations? (This is directly mentioned in the AP CSA Course Description.)
Formal parameters, Arrays, and ArrayLists
public class Animal {
//eat is public void method that prints “Animal is eating”
}
public class Cat extends Animal {
//eat is public void method that prints “Cat is eating”
//meow is public void method that prints “Cat says meow”
}
Which of the following lines (can be multiple correct answers) will compile and run without error?
A. Animal a = new Cat(); a.eat();
B. Animal a = new Cat(); a.meow();
C. Cat c = new Animal(); c.eat();
D. Cat c = new Cat(); c.meow();
A and D
public class Shape {
//getArea method returns double value of 0 }
public class Rectangle extends Shape {
//Constructor signature → Rectangle(double length, double width)
//getArea method returns double of length * width }
public class Circle extends Shape {
//Constructor signature → Circle(double radius)
//getArea method returns double of Math.PI*radius*radius }
public class ShapeTester {
public static void main(String[] args) {
Shape s1 = new Rectangle(5, 4); Shape s2 = new Circle(3); Rectangle r1 = new Rectangle(2, 6); System.out.println(s1.getArea()); System.out.println(s2.getArea()); System.out.println(r1.getArea()); } }
What will be the output when the main method of ShapeTester is executed?
20.00
28.27
12.00
public class Animal {
//makeSound method returns “Animal sound” }
public class Dog extends Animal {
//makeSound method returns “Woof” }
public class Cat extends Animal {
//makeSound method returns “Meow” }
public class AnimalTester {
public static void main(String[] args) {
Animal a1 = new Dog(); Animal a2 = new Cat(); Dog d1 = new Dog();
System.out.println(a1.makeSound()); System.out.println(a2.makeSound()); System.out.println(d1.makeSound()); } }
What will be the output when the main method of AnimalTester is executed?
Woof
Meow
Woof