Module 09: Inheritance Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

9.1 Inheritance

What is a “Has A” Relationship?

A

Represent instance variables in our class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

9.1 Inheritance

What is a “Is A” Relationship?

A

A relationship where one class is a more specific example of another class

(not symmetrical)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

9.1 Inheritance

What is a superclass and how does it relate to a subclass?

A

📌 Extending a subclass from a superclass creates an “IS A” relationship from the subclass to the superclass

  • Create a hierarchy of classes that allow us to reuse common attributes and behaviors
  • A subclass uses the “IS A” Relationship signifying that it is a more specific example of the broader superclass
  • The keyword extends used to establish the relationship between a superclass and subclass
  • A class can only extend one superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

9.1 Inheritance

How do you create a class hierarchy?

A

public class Person {

}

public class Student extends Person {

}

  • Use keyword extends to create a subclass relationship
  • A class can only extend one superclass
  • “Person” = class name that is the superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

9.1 Inheritance

How do you create a multi-level class hierarchy?

A

public class Person {

}

public class Student extends Person {

}

public class HighSchoolStudent extends Student {

}

Extending a class that has been extended is okay

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

9.1 Inheritance

Question: 1

When we talk about two classes having a Superclass/Subclass relationship, which type of relationship do we have?

Is A

Has A

A

Is A

A subclass can be described using a ‘is a’ relationship, such as a Student is a Person

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

9.1 Inheritance

Question 2

Which keyword do we use to establish the inheritance relationship between a subclass and a superclass?

  1. implements
  2. public
  3. extends
  4. subclass
A
  1. extends
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

9.1 Inheritance

Question: 3

True or False: ‘Is A’ relationships are symmetrical.

True

False

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9.1 Inheritance

Question: 4

What types of things do we put in the Superclass?

  1. User input for the subclass
  2. Common Attributes and Behaviors
  3. Private methods only available for the subclass
  4. All of these
A
  1. Common Attributes and Behaviors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

9.2 Writing Constructors for Subclasses

Question: 5

Which of the following best describes a Subclass / Superclass relationship?

  1. Dog / Color
  2. Dog / Animal
  3. Dog / Tail
  4. Dog / Age
A
  1. Dog / Animal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

9.2 Writing Constructors for Subclasses

What are subclass constructors?

A

Don’t inherit constructor from the superclass

Need to create its own

  • Since constructors are not inherited, a subclass needs to create its own constructor
  • Superclass constructor should be called as the first line of the subclass constructor and the parameters to satisfy the superclass constructor should be passed
  • No call is made to the superclass constructor, Java inserts a call to the no-argument constructor
  • When no superclass is defined, the Object class is the superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

9.2 Writing Constructors for Subclasses

How does the subclass constructor call superclass?

A

Must call the parent constructor

When a subclass is created, a superclass object is also created

Subclass must make a call to the superclass constructor when it exists

  • To call the superclass constructor, use the keyword super on the first line of our subclass constructor, then pass it the required parameters
  • Actual parameter passed in the call to superclass and use the superclass constructor can use to initialize the object’s instance variables
  • If a no-argument constructor doesn’t exist, then the program will not compile without an explicit call to a constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

9.2 Writing Constructors for Subclasses

What are subclasses parameters?

A

Even though a subclass has to pass parameters to the superclass, they don’t need to take them as parameters for their constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

9.2 Writing Constructors for Subclasses

How are implicit calls to super constructor?

A

When non-argument student object is created, the no-argument Person class will be called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

9.2 Writing Constructors for Subclasses

What if we don’t extend any class?

A
  • Don’t extend any class, then by default we are extending Object which is the top of the Java class hierarchy
  • Object class constructor takes no arguments, so we don’t need to call it explicitly

Each subclass either implicitly or explicitly call a superclass constructor

  • Continues until Object constructor is called
  • All constructors are executed beginning with the Object constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

9.2 Writing Constructors for Subclasses

Question: 1

Does a Subclass need to have a constructor?

  1. No, it can inherit the constructor from the Superclass.
  2. Yes, it must always have an explicit constructor to take the parameters of the superclass.
  3. No, subclasses objects are part of the superclass object, so the superclass constructor is called.
  4. Yes, the subclass has its own constructor, either explicit or implicitly created.
A

4. Yes, the subclass has its own constructor, either explicit or implicitly created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

9.2 Writing Constructors for Subclasses

Question: 2

How does a Subclass call the Superclass constructor?

  1. Using the super keyword in a separate method within the subclass
  2. Using the Superclass name in a separate method within the subclass
  3. Using the super keyword in the first line of the subclass constructor.
  4. Using the Superclass name in the first line of the subclass constructor.
A

3. Using the super keyword in the first line of the subclass constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

9.2 Writing Constructors for Subclasses

Question: 3

If a superclass constructor exists and a subclass doesn’t explicitly call the that constructor, what happens?

  1. Java implicitly calls the no-argument superclass constructor
  2. Java doesn’t create a superclass object
  3. The program will not compile and will generate an error.
  4. Java initializes all superclass variables with default values.
A

1. Java implicitly calls the no-argument superclass constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

9.2 Writing Constructors for Subclasses

Question: 4

If a class does not extend any class, what happens?

  1. Nothing. Not every class needs to have a superclass.
  2. The program will not compile and will throw an error.
  3. The class with the main method becomes the superclass.
  4. By default, we extend the Object class
A

4. By default, we extend the Object class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

9.2 Writing Constructors for Subclasses

Question: 5

True or False: A subclass can be extended by another class to become a superclass.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

9.3 Overriding Methods

What is Method Override?

A

💻 Method Overrides allow a subclass to redefine a method instead of inheriting that method form superclass

When a public method in a subclass has the same method signature as a public method

//Person Class:

public String toString(){

return name + “ was born “ + birthday;

}

//Student Class:

public String toString(){

return super.getName() + “ is in grade “ + grade;

}

  • Both methods have sam name and input parameters
  • Person objects use one method and Student objects use their method

Any method called by object needs to be defined someplace in class hierarchy

Subclass inherits all public methods from the superclass. It can then add additional variables and/or methods, or modify them with an override

Methods overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

9.3 Overriding Methods

What is the difference between override and overload?

A

Different implementations

Method override:

  • Methods in Superclass/Subclass
  • Methods have the same name and same parameters

Method overload:

  • Methods in the same class
  • Methods have same name, but different parameters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

9.3 Overriding Methods

What is override notation?

A

Use @Override notation:

@Override

//Student Class: public String toString(){

return super.getName() + “ is in grade “ + grade;

}

  • @Override notation not required if it is not included, a method with the same signature as the superclass will still be overriden

Use @Override notation is best practiced for…

  1. It helps with debugging. When you use @Override the compiler double checks that you correctly override the method from the superclass
  2. It makes your code more readable since it becomes clear that you are overriding a superclass method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

9.3 Overriding Methods

What are class hierarchy considerations?

A

💻 A subclass is usually designed to have modified (overridden) methods, or additional methods and instance variables

  • When calling methods for an object, Java first looks in that object class. If it defined in that class, use that method, otherwise it will look to the superclass for the method
  • Any method called must be defined within its own class or superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

9.3 Overriding Methods

Question: 1

When does a method override occur?

  1. Anytime we use the @Override on the line before the method in the subclass.
  2. Anytime we use the @Override on the line before the method in the superclass.
  3. Anytime we use the @Override on the line before the method in the subclass and the method signatures match.
  4. Anytime a subclass method signature matches that of the superclass
A

4. Anytime a subclass method signature matches that of the superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

9.3 Overriding Methods

Question: 2

Why might we want to override a superclass method? Select the best option.

  1. We override methods so that we can see how a method is working within the class.
  2. We override methods because superclasses cannot correctly define most subclass behaviors.
  3. We override a method because a subclass needs to define a behavior in a different way than the superclass.
  4. We override methods to make Java run more efficiently.
A

3. We override a method because a subclass needs to define a behavior in a different way than the superclass

( A superclass may need to define a behavior and the subclass also needs that behavior, but in a different way.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

9.3 Overriding Methods

Question: 3

How does Java determine the order to execute methods between superclasses and subclasses?

  1. Java first looks in a subclass for a method signature. If it is found there, it executes that method. Otherwise it checks the superclass methods
  2. Java first looks in a superclass for a method signature. If it is found there, it executes that method. Otherwise it checks the subclass methods.
  3. Java first looks in a superclass for a method signature. If it is found there, it executes that method after checking the subclass for an override.
  4. Javs starts in the Object class and works its way down the hierarchy until it finds the correct method signature.
A
  1. Java first looks in a subclass for a method signature. If it is found there, it executes that method. Otherwise it checks the superclass methods

(Java starts at the bottom and works its way up.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

9.3 Overriding Methods

Question: 4

Why do we use the @Override in our code?

  1. When we use it, the compiler checks that we are actually overriding the method we intended to.
  2. It is required to override a method.
  3. It tells Java to skip that method in the code.
  4. It tells Java to exit out of the code at this point.
A
  1. When we use it, the compiler checks that we are actually overriding the method we intended to.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

9.3 Overriding Methods

Question: 5

From a design perspective, what goes in the subclass?

  1. Modified/Overridden methods
  2. Additional instance variables specific to the subclass
  3. Additional methods specific to the subclass
  4. All of these options
A
  1. All of these options
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

9.4 “super” Keyword

What is the super keyword?

A
  • Use keyword super
  • Call to supercall constructor
  • Call with the method name and passing appropriate parameters

public Student(String name, String birthday, int grade){

super(name, birthday);

this.grade = grade;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

9.4 “super” Keyword

How is the super and this keyword similar?

A
  1. this refers to the current object and we can call methods using: this.methodName(parameters);
  2. super refers to the superclass object and we can call methods using super.methodName(parameters);
32
Q

9.4 “super” Keyword

Question: 1

The super keyword can be used from the subclass for the following:

  1. To call the superclass constructor
  2. To call a public method in the superclass
  3. To refer to the superclass object
  4. All of these choices
A
  1. All of these choices
33
Q

9.4 “super” Keyword

Question: 2

Which of the following is the correct syntax to call the superclass constructor?

  1. super.constructor(parameters)
  2. super(parameters)
  3. (parameters)
  4. classname(parameters)
A

2. super(parameters)

34
Q

9.4 “super” Keyword

Question: 3

Given the following code, what is the correct way for the subclass to call the speak method in the Animal class?

public class Animal {

public void speak(String sound) {

System.out.println(sound);

}

}

public class Dog extends Animal {

public void bark() {

/* Missing code */

}

}

  1. super(“Bark”);
  2. speak(“Bark”);
  3. super.speak(“Bark”);
  4. Animal.speak(“Bark”);
A
  1. super.speak(“Bark”);
35
Q

9.5 Creating Reference Using Inheritance

What is polymorphism?

A

💻 Polymorphism is the capability of a method to do different things depending on the object it is acting upon.

  • From Greek poly meaning many and morph meaning forms
  • In Java and other Object Oriented Programming languages, the concept of polymorphism means an object can take on different forms depending on its implementation
  • Java can call the correct method even when an object is disguised as a more generic reference type

Student student1 = new Student();

  1. Student student1 → Reference Type (Variable declaration)
  2. new Student(); → Object Type (Variable instantiation)
36
Q

9.5 Creating Reference Using Inheritance

How can a superclass be used as a reference type?

A

Use a Superclass as a reference type T, then we can create an object as either the Superclass T, or any Subclass C.

Person person1 = new Person();

  1. Person person1 → Reference Type T
  2. new Person(); → Object Type T

Person person1 = new Student();

  1. Person person1 → Reference Type T
  2. new Student(); → Object Type S
37
Q

9.5 Creating Reference Using Inheritance

Why do we declare using a Superclass?

A

Polymorphism allows flexibility when creating with a Superclass reference type

  • Can use type T as a formal parameter in a method, then pass any object of type T or S
  • Can create Arrays and Arraylist of a T and store any T or S object
38
Q

9.5 Creating Reference Using Inheritance

How can T be used as a formal parameter?

A

Our method Formal Parameters take our reference type:

39
Q

9.5 Creating Reference Using Inheritance

Question: 1

What is the definition of polymorphism?

  1. Polymorphism means that an instance of a class can have a different behavior depending on the constructor.
  2. Polymorphism means that a subclass can override the instance variables in the superclass.
  3. Polymorphism lets a method do different things depending on the type of the object it is called on.
  4. Polymorphism in Java means that different classes can have different methods.
A
  1. Polymorphism lets a method do different things depending on the type of the object it is called on.
40
Q

9.5 Creating Reference Using Inheritance

Question: 2

Given the following class structure:

public class Person {

/* implementation not shown */

}

public class Student extends Person {

/* implementation not shown */

}

Which of the following objects can be created?

Person obj1 = new Student();

Student obj2 = new Student();

Student obj3 = new Person();

Person obj4 = new Person();

  1. obj2 & obj4
  2. obj1, obj2, obj4
  3. All of these are valid
  4. obj2, obj3, obj4
A
  1. obj1, obj2, obj4
41
Q

9.5 Creating Reference Using Inheritance

Question: 3

If the Student class and Worker class both extend the Person class, how can I create an array to store 10 objects of any of the three classes?

  1. Person[] people = new Person[10];
  2. Person[] people = new Student,Worker[10];
  3. Person[] people = new Person,Student,Worker[10];
  4. It cannot be done. Arrays can only store one object type.
A
  1. Person[] people = new Person[10];
42
Q

9.5 Creating Reference Using Inheritance

Question: 4

Given the following code:

The following code segment appears in a method in another class.

Dog clover = new Dog(“Beagle”); // Line 5

Dog butch = new MixedDog(“Beabull”, “Beagle”, “Bulldog”); // Line 12

MixedDog bailey = new MixedDog(“Pitsky”, “Pitbull”, “Husky”); // Line 20

MixedDog macey = new Dog (“Chug”); // Line 22

What is the result of this program when it is compiles and runs?

  1. The program will compile and run without a problem.
  2. Line 5 will error.
  3. Line 12 will error
  4. Line 20 will error
  5. Line 22 will error
A
  1. Line 22 will error
43
Q

9.5 Creating Reference Using Inheritance

Question: 5

Given the following code:

public class Dog {

/* implementation not shown */

}

public class MixedDog extends Dog {

/* implementation not shown */

}

What is the correct way to create an array list to store both MixedDog objects and Dog objects?

  1. ArrayList dogs = new ArrayList();
  2. ArrayList dogs = new ArrayList();
  3. ArrayList dogs = new ArrayList();
  4. ArrayList dogs = new ArrayList();
A
  1. ArrayList dogs = new ArrayList();
44
Q

9.6 Polymorphism

How does polymorphism work at compile time?

A

At the time the program gets compiled, methods in or inherited by the declared type determine the correctness of a non-static method.

Ex:

  • Person person1 = new Student*
  • person1.getName();*
  • For this program to compile - getName needs to be a method defined in the Person class.
  • If it is only defined in the student class, it will not compile
45
Q

9.6 Polymorphism

How does polymorphism work at run time?

A

At the time the program runs, the method in the actual object type gets executed.

If the method doesn’t exist there, Java looks at the superclass.

Ex:

  • Person person1 = new Student*
  • person1.getName();*
  • When the program runs, Java look for the getName() method in Student first. If it does not find it, it looks in the parent class
46
Q

9.6 Polymorphism

Question: 1

In order for a program to compile, any method that an object uses …

  1. Must be defined in the class where the object is declared.
  2. Must be defined in the class where the object is instantiated.
  3. Must be defined in both the class where the object is declared and where it is instantiated.
  4. Doesn’t have to be defined anywhere. Java only checks for the method at run time.
A
  1. Must be defined in the class where the object is declared
47
Q

9.6 Polymorphism

Question: 2

When a program runs, how does Java choose which methods to run?

  1. It looks first in the declared class, then checks the superclasses if the method is not found.
  2. It looks first in the declared class, then checks the subclasses if the method is not found.
  3. It looks first in the instantiated class, then checks the superclasses if the method is not found.
  4. It looks first in the instantiated class, then checks the subclasses if the method is not found.
A

3. It looks first in the instantiated class, then checks the superclasses if the method is not found

48
Q

9.6 Polymorphism

Question: 3

Given the following code:

public class Animal {

public void speak(String sound) {

System.out.println(sound);

}

}

public class Dog extends Animal {

public void bark() {

super.speak(“Bark”);

}

}

Which of the following lines would generate a compile time error when placed in a main method?

  1. Animal karel = new Dog();
    karel. speak(“Woof”);
  2. Animal karel = new Animal();
    karel. speak(“Woof”);
  3. Animal karel = new Dog();
    karel. bark();
  4. Dog karel = new Dog();
    karel. bark();
A
  • *3.** Animal karel = new Dog();
    karel. bark();
49
Q

9.6 Polymorphism

Question: 4

Given the following code:

public class Animal {

public void speak(String sound) {

System.out.println(sound);

}

}

public class Dog extends Animal {

public void bark() {

super.speak(“Bark”);

}

}

Which of the following lines would generate a run time error when placed in a main method?

  1. Dog karel = new Dog();
    karel. bark();
  2. Animal karel = new Animal();
    karel. speak(“Woof”);
  3. Animal karel = new Dog();
    karel. bark();
  4. None of these will generate a run time error.
A

4. None of these will generate a run time error

50
Q

9.7 Object Superclass

What are object class methods?

A
  • 11 methods in Object class can be inherited by any object
  • Object class is superclass of all other classes in Java

(1) boolean equals(Object other)

(2) String toString()

51
Q

9.7 Object Superclass

What is boolean equals(Object other)?

A

Object obj1 = new Object();

Object obj2 = new Object();

obj1. equals(obj2);
* Returns true is obj1 and obj2 are represented by the same object

52
Q

9.7 Object Superclass

What is String toString()?

A

Object obj1 = new Object();

System.out.println(obj1);

//Returns memory address: java.lang.Object@d2344

  • Override to print a String:

Person person1 = new Person(“Steve”);

System.out.println(person1); //returns “Steve”

53
Q

9.7 Object Superclass

Question: 1

Every object created in Java can use the Object class methods since the object class is at the top of the hierarchy. (True or False)

A

True

54
Q

9.7 Object Superclass

Question: 2

Approximately how many methods are contained in the Object class for us to use?

1

11

21

51

A

11

55
Q

9.7 Object Superclass

Question: 3

Which of the following methods would you find in the Object class?

  1. comparesTo
  2. toString
  3. override
  4. delete
A
  1. toString
56
Q

9.7 Object Superclass

Question: 4

Methods in the Object class are useful as is and seldom need to be overridden.

(True or False)

A

False

57
Q

9.7 Object Superclass

Question: 5

What does the Object class equals() method compare? Choose the best answer.

  1. It compares the actual values in two variables to see if they are the same.
  2. It doesn’t actually compare anything on this level. You must override it to use it.
  3. It compares two variable names to determine if they are equal.
  4. It compares whether two variables are represented by the same object.
A
  1. It compares whether two variables are represented by the same object
58
Q

Module 09 Inheritance Exam

Question: 01

The Java Classes Skeleton, Spider, and Zombie all extend the Java Class Monster. The Monster Class is defined below.

What variables and methods do the Skeleton, Spider, and Zombie Classes inherit?

  1. Everything
  2. Only the variables
  3. Only the method move
  4. Everything except the constructor
A
  1. Only the method move
59
Q

Module 09 Inheritance Exam

Question: 2

Given the definitions of class A and class B

what is the output of this code

B obj = new B();

System.out.println(obj.i + “ “ + obj.j);

  1. 1 2
  2. 2 1
  3. Runtime Error
  4. Compilation Error
A

1. 1 2

60
Q

Module 09 Inheritance Exam

Question: 3

Given the class definitions of Vector2D and Vector3D below:

Which of the constructors that follow would be valid to put in the Vector3D class?

Possible constructors for Vector3D:

I.

public Vector3D() {}

II.

public Vector3D(int x, int y, int z)

{

super(x,y); this.z = z;

}

III.

public Vector3D(int x, int y)

{

super. x = x;
super. y = y;
this. z = 0;

}

  1. I, II, and III
  2. I and II
  3. II and III
  4. Only III
  5. Only II
A

2. I and II

61
Q

Module 09 Inheritance Exam

Question: 4

Based on this code snippet

what does this program output?

**Shape shape1 = new Shape();
Shape shape2 = new Rectangle();
Shape shape3 = new Square();
Shape shape4 = new Circle();**

System.out.println(shape1.getShapeName()); System.out.println(shape2.getShapeName()); System.out.println(shape3.getShapeName()); System.out.println(shape4.getShapeName());

  1. Shape
    Rectangle
    Rectangle
    Oval
  2. Shape
    Rectangle
    Square
    Circle
  3. This code will error
  4. Shape
    Rectangle
    Rectangle
    Circle
  5. Shape
    Shape
    Shape
    Shape
A
  1. Shape
    Rectangle
    Rectangle
    Circle
62
Q

Module 09 Inheritance Exam

Question: 5

Consider the following class declarations

Which of the following will print City Schools?

I.

Student jackson = new Student(); jackson.printSchool();

II.

HSStudent jackson = new HSStudent(); jackson.printSchool();

III.

MSStudent jackson = new MSStudent(); jackson.printSchool();

  1. I only
  2. I, II only
  3. II, III only
  4. I, II, III
A

2. I, II only

63
Q

Module 09 Inheritance Exam

Question: 6

Consider the following class declarations

Which of the following statements will cause a compile-time error?

  1. Parent person = new Child();
    person. grade();
  2. Child person = new Child();
    person. grade();
  3. Parent person = new Parent();
    person. age();
  4. Parent person = new Child();
    person. age();
A
  1. Parent person = new Child();
    person. grade();
64
Q

Module 09 Inheritance Exam

Question: 7

Consider the following class declarations

A code segment located in a different class produced the following output:

Centennial park with a soccer field

Which of the following code segments will produce that output?

I.

Park cent = new Park(“Centennial”, “soccer”); System.out.println(cent);

II.

Park cent = new Field(“Centennial”, “soccer”); System.out.println(cent);

III.

Field cent = new Field(“Centennial”, “soccer”); System.out.println(cent);

  1. I Only
  2. II Only
  3. III only
  4. II and III Only
  5. I, II, III
A
  1. II and III Only
65
Q

Module 09 Inheritance Exam

Question: 8

Consider the following class declarations

The following line of code appears in a test class:

ArrayList cars = new ArrayList();

Which of the following objects can be added to the ArrayList?

**I. Car leaf = new ElectricCar();
II. Car mustang = new Car();
III. ElectricCar model3 = new ElectricCar();**
  1. I Only
  2. I and II only
  3. I and III only
  4. III only
  5. I, II, III
A
  1. I, II, III
66
Q

Module 09 Inheritance Exam

Question: 9

Which of the following is the best example of a Superclass / Subclass relationship?

  1. Balloon / Color
  2. Team / Mascot
  3. Student / Grade
  4. Shirt / Button
  5. Fruit / Banana
A
  1. Fruit / Banana
67
Q

Module 09 Inheritance Exam

Question: 10

Given the following class structure, which class is on the top of the hierarchy?

  1. Child
  2. Object
  3. parent
  4. Grandparent
  5. Grandchild
A
  1. Object
68
Q

Module 09 Inheritance Exam

Question: 11

Given the following class declaration

If 4 BaseballTeam objects are created as such:

**BaseballTeam one = new BaseballTeam("Athletics", "Oakland");
BaseballTeam two = new BaseballTeam("Athletics", "Kansas City");
BaseballTeam three = new BaseballTeam("Athletics", "Oakland");
BaseballTeam four = two;**

Which of the following will print true?

I. System.out.println(one.equals(three));

II. System.out.println(two.equals(four));

III. System.out.println(one == three);

IV. System.out.println(two == four);

  1. I and II only
  2. III and IV only
  3. II only
  4. III Only
  5. I, II, and IV Only
A
  1. I, II, and IV Only
69
Q

Module 09 Inheritance Exam

Question: 12

Given the following class declarations

Which of the following will print Los Angeles Dodgers?

1. Team dodgers = new BaseballTeam("Dodgers", "Los Angeles");
 System.out.println(dodgers);
2. Team dodgers = new BaseballTeam("Dodgers", "Los Angeles");
 dodgers.toString();
3. Team dodgers = new Team("Dodgers", "Los Angeles");
 dodgers.toString();
4. Team dodgers = new Team("Dodgers", "Los Angeles");
 System.out.println(dodgers);
5. All of these will print correctly
A
1. Team dodgers = new BaseballTeam("Dodgers", "Los Angeles");
System.out.println(dodgers);
70
Q

Module 09 Inheritance Exam

Question: 13

Which of the following objects can be passed as a parameter so that the code compiles and runs?

**I. OnlineCompany abc = new StartUp();
II. StartUp abc = new StartUp();
III. Company abc = new OnlineCompany();**
  1. I only
  2. II Only
  3. I and II only
  4. I and III only
  5. I, II, and III
A
  1. I and II only
71
Q

Module 09 Inheritance Exam

Question: 14

Does the Child printName method successfully override the Parent method?

  1. Yes, the method is overridden correctly.
  2. No, the method is not overridden correctly because it is missing @Override before the method.
  3. No, the method is not overridden correctly it has a different method signature.
  4. No, the method is not overridden correctly because the code in the method is different.
A
  1. Yes, the method is overridden correctly
72
Q

Module 09 Inheritance Exam

Question: 15

I only

II only

III Only

II and III only

I, II, and III

A

II and III only

73
Q

Module 09 Inheritance Exam

Question: 16

Which of the following best describes what will happen when the code is executed?

  1. This code will create a superclass object, but the make variable will be null.
  2. This code will create a superclass object with the make variable set to Tesla from the Subclass class name.
  3. This code will not execute since a call to the Superclass constructor is not made.
  4. This code will create a superclass object by calling the no-argument Car constructor.
A
  1. This code will create a superclass object by calling the no-argument Car constructor.
74
Q

Module 09 Inheritance Exam

Question: 17

Given the following code:

public int addNumbers(int one, int two)

Which method will correctly override this method when placed in a subclass?

I. public int addNumbers(int one, int two, int three)
II. public int addNumbers(int one, int two)
III. public int addNumbers(int a, int b)
IV. public int addNumbers()

I and IV only

II and III only

III only

II only

A

II and III only

75
Q

Module 09 Inheritance Exam

Question: 18

What goes in the missing code if the desired return is a square of the number?

  1. return super.multiply(num1, num2);
  2. return super.multiply(number);
  3. return super.multiply(number, number);
  4. return super.multiply(num1);
A
  1. return super.multiply(number, number);
76
Q

Module 09 Inheritance Exam

Question: 19

What goes in the missing code if the desired return is the following (as an example):
This Tree is not an evergreen fruit tree

  1. return super; return “ fruit tree”;
  2. return super() + “ fruit tree”;
  3. System.out.println(super);
    return “ fruit tree”;
  4. return super.toString() + “ fruit tree”;
A
  1. return super.toString() + “ fruit tree”;
77
Q

Module 09 Inheritance Exam

Question: 20

What is the correct implementation for the subclass constructor?

  1. super(evergreen);
    this.fruit = fruit;
  2. evergreen = evergreen;
    fruit = fruit;
  3. this.fruit = fruit;
    super(evergreen);
  4. super.evergreen = evergreen;
    this.fruit = fruit;
  5. Continue
A
  1. super(evergreen);
    this. fruit = fruit;