CFU 9.1-9.5

Ace your homework & exams now with Quizwiz!

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(); obj2 & obj4 obj1, obj2, obj4 All of these are valid obj2, obj3, obj4

obj1, obj2, obj4

The super keyword can be used from the subclass for the following: To call the superclass constructor To call a public method in the superclass To refer to the superclass object All of these choices

All of these choices

From a design perspective, what goes in the subclass? Modified/Overridden methods Additional instance variables specific to the subclass Additional methods specific to the subclass All of these options

All of these options

When does a method override occur? Anytime we use the @Override on the line before the method in the subclass. Anytime we use the @Override on the line before the method in the superclass. Anytime we use the @Override on the line before the method in the subclass and the method signatures match. Anytime a subclass method signature matches that of the superclass

Anytime a subclass method signature matches that of the superclass

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? ArrayList<Dog> dogs = new ArrayList<Dog>(); ArrayList<Dog> dogs = new ArrayList<MixedDog>(); ArrayList<MixedDog> dogs = new ArrayList<Dog>(); ArrayList<MixedDog> dogs = new ArrayList<MixedDog>();

1: ArrayList<Dog> dogs = new ArrayList<Dog>();

If a class does not extend any class, what happens? Nothing. Not every class needs to have a superclass. The program will not compile and will throw an error. The class with the main method becomes the superclass. By default, we extend the Object class

By default, we extend the Object class

What types of things do we put in the Superclass? User input for the subclass Common Attributes and Behaviors Private methods only available for the subclass All of these

Common Attributes and Behaviors

Which of the following best describes a Subclass / Superclass relationship? Dog / Color Dog / Animal Dog / Tail Dog / Age

Dog / Animal

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

Is A

How does Java determine the order to execute methods between superclasses and subclasses? 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 first looks in a superclass for a method signature. If it is found there, it executes that method. Otherwise it checks the subclass methods. 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. Javs starts in the Object class and works its way down the hierarchy until it finds the correct method signature.

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

If a superclass constructor exists and a subclass doesn't explicitly call the that constructor, what happens? Java implicitly calls the no-argument superclass constructor Java doesn't create a superclass object The program will not compile and will generate an error. Java initializes all superclass variables with default values.

Java implicitly calls the no-argument superclass constructor

What is the result of this program when it is compiles and runs? The program will compile and run without a problem. Line 5 will error. Line 12 will error Line 20 will error Line 22 will error

Line 22 will error

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

True

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? Person[] people = new Person[10]; Person[] people = new Student,Worker[10]; Person[] people = new Person,Student,Worker[10]; It cannot be done. Arrays can only store one object type. Answered

Person[] people = new Person[10];

What is the definition of polymorphism? Polymorphism means that an instance of a class can have a different behavior depending on the constructor. Polymorphism means that a subclass can override the instance variables in the superclass. Polymorphism lets a method do different things depending on the type of the object it is called on. Polymorphism in Java means that different classes can have different methods.

Polymorphism lets a method do different things depending on the type of the object it is called on.

How does a Subclass call the Superclass constructor? Using the super keyword in a separate method within the subclass Using the Superclass name in a separate method within the subclass Using the super keyword in the first line of the subclass constructor. Using the Superclass name in the first line of the subclass constructor.

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

Why might we want to override a superclass method? Select the best option. We override methods so that we can see how a method is working within the class. We override methods because superclasses cannot correctly define most subclass behaviors. We override a method because a subclass needs to define a behavior in a different way than the superclass. We override methods to make Java run more efficiently.

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

Why do we use the @Override in our code? When we use it, the compiler checks that we are actually overriding the method we intended to. It is required to override a method. It tells Java to skip that method in the code. It tells Java to exit out of the code at this point.

When we use it, the compiler checks that we are actually overriding the method we intended to.

Does a Subclass need to have a constructor? No, it can inherit the constructor from the Superclass. Yes, it must always have an explicit constructor to take the parameters of the superclass. No, subclasses objects are part of the superclass object, so the superclass constructor is called. Yes, the subclass has its own constructor, either explicit or implicitly created.

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

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

extends

True or False: 'Is A' relationships are symmetrical. True False

false

Which of the following is the correct syntax to call the superclass constructor? super.constructor(parameters) super(parameters) (parameters) classname(parameters)

super(parameters)

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 */ } } super("Bark"); speak("Bark"); super.speak("Bark"); Animal.speak("Bark");

super.speak("Bark");


Related study sets

CYBR1-1.1 OSI Model - MC Format (N10-008)

View Set

3.001.2 Les pronoms relatifs WHO et THAT. Clique sur "cartes"

View Set

Government and Economics Unit 2 Quiz 2: Branches of Government

View Set

ITF Block 4: Unit 4 Domain Name System (DNS)

View Set

Physical Science - Ch. 4 comprehension check study practice

View Set

Pamela Paul - "You and Me and Pornography: How Porn Affects Relationships"

View Set