2. Consider the classes shown below: Which lines in the following code will comp
ID: 3604617 • Letter: 2
Question
2. Consider the classes shown below:
Which lines in the following code will compile without error?
a) Line 1 only
b) Line 2 only
c) Lines 1 and 2
d) Neither line will compile without error
3. Consider the classes shown below:
What is the output of the following lines of code?
a) 100 100
b) -14 100
c) -14 -14
d) 100 -14
4. Suppose the class Value is partially defined below
A subclass of Value, LargerValue, is defined with a getValue method that returns twice the value of the parent. Which line is the body of LargerValue's getValue method?
a) return getValue() * 2;
b) return super.getValue() * 2;
c) return number * 2;
d) return super.number * 2;
5. You are creating a Motorcycle class which is supposed to inherit from the Vehicle class. Which of the following class declaration statements will accomplish this?
a) public class Motorcycle inherits Vehicle
b) public class Motorcycle implements Vehicle
c) public class Motorcycle interfaces Vehicle
d) public class Motorcycle extends Vehicle
6. Consider the following class hierarchy:
You have written a program to use these classes, as shown in the following code snippet:
Complete the code in this program snippet to correctly display the auto's type.
a) myAuto.displayInfo()
b) myAuto.super.displayInfo()
c) myAuto.super.super.displayInfo()
d) This cannot be done unless the Auto class overrides the displayInfo method.
7. 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.
a) super(type);
b) super.type;
c) super.super.type;
d) super.displayInfo()
8. 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?
a) s will contain the values of the instance variables in programmer.
b) s will contain only the class name of the programmer object.
c) s will contain the class name of the programmer object followed by a hash code.
d) This code will not compile.
9. Consider the following code snippet:
What is wrong with this code?
a) The Timer object should be declared before the MyListener class.
b) The listener has not been attached to the Timer object.
c) The Timer object must be declared as final.
d) There is nothing wrong with the code.
Explanation / Answer
2) C
Since child extends parent. Child can access its own and its parent methods. So line1 and line2 will execute without error.
3)A
100,100
Because objects call the method, getValue which returns the value 100. Because value is assigned with 100 in Parent class.
4)B
return super.getValue() * 2;
5)D
Using extends to inherit properties from its Parent class
public class Motorcycle extends Vehicle
6)A
myAuto.displayInfo()
7)A
super(type);
8)C
s will contain the name of the programmer object followed by a hash code.
9)B
The listener has not been attached to the Timer object.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.