chapter 6 sample exam questions Flashcards
Working with inheritance
What is the output of the following code? class Animal { void jump() { System.out.println("Animal"); } } class Cat extends Animal { void jump(int a) { System.out.println("Cat"); } } class Rabbit extends Animal { void jump() { System.out.println("Rabbit"); } } class Circus { public static void main(String args[]) { Animal cat = new Cat(); Rabbit rabbit = new Rabbit(); cat.jump(); rabbit.jump(); } } a Animal Rabbit b Cat Rabbit c Animal Animal d None of the above
Answer: a Explanation: Although the classes Cat and Rabbit seem to override the method jump, the class Cat doesn’t override the method jump() defined in the class Animal. The class Cat defines a method parameter with the method jump, which makes it an 462 CHAPTER 6 Working with inheritance overloaded method, not an overridden method. Because the class Cat extends the class Animal, it has access to the following two overloaded jump methods: void jump() { System.out.println("Animal"); } void jump(int a) { System.out.println("Cat"); } The following lines of code create an object of class Cat and assign it to a variable of type Animal: Animal cat = new Cat(); When you call the method jump on the previous object, it executes the method jump, which doesn’t accept any method parameters, printing the following value: Animal The following code will also print Animal and not Cat: Cat cat = new Cat(); cat.jump();
Given the following code, select the correct statements: class Flower { public void fragrance() {System.out.println("Flower"); } } class Rose { public void fragrance() {System.out.println("Rose"); } } class Lily { public void fragrance() {System.out.println("Lily"); } } class Bouquet { public void arrangeFlowers() { Flower f1 = new Rose(); Flower f2 = new Lily(); f1.fragrance(); } } a The output of the code is Flower b The output of the code is Rose c The output of the code is Lily d The code fails to compile
Answer: d
Explanation: Although the code seems to implement polymorphism using classes,
note that neither of the classes Rose or Lily extends the class Flower. Hence, a variable
of type Flower can’t be used to store objects of the classes Rose or Lily. The following
lines of code will fail to compile:
Flower f1 = new Rose();
Flower f2 = new Lily();
Examine the following code and select the correct method declaration to be inserted at //INSERT CODE HERE: interface Movable { void move(); } class Person implements Movable { public void move() { System.out.println("Person move"); } } class Vehicle implements Movable { public void move() { System.out.println("Vehicle move"); } } class Test { // INSERT CODE HERE movable.move(); } } a void walk(Movable movable) { b void walk(Person movable) { c void walk(Vehicle movable) { d void walk() {
Answer: a, b, c Explanation: You need to insert code in the class Test that makes the following line of code work: movable.move(); Hence, option (d) is incorrect. Because class Test doesn’t define any instance meth-ods, the only way that the question’s line of code can execute is when a method parameter movable is passed to the method walk. Option (a) is correct. Because the interface Movable defines the method move, you can pass a variable of its type to the method move. Option (b) is correct. Because the class Person implements the interface Movable and defines the method move, you can pass a variable of its type to the method walk. With this version of the method walk, you can pass it an object of the class Person or any of its subclasses. Option (c) is correct. Because the class Vehicle implements the interface Movable and defines the method move, you can pass a variable of its type to the method walk. With this version of method walk, you can pass it an object of the class Vehicle or any of its subclasses.
Select the correct statements:
a Only an abstract class can be used as a base class to implement polymorphism
with classes.
b Polymorphic methods are also called overridden methods.
c In polymorphism, depending on the exact type of object, the JVM executes the
appropriate method at compile time.
d None of the above
Answer: b
Option (a) is incorrect. To implement polymorphism with classes, either an abstract
class or a concrete class can be used as a base class.
Option (c) is incorrect. First of all, no code execution takes place at compile time.
Code can only execute at runtime. In polymorphism, the determination of the exact
method to execute is deferred until runtime and is determined by the exact type of
the object on which a method needs to be called.
Given the following code, select the correct statements: class Person {} class Employee extends Person {} class Doctor extends Person {} a The code exhibits polymorphism with classes. b The code exhibits polymorphism with interfaces. c The code exhibits polymorphism with classes and interfaces. d None of the above
Answer: d Explanation: The given code doesn’t define any method in the class Person that is redefined or implemented in the classes Employee and Doctor. Although the classes Employee and Doctor extend the class Person, all three polymorphism concepts or design principles are based on a method, which is missing in these classes.
Which of the following statements are true?
a Inheritance enables you to reuse existing code.
b Inheritance saves you from having to modify common code in multiple classes.
c Polymorphism passes special instructions to the compiler so that the code can
run on multiple platforms.
d Polymorphic methods can’t throw exceptions.
Answer: a, b
Explanation: Option (a) is correct. Inheritance can allow you to reuse existing code
by extending a class. In this way, the functionality that’s already defined in the base
class need not be defined in the derived class. The functionality offered by the base
class can be accessed in the derived class as if it were defined in the derived class.
Option (b) is correct. Common code can be placed in the base class, which can be
extended by all the derived classes. If any changes need to be made to this common
code, it can be modified in the base class. The modified code will be accessible to all
the derived classes.
Option (c) is incorrect. Polymorphism doesn’t pass any special instructions to the
compiler to make the Java code execute on multiple platforms. Java code can execute
on multiple platforms because the Java compiler compiles to virtual machine code,
which is platform neutral. Different platforms implement this virtual machine.
Option (d) is incorrect. Polymorphic methods can throw exceptions.
Given the following code, which of the options are true? class Satellite { void orbit() {} } class Moon extends Satellite { void orbit() {} } class ArtificialSatellite extends Satellite { void orbit() {} } a The method orbit defined in the classes Satellite, Moon, and Artificial-Satellite is polymorphic. b Only the method orbit defined in the classes Satellite and Artificial-Satellite is polymorphic. c Only the method orbit defined in the class ArtificialSatellite is polymorphic. d None of the above.
Answer: a
Explanation: All these options define classes. When methods with the same method
signature are defined in classes that share an inheritance relationship, the methods
are considered polymorphic.
Examine the following code: class Programmer { void print() { System.out.println("Programmer - Mala Gupta"); } } class Author extends Programmer { void print() { System.out.println("Author - Mala Gupta"); } } class TestEJava { Programmer a = new Programmer(); // INSERT CODE HERE a.print(); b.print(); } Which of the following lines of code can be individually inserted at //INSERT CODE HERE so that the output of the code is as follows? Programmer - Mala Gupta Author - Mala Gupta a Programmer b = new Programmer(); b Programmer b = new Author(); c Author b = new Author(); d Author b = new Programmer(); e Programmer b = ((Author)new Programmer()); f Author b = ((Author)new Programmer());
Answer: b, c Explanation: Option (a) is incorrect. This code will compile, but because both the ref-erence variable and object are of type Programmer, calling print on this object will print Programmer - Mala Gupta, not Author - Mala Gupta. Option (d) is incorrect. This code will not compile. You can’t assign an object of a base class to a reference variable of a derived class. Option (e) is incorrect. This line of code will compile successfully, but it will fail at runtime with a ClassCastException. An object of a base class can’t be cast to an object of its derived class. Option (f) is incorrect. The expression ((Author)new Programmer()) is evaluated before it can be assigned to a reference variable of type Author. This line of code also tries to cast an object of the base class, Programmer, to an object of its derived class, Author. This code will also compile successfully but will fail at runtime with a Class-CastException. Using a reference variable of type Author won’t make a difference here. What matters here is the type that follows the new operator.
Given the following code, which of the options, when applied individually, will make it compile successfully? Line1> interface Employee {} Line2> interface Printable extends Employee { Line3> String print(); Line4> } Line5> class Programmer { Line6> String print() { return("Programmer - Mala Gupta"); } Line7> } Line8> class Author extends Programmer implements Printable, Employee { Line9> String print() { return("Author - Mala Gupta"); } Line10> } a Modify the code on line 2 to interface Printable { b Modify the code on line 3 to public String print(); c Define the accessibility of the print methods to public on lines 6 and 9. d Modify the code on line 8 so that it implements only the interface Printable.
Answer: c
Explanation: The methods in an interface are implicitly public. A non-abstract class
that implements an interface must implement all the methods defined in the inter-face. While overriding or implementing the methods, the accessibility of the imple-mented method must be public. An overriding method can’t be assigned a weaker
access privilege than public.
Option (a) is incorrect. There are no issues with the interface Printable extending
the interface Employee and the class Author implementing both of these interfaces.
Option (b) is incorrect. Adding the access modifier to the method print on line 3
won’t make any difference to the existing code. The methods defined in an interface
are implicitly public.
Option (d) is incorrect. There are no issues with a class implementing two inter-faces when one of the interfaces extends the other interface.
What is the output of the following code? class Base { String var = "EJava"; void printVar() { System.out.println(var); } } class Derived extends Base { String var = "Guru"; void printVar() { System.out.println(var); } } class QReference { public static void main(String[] args) { Base base = new Base(); Base derived = new Derived(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } } a EJava EJava EJava Guru b EJava Guru EJava Guru c EJava EJava EJava EJava d EJava Guru Guru Guru
Answer: a Explanation: With inheritance, the instance variables bind at compile time and the methods bind at runtime. The following line of code refers to an object of the class Base, using a reference variable of type Base. Hence, both of the following lines of code print EJava: System.out.println(base.var); base.printVar(); But the following line of code refers to an object of the class Derived using a refer-ence variable of type Base: Base derived = new Derived(); Because the instance variables bind at compile time, the following line of code accesses and prints the value of the instance variable defined in the class Base: System.out.println(derived.var); // prints EJava In derived.printVar(), even though the method printVar is called using a refer-ence of type Base, the JVM is aware that the method is invoked on a Derived object and so executes the overridden printVar method in the class Derived.