1. Consider the following code snippet: public class Vehicle { protected int num
ID: 3691110 • Letter: 1
Question
1. Consider the following code snippet:
public class Vehicle
{
protected int numberAxles;
. . .
}
Data in the numberAxles variable can be accessed by ____?
a. Only by the Vehicle class's methods and by all of its subclasses.
b. Only by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.
c. Only by the Vehiclee class's methods.
d. By any class.
2. Consider the following class hierarchy:
public class Vehicle
{
private String type;
public Vehicle(String type)
(
this.type = type;
}
public String dsplaInfo()
{
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.
a. super(type);
b. super(super(type));
c. super.super(type);
d. This cannot be done unless the Auto declares an instance variable named type.
3. 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?
a. The subclass is shadowing a superclass method.
b. The subclass is overloading a superclass method.
c. The subclass is overriding a superclass method.
d. This code will not compile.
4. To define a class that fulfills the requirements of an interface, the class header should include which of the following?
a. The keyword extends and the name of an abstract method in the interface.
b. The keyword extends and the name of the interface.
c. The keyword implements and the name of an abstract method in the interface.
d. The keyword implements and the name of the interface.
5. Consider the following code snippet:
public class Motorcycle extends Vehicle
{
private String model;
. . .
public Motorcycle(int numberAxles, String modelName)
model = modelName;
super(numberAxles);
}
}
What does this code do?
a. It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
b. It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
c. It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
d. This code will not compile.
Explanation / Answer
1Ans) B.Only by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.
Because 'protected' members are available upto all classes and interfaces available in the current package and child classes available in the other packages.
'public' members are available upto all packages which are available in present application.
'default' members are avaliable up to classes and interfaces available upto current package.
'private' members are available up to current class only.
2Ans) a.( Super(type) )
When we declare a Super(type) keyword it will goes to the super class and assigns the value of variable to the super class method.
3Ans) c (The subclass is overriding a superclass method).
Here given a two same methods but in different classes.Overriding is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes.
4Ans) (d) The keyword implements and the name of the interface.
In order to implement an interface first declare an interface define abstract methods in it.If we want to implement a methods in a interface we have to use a keyword 'implements' and next declare a name of an interface.In this way we can fulfill the requirements of a interface.
5Ans)This code will not compile because constructor Vehicle cannot be applied to Motorcycle because difference in parameters and the keyword super here is NumberAxles, this NumberAxles is not used in the Vehicle so this code will not be compiled.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.