Quiz 6
Consider the following code snippet, which is meant to override the equals() method of the Object class: public class Coin { . . . public boolean equals(Coin otherCoin) { . . . } . . . } What is wrong with this code? A class cannot override the equals() method of the Object class. The equals() method must be declared as private. A class cannot change the parameters of a superclass method when overriding it. There is nothing wrong with this code.
A class cannot change the parameters of a superclass method when overriding it.
Consider the classes shown below: public class Parent { public void doSomething() // method 1 { /* Implementation not shown */ } } public class Child extends Parent { public void doSomething(int n) // method 2 { /* Implementation not shown */ } public void doSomething() // method 3 { /* Implementation not shown */ } } If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Child kid = new Child(); Methods 1 and 2 only Method 2 only Methods 2 and 3 only Methods 1, 2, and 3
Methods 2 and 3 only
Consider the following class hierarchy: public final class Shape { private String mycolor; public Shape(String mycolor) { this.type = mycolor; } public String getColor() { return mycolor; } } public class Triangle extends Shape { public Triangle(String mycolor) { super(mycolor); } } } What is wrong with this class hierarchy definition? Nothing is wrong with the code. There should be more subclasses of the Shape class than just Triangle. There cannot be any subclasses of the Shape class. It is not possible to use super in the Triangle constructor.
There cannot be any subclasses of the Shape class.
In Java, two methods can have the same name provided that what is true? They cannot be defined in the same class. They must differ in their parameter types. One must be private and the other public. They must be related to each other through inheritance.
They must differ in their parameter types.
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 of the hierarchy. Instance variables that are relevant to only one class should be placed at the base of the hierarchy.
Instance variables that are common to all classes should be placed at the base of the hierarchy.
Consider the classes shown below: public class Parent { public void doSomething(){/* Implementation not shown */} } public class Child extends Parent { public void doAnotherThing(){/* Implementation not shown */} } Which lines in the following code will compile without error? Child kid = new Child(); kid.doSomething(); // line 1 kid.doAnotherThing(); // line 2 Line 1 only Line 2 only Lines 1 and 2 Neither line will compile without error
Lines 1 and 2
Consider the classes shown below: public class Parent { public void doSomething() // method 1 { /* Implementation not shown */ } } public class Child extends Parent { public void doSomething(int n) // method 2 { /* Implementation not shown */ } public void doSomething() // method 3 { /* Implementation not shown */ } } If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Parent kid = new Child(); Method 1 only Methods 2 and 3 only Methods 1 and 2 only Methods 1, 2, and 3
Method 1 only
Consider the classes shown below: public class Parent { private int value = 100; public int getValue() { return value; } } public class Child extends Parent { private int value; public Child(int number) { value = number; } public int getValue() { return value; } } What is the output of the following lines of code? Child kid1 = new Child(-14); Child kid2 = new Child(21); System.out.println(kid1.getValue() + " " + kid2.getValue()); -14 21 21 21 21 100 100 100
-14 21
Consider the classes shown below: public class Parent { public int getValue() { return 24; } public void display() { System.out.print(getValue() + " "); } } public class Child extends Parent { public int getValue() { return -7; } } Using the classes above, what is the output of the following lines of code? Parent kid = new Child(); Parent adult = new Parent(); kid.display(); adult.display(); -7 24 24 24 -7 -7 24 -7
-7 24
Consider the following code snippet: public class Employee { . . . public void setDepartment(String deptName) { . . . } } public class Programmer extends Employee { . . . public void setProjectName(String projName) { . . . } public void setDepartment(String deptName) { . . . } } Which of the following statements is correct? An object of type Programmer can call the setDepartment method of the Employee class on itself. An object of type Employee can call the setProjectName method on itself. The Employee class's setDepartment method overrides the Programmer class's setDepartment method. An object of type Employee can call the setDepartment method of the Programmer class on itself.
An object of type Programmer can call the setDepartment method of the Employee class on itself.
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: Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); Assume that the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type. The process for determining which class's moveForward method to execute is called ____. inheritance disambiguation. inheritance hierarchy. dynamic inheritance. dynamic method lookup.
dynamic method lookup.
With a few exceptions, what access should instance variables of classes always have? final private public protected
private
Suppose the class Message is partially defined as shown below: public class Message { private String value; public Message(String initial) { value = initial; } public String getMessage() { return value; } } A subclass of Message, ExcitedMessage, is defined that will behave like Message, except that it will add two exclamation points to the end of the message. Sample code that uses ExcitedMessage is shown below. ExcitedMessage greeting = new ExcitedMessage("Hello"); System.out.print(greeting.getMessage());// will print "Hello!!" Which ExcitedMessage constructor will give this behavior? public ExcitedMessage(String line) { super(line + "!!"); } ---------------------------------------- public ExcitedMessage(String line) { value = line + "!!"; } ---------------------------------------- public ExcitedMessage(String line) { line = line + "!!"; super(line); } ---------------------------------------- public ExcitedMessage(String line) { new Message(line + "!!"); }
public ExcitedMessage(String line) { super(line + "!!"); }
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); super(super(type)); super.super(type); This cannot be done unless Auto declares an instance variable named type.
super(type);
Consider the following code snippet: Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); Assume that the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type. What determines which class's moveForward method is to be executed? the actual object type. the variable's type. the hierarchy of the classes. it is not possible to determine which method is executed.
the actual object type.
What does the getClass method do? Returns an object that describes a class and its properties. Returns a string that contains the instance variables of a class. Returns an object that describes all subclasses of a given object. Returns an object that describes all superclasses of a given object.
Returns an object that describes a class and its properties.
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) { super(type); } } You have written a program to use these classes, as shown in the following code snippet: public class VehicleTester { public static void main(String[] args) { Auto myAuto = new Auto("sedan"); System.out.println("MyAuto type = " + ______); } } Complete the code in this program snippet to correctly display the auto's type. myAuto.displayInfo() myAuto.super.displayInfo() myAuto.super.super.displayInfo() This cannot be done unless the Auto class overrides the displayInfo method.
myAuto.displayInfo()
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
Suppose the abstract class Message is defined below public abstract class Message { private String value; public Message(String initial) { value = initial; } public String getMessage() { return value; } public abstract String translate(); } A concrete subclass of Message, called FrenchMessage, is defined. Which methods must FrenchMessage define? translate() only getMessage() only The FrenchMessage constructor and translate() only The FrenchMessage constructor, getMessage(), and translate()
translate() only
Which of the following statements about abstract methods is true? An abstract method has a name, parameters, and a return type, but no code in the body of the method. An abstract method has parameters, a return type, and code in its body, but has no defined name. An abstract method has a name, a return type, and code in its body, but has no parameters. An abstract method has only a name and a return type, but no parameters or code in its body.
An abstract method has a name, parameters, and a return type, but no code in the body of the method.
Consider the following code snippet: double salary = 45000.00; String sal = "Current salary is " + salary; Which of the following statements is correct? The toString() method of the Object class is being used to set the value of salary. The toString() method of the Double class is being used to set the value of salary. No toString() method is being used to set the value of salary. This code will not compile.
No toString() method is being used to set the value of salary.
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.
When declared as protected, which statement is true about the access to data in an object? The data is accessible only by that class's methods and by all of its subclasses The data is accessible only by that class's methods, by all of its subclasses, and by methods in classes within the same package. The data is accessible only by that class's methods. The data is accessible by any class.
The data is accessible only by that class's methods, by all of its subclasses, and by methods in classes within the same package.
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.
Consider the following code snippet: Employee anEmployee = new Programmer(); anEmployee.increaseSalary(2500); If the Programmer class inherits from the Employee class, and only the Employee class has an implementation of the increaseSalary method, which statement is correct? The increaseSalary method call will cause a run-time error. The increaseSalary method of the Employee class will be executed. The Programmer class is required to provide an implementation of the increaseSalary method. Programmer objects must be cast to Employee objects before the method call can me made.
The increaseSalary method of the Employee class will be executed.
Which of the following statements about classes is true? You can create an object from a concrete class, but not from an abstract class. You can create an object from an abstract class, but not from a concrete class. You cannot have an object reference whose type is an abstract class. You cannot create subclasses from abstract classes.
You can create an object from a concrete class, but not from an abstract class.
Consider the Counter class below. public class Counter { public int count = 0; public int getCount() { return count; } public void increment() { count++; } } Using the class above and the variables declared below, what is the value of num1.equals(num2)? Counter num1 = new Counter(); Counter num2 = new Counter(); true false nothing since equals is not defined for Counter a syntax error
false
Consider the following code snippet: public class Coin { private String name; . . . public boolean equals(Object otherCoin) { return name.equals(otherCoin.name); } . . . } What is wrong with this code? The return statement should use the == operator instead of the equals method. The parameter in the equals method should be declared as Coin otherCoin. otherCoin must be cast as a Coin object before using the equals method. There is nothing wrong with this code.
otherCoin must be cast as a Coin object before using the equals method.
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
Which of the following statements about classes is true? You can create an object from a concrete class, but not from an abstract class. You can create an object from an abstract class, but not from a concrete class. You cannot have an object reference whose type is an abstract class. You cannot create subclasses from abstract classes.
You can create an object from a concrete class, but not from an abstract class.
Consider the following code snippet: Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); If the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type, which statement is correct? The moveForward method of the Auto class will be executed. The moveForward method of the Vehicle class will be executed. You must specify in the code which class's moveForward method is to be used. It is not possible to determine which class's method is called.
The moveForward method of the Auto class will be executed.
Consider the following code snippet: public class Vehicle { private String manufacturer; . . . public void setVehicleClass(double numberAxles) { . . . } } If a Motorcycle class is created as a subclass of the Vehicle class, which of the following statements is correct? A Motorcycle object inherits and can directly use both the instance variable manufacturer and the method setVehicleClass. A Motorcycle object inherits and can directly use the instance variable manufacturer but not the method setVehicleClass. A Motorcycle object inherits but cannot directly use either the instance variable manufacturer or the method setVehicleClass. A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
Which of the following is true regarding subclasses? A subclass has access to private instance variables of its superclass. A subclass does not have access to public instance variables of its superclass. A subclass must specify the implicit parameter to use methods inherited from its superclass. A subclass has no access to private instance variables of its superclass.
A subclass has no access to private instance variables of its superclass.
Consider the following code snippet: public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Motorcycle extends Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } Which of the following statements is correct? The Motorcycle class's setVehicleClass method overrides the Vehicle class's setVehicleClass method. The Vehicle class's setVehicleClass method overrides the Motorcycle class's setVehicleClass method. The Motorcycle class's setVehicleClass method overloads the Vehicle class's setVehicleClass method. The Vehicle class's setVehicleClass method overloads the Motorcycle class's setVehicleClass method.
The Motorcycle class's setVehicleClass method overrides the Vehicle class's setVehicleClass method.
Consider the following code snippet: Vehicle aVehicle = new Auto(4,"gasoline"); String s = aVehicle.toString(); Assume that the Auto class inherits from the Vehicle class, and neither 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 String class will be used when this code is executed. This code will not compile because there is no toString() method in the Vehicle class. This code will not compile because there is no toString() method in the Auto class.
The toString() method of the Object class will be used when this code is executed.
If a class has an abstract method, which of the following statements is NOT true? You can construct an object from this class. You can have an object reference whose type is this class. You can inherit from this class. All non-abstract subclasses of this class must implement this method.
You can construct an object from this class.
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
You are creating a Motorcycle class which is supposed to be a subclass of the Vehicle class. Which of the following class declaration statements will accomplish this? public class Motorcycle extends Vehicle public class Motorcycle implements Vehicle public class Motorcycle interfaces Vehicle public class Motorcycle inherits Vehicle
public class Motorcycle extends Vehicle