1020Chapter09

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following indicates that a class named ClassA class is a superclass of the ClassB class? -public class ClassB extends ClassA -public class ClassB implements ClassA -public class ClassA extends ClassB -public class ClassA implements ClassB

public class ClassB extends ClassA

Which of the following statements are true? (Select all that apply) -"class A extends B" means A is a subclass of B. -"class A extends B" means B is a subclass of A. -A subclass is usually extended to contain more functions and more detailed information than its superclass. -A superclass is a subset of a subclass.

"class A extends B" means A is a subclass of B. A subclass is usually extended to contain more functions and more detailed information than its superclass.

When the reserved word super is followed by a parenthesis, what does it indicate? -A call to a superclass method. -A call to a superclass constructor. -A call to a subclass method. -A call to a subclass constructor.

A call to a superclass constructor.

Which of the following statements about superclasses and subclasses is true? -A superclass is larger than its subclass. -A superclass inherits from a subclass. -A superclass extends a subclass. -A subclass extends a superclass.

A subclass extends a superclass.

When identifying the proper instance variables to use in the design of an inheritance hierarchy, how do you decide where in the hierarchy a variable should be placed? -Instance variables should only be declared in the superclass and then accessed using the super keyword. -Instance variables should only be declared in subclasses. -Instance variables that are common to all classes should be placed at the base (super class) of the hierarch . -Instance variables that are relevant to only one class should be placed at the base (super class) of the hierarchy.

Instance variables that are common to all classes should be placed at the base (super class) of the hierarch .

Consider the following code snippet: public class Auto extends Vehicle { . . . public Auto(int numberAxles) { super(numberAxles); } } What does this code do? -It invokes the constructor of the Vehicle class from within the constructor of the Auto class. -It invokes the constructor of the Auto class from within the constructor of the Vehicle class. -It invokes a private method of the Vehicle class from within a method of the Auto class. -This code will not compile.

It invokes the constructor of the Vehicle class from within the constructor of the Auto class.

What must a subclass method do in order to override a superclass method? -Must use a different method name. -Must use the same method name and the same parameter types. -Must use a different method name and the same parameter types. -Must use a different method name and different parameter types.

Must use the same method name and the same parameter types.

Consider the following code snippet: public class Motorcycle extends Vehicle { . . . public Motorcycle(int numberAxles) { super(numberAxles); //line #1 } } If the line marked "//line #1" was missing, which of these statements would be correct? -The Vehicle class constructor would invoke the constructor of the Motorcycle class with no parameters. -The Motorcycle class constructor would invoke the constructor of the Vehicle class with a parameter value of 0. -The Motorcycle class constructor would invoke the constructor of the Vehicle class with no parameters. -This code would not compile.

The Motorcycle class constructor would invoke the constructor of the Vehicle class with no parameters.

Which of the following statements is true about using the reserved word super to call a superclass constructor? -The call must use the keyword super followed by a period and a method name. -The call must use the keyword super with no arguments. -The call must be the last line of the subclass constructor. -The call must be the first line of the subclass constructor.

The call must be the first line of the subclass constructor.

In Java, if you forget to call a superclass constructor explicitly in the constructor of a subclass, what will happen? -The code will not compile. -The default constructor (the one with no arguments) of the superclass will be invoked automatically. -The subclass will be constructed without invoking a constructor for the superclass. -The subclass object will be given a value of null.

The default constructor (the one with no arguments) of the superclass will be invoked automatically.

Analyze the following code: public class Test { public static void main(String[] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } } class A { int i; public void m(int i) { this.i = i; } } class B extends A { void m(String s) { } } -The program has a compilation error, because m is overridden with a different signature in B. -The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B. -The program has a runtime error on b.i, because i is not accessible from b. -The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code: Square(double length) { GeometricObject(length); } } -The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally. -None of them -The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square. -The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

Which of the following statements about comparing objects is correct? -The purpose of the equals method is to compare whether two references are to the same object. -The purpose of the equals method is to compare whether two objects have the same contents. -The == operator is used to compare whether two objects have the same contents. -For objects other than Object, the equals method and the == operator always perform the same actions.

The purpose of the equals method is to compare whether two objects have the same contents.

Consider the following code snippet: public class Vehicle { . . . public void setVehicleAttributes() { . . . } } public class Auto extends Vehicle { . . . public void setVehicleAttributes() { . . . } } Which of the following statements is correct? -The subclass is shadowing a superclass method. -The subclass is overloading a superclass method. -The subclass is overriding a superclass method. -This code will not compile.

The subclass is overriding a superclass method.

What must a subclass do to modify a private superclass instance variable? -The subclass must simply use the name of the superclass instance variable. -The subclass must declare its own instance variable with the same name as the superclass instance variable. -The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable. -The subclass must have its own public method to update the superclass's private instance variable.

The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.

Consider the following code snippet: Employee anEmployee = new Programmer(); String emp = anEmployee.toString(); Assume that the Programmer class inherits from the Employee class, and only the Employee class has an implementation of the toString() method. Which of the following statements is correct? -The toString() method of the Object class will be used when this code is executed. -The toString() method of the Employee class will be used when this code is executed. -The toString() method of the String class will be used when this code is executed. -This code will not compile because there is no toString() method in the Programmer class.

The toString() method of the Employee class will be used when this code is executed.

Which keyword is used to create a subclass? -inherits -implements -interface -extends

extends

Given the following code, find the compile error? (Select all that apply) public class Test {public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Student x) { System.out.println(x.toString()); } } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } } class Person extends Object { public String toString() { return "Person"; } } m(new Person()) causes an error m(new GraduateStudent()) causes an error m(new Student()) causes an error m(new Object()) causes an error

m(new Object()) causes an error m(new Person()) causes an error

What is the term used for a subclass that defines a method with the same name as a method in its superclass, but with different parameter types? -implementing -inheriting -overriding -overloading

overloading

Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String s = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will s contain when this code is executed? -s will contain the values of the instance variables in programmer. -s will contain only the class name of the programmer object. -s will contain the class name of the programmer object followed by a hash code. -This code will not compile.

s will contain the class name of the programmer object followed by a hash code.

Which reserved word must be used to call a method of a superclass? -this -my -parent -super

super

Consider the following class hierarchy: public class Vehicle { private String type; public Vehicle(String type) { this.type = type; } public String displayInfo() { return type; } } public class LandVehicle extends Vehicle { public LandVehicle(String type) { super(type); } } public class Auto extends LandVehicle { public Auto(String type) { _________; } } Complete the code in the Auto class constructor to store the type data. -super(type); -LandVehicle(type); -Vehicle(type); -This cannot be done unless Auto declares an instance variable named type.

super(type);

Consider the following class hierarchy: public class Vehicle { private String type; public Vehicle(String type) { this.type = type; } public String displayInfo() { return type; } } public class LandVehicle extends Vehicle { public LandVehicle(String type) { . . . } } public class Auto extends LandVehicle { public Auto(String type) { . . . } public String displayAutoType() { return _____; } } Complete the code in the Auto class method named displayAutoType to return the type data. super(type); super.type; super.super.type; super.displayInfo()

super.displayInfo()

To test whether an object belongs to a particular type, use -the this reserved word. -the subclassOf reserved word. -the instanceof operator. -the equals method.

the instanceof operator.

If a subclass defines the same method name and the same parameter types for a method that appears in its superclass, which statement is true? -the subclass method overloads the superclass method. -the subclass method overrides the superclass method. -the subclass has implemented the method on behalf of the superclass. -a compiler error will occur.

the subclass method overrides the superclass method.


संबंधित स्टडी सेट्स

Social Studies Test November 31st

View Set

Chapter 22: The Child with Gastrointestinal Dysfunction Hockenberry: Wong's Essentials of Pediatric Nursing, 10th Edition Exam 2

View Set

Chapter 5: The Research Methods of Biopsychology (Part 1)

View Set