17. What happens when these statements are compiled? Loan loan = new Loan (amoun
ID: 3764814 • Letter: 1
Question
17. What happens when these statements are compiled?
Loan loan = new Loan (amount , months ) ;
Object obj = loan ;
obj . calculatePayments ( ) ;
(a)A compile-time error occurs because the Object object can’t reference a Loan object without
a cast
(b)A compile-time error occurs because the Object object can’t invoke the calculatePayments
method
(c)These statements will compile without errors
18. If a method accepts an Object object, what other types of objects can it accept?
(a)None
(b)All objects
(c)Objects from the Java API classes
(d)Objects from classes that explicitly extend the Object class
19. If the AbstractButton class is abstract and the JButton class inherits it, which of the following
data from the AbstractButton class must be defined in the JButton class?
(a)Only abstract methods
(b)All methods
(c)Only final methods
(d)Instance variables and methods
20. Which of the following is not a reason to declare a class as final?
(a)To prevent others from inheriting the class
(b)To improve efficiency
(c)To prevent others from changing how the methods work
(d)To give a class more functionality
21. If the Loan class contains a final method named calculatePayment, what does the following code
do?
HomeLoan loan = new HomeLoan(amount , months ) ;
loan . calculatePayment ( ) ;
(a)Causes a compile-time error
(b)Throws an exception
(c)Calls the calculatePayment method in the HomeLoan class
(d)Calls the calculatePayment method in the Loan class
22. Which of the following statements is true?
(a)Abstract classes can contain final methods.
(b)Final classes can contain abstract methods.
(c)A class with a final method is a final class.
23. If the Professor class extends the Teacher class, which of the following if statements will compile?
Professor professor = new Professor ( ) ;
Teacher teacher = new Teacher ( ) ;
(a)if (Professor instanceof Teacher){...}
(b)if (Professor instanceof Object){...}
(c)if (professor instanceof Teacher){...}
(d)if (Professor instanceof teacher){...}
24. Which of the following can you not code in a subclass?
(a)a method with the same signature as a method in the superclass
(b)a call to a constructor of the superclass
(c)a method that’s not defined by the superclass
(d)a call to a private method of the superclass
25. What keyword do you use in a class declaration to create a subclass?
(a)extends
(b)inherits
(c)overrides
(d)overloads
26. What feature allows you to treat an Admin object as though it were an Account object in this
inheritance hierarchy?
(a)polymorphism
(b)encapsulation
(c)abstraction
(d)garbage collection
(e)instantiation
27. Consider the following code. What modification would you have to make to this code for it to
compile?
public class Person
{
String name;
protected abstract String getName ( ) ;
public void setName ( ) { } ;
}
(a)Declare the getName method as public
(b)Declare the setName method as abstract
(c)Declare the Person class as abstract
(d)Declare the Person class as abstract and declare the getName method as public
28.
Refer to the code shown in Figures 9 and 10. If the user enters “C” at the console prompt,
which class contains the setTotal method that is called?
(a)Rental
(b)CanoeRental
(c)JetSkiRental
import java . ut i l . Scanner ;
public class Rental
{
private double total ;
public void setTotal ( )
{
System . out . print ln ( ‘ ‘ Rental total set ’ ’ ) ;
}
public stat ic void main( String [ ] args )
{
Scanner sc = new Scanner ( System. in ) ;
System . out . print ( ‘ ‘ Enter rental type : ’ ’
+ ‘ ‘ ’C’ for Canoe rental ’ ’
+ ‘‘ ’J’ for Jet Ski rental ’ ’);
String rentalType = sc . next ( ) ;
Rental rental = new Rental ( ) ;
i f ( rentalType . equalsIgnoreCase ( ‘ ‘C’ ’ ) )
rental = new CanoeRental ( ) ;
else i f ( rentalType . equalsIgnoreCase ( ‘ ‘ J ’ ’ ) )
rental = new JetSkiRental ( ) ;
rental . setTotal ( ) ;
}
}
Figure 9:
public class CanoeRental extends Rental
{
public void setTotal ( )
{
System. out . print ln ( ‘ ‘ Canoe total set ’ ’ ) ;
}
}
public class JetSkiRental extends Rental
{
public void setTotal ( )
{
System . out . print ln ( ‘ ‘ Jet ski total set ’ ’ ) ;
}
}
Figure 10:
29. Refer to the code shown in Figures 9 and 10. What happens if the user enters “K” at the console
prompt?
(a)The setTotal method in the Rental class is called.
(b)The setTotal method in the CanoeRental class is called.
(c)The setTotal method in the JetSkiRental class is called.
(d)A runtime error occurs.
30. Refer to the code shown in Figures 9 and 10. What happens when the code that follows is
executed?
Rental rental = new JetSkiRental ( ) ;
rental . setTotal ( ) ;
(a)The setTotal method in the Rental class is called.
(b)The setTotal method in the CanoeRental is called.
(c)The setTotal method in the JetSkiRental is called.
(d)A compile-time error occurs.
32. Assume that all three classes in this inheritance hierarchy contain a calculateMaxSpeed method.
Which class defines the method that is called when the second statement in the code that follows
is executed?
MotorVehicle car = new Automobile ( ) ;
car . calculateMaxSpeed ( ) ;
(a)Automobile
(b)MotorVehicle
(c)Vehicle
(d)Object
33. What change do you have to make to the code that follows to get it to compile?
public f inal class Trajectory
{
double veloci ty ;
void convertVelocityUnits ( )
{
velocity *= 1.2;
}
}
public class FlightPath extends Trajectory
{
f inal Trajectory trajectory = new Trajectory ( ) ;
f inal void computeFlightPath ( )
{
System . out . print ln ( ‘ ‘Computing Fl ight Path ’ ’ ) ;
trajectory . velocity /= 1.2;
}
}
(a)Remove the final keyword from the Trajectory class
(b)Remove the final keyword from the trajectory instance variable
(c)Remove the final keyword from the Trajectory class and the trajectory instance variable
(d)No change needed; the code compiles as is
34. What class or classes is a variable available to if it is declared without an access modifier?
(a)All classes in the same package
(b)All classes in the same package and to subclasses
(c)All classes in all packages
(d)The current class
35. What class or classes is a method that is declared protected available to?
(a)All classes in the same package
(b)All classes in the same package and to subclasses
(c)All classes in all packages
(d)The current class
Explanation / Answer
17.
(a)A compile-time error occurs because the Object object can’t reference a Loan object without
a cast
18.
(c)Objects from the Java API classes
19.
(a)Only abstract methods
20.
(d)To give a class more functionality
21.
(c)Calls the calculatePayment method in the HomeLoan class
22.
(c)A class with a final method is a final class.
23.
(c)if (professor instanceof Teacher){...}
24.
(d)a call to a private method of the superclass
25.
(a)extends
26.
a)polymorphism
27.
(d)Declare the Person class as abstract and declare the getName method as public
28.
(b)CanoeRental
29.
(a)The setTotal method in the Rental class is called.
30.
c)The setTotal method in the JetSkiRental is called.
32.
(a)Automobile
33.
a)Remove the final keyword from the Trajectory class
34.
(b)All classes in the same package and to subclasses
35.
(a)All classes in the same package
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.