Polymorphism Flashcards

1
Q

What is a method’s signature?

a. The signature of a method is the name of the method the type of its return value.
b. The signature of a method is the name of the method and the names of its parameters.
c. The signature of a method is the name of the method and the data types of its parameters.
d. The signature of a method is the name of the method, its parameter list, and its return type.

A

c. The signature of a method is the name of the method and the data types of its parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Here is a method definition:
int compute( a int, y double ){ . . . .}
Which of the following has a different signature?
a.    int compute( sum int, value double ){ . . . .}
b.    double compute( a int, y double ){ . . . .}
c.    double compute( sum int, y double ){ . . . .}
d.    int compute( a int, y int ){ . . . .}
A

d. int compute( a int, y int ){ . . . .}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What must a non-abstract child do about an abstract method in its parent class?

a. A child must override an abstract method inherited from its parent by defining a method with the same signature and same return type.
b. A child must define an additional method similar to the one inherited from its parent by defining a method with the same signature and different return type.
c. A child must not define any method with the same signature as the parent’s abstract method.
d. A non-abstract child must define an abstract method with the same signature and same return type as the parent’s abstract method.

A

a. A child must override an abstract method inherited from its parent by defining a method with the same signature and same return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Here is an abstract method defined in the parent:
public abstract int sumUp ( int[] arr );
Which of the following is required in a non-abstract child?
a. public abstract int sumUp ( int[] arr ) { . . . }
b. public int sumUp ( int[] arr ) { . . . }
c. public double sumUp ( int[] arr ) { . . . }
d. public int sumUp ( long[] arr ) { . . . }

A

b. public int sumUp ( int[] arr ) { . . . }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What must be true if a child of an abstract parent class does not override all of the parent’s abstract methods?

a. This is always an error.
b. The child class itself must be declared to be abstract.
c. Child classes are automatically non-abstract, so this is OK.
d. The parent class is in error.

A

b. The child class itself must be declared to be abstract.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Here is a situation:
Birthday happy;
happy = new AdultBirthday( “Joe”, 39);

happy.greeting();
Which greeting() method is run: the one defined for Birthday or the one defined for AdultBirthday?
a. The one defined for Birthday because that is the type of the variable happy.
b. The one defined for AdultBirthday because that is the type of the object referred to by happy.
c. The one closest in the source code to the happy.greeting() statement.
d. Neither—the assignment statement is in error.

A

b. The one defined for AdultBirthday because that is the type of the object referred to by happy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Say that class Rodent has a child class Rat and anothr child class Mouse. Class Mouse has a child class PocketMouse. Examine the following
   Rodent rod;
   Rat rat = new Rat();
   Mouse mos = new Mouse();
   PocketMouse pkt = new PocketMouse();
Which one of the following will cause a compiler error?
a.    rod = rat;
b.    rod = mos;
c.    pkt = null;
d.    pkt = rat;
A

d. pkt = rat;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
class Rodent has a child class Rat and anothr child class Mouse. Class Mouse has a child class PocketMouse. Examine the following
   Rodent rod;
   Rat rat = new Rat();
   Mouse mos = new Mouse();
   PocketMouse pkt = new PocketMouse();

Which of the following array declarations is correct for an array that is expected to hold up to 10 objects of types Rat, Mouse, and PocketMouse?

a. Rat[] array = new Rat[10];
b. Rodent[] array = new Rat[10];
c. Rodent[] array = new Rodent[10];
d. Rodent[10] array;

A

c. Rodent[] array = new Rodent[10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the name of the class that is the ancestor to every other class in Java?

a. Object
b. Class
c. Root
d. Java

A

a. Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Examine the following code:
String str = “Hot Java”;

boolean switch = str instanceof String;
What value is placed in switch?
a.    true
b.    false
c.    "Hot Java"
d.    null
A

a. true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the output of the following program?

class GFG 
{ 
    protected void getData() 
    { 
        System.out.println("Inside GFG"); 
    } 
} 
class GeeksforGeeks extends GFG 
{ 
    protected void getData() 
    { 
        System.out.println("Inside GeeksforGeeks"); 
    } 
} 
public class Test 
{ 
    public static void main(String[] args) 
    { 
        GFG obj = new GeeksforGeeks(); 
        obj.getData(); 
    } 
} 
a) Inside GFG
b) Inside GeeksforGeeks
c) Compilation error
d) Runtime error
A
Ans. (b)
Explanation: A reference variable of GFG class is used to point to an object of class GeeksforGeeks. At the time of compilation, the JVM checks whether the method being called is defined in GFG class, but at the runtime, JVM invoke the method of GeeksforGeeks class because the object is from class GeeksforGeeks. Refer to static vs dynamic binding in java for more details.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output of the following program?

class Test 
{ 
    void myMethod() 
    { 
        System.out.println("GeeksforGeeks"); 
    } 
} 
public class Derived extends Test 
{ 
    void myMethod() 
    { 
        System.out.println("GFG"); 
    } 
    public static void main(String[] args) 
    { 
        Derived object = new Test(); 
        object.myMethod(); 
    } 
} 
a) GeeksforGeeks
b) GFG
c) Compilation error
d) Runtime error
A
Ans. (c)
Explanation: A child class cannot be used as a reference to an object of super class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the output of the following program?

class GFG 
{ 
    protected void getData() 
    { 
        System.out.println("Inside GFG"); 
    } 
} 
class GeeksforGeeks extends GFG 
{ 
    protected void getData() 
    { 
        System.out.println("Inside GeeksforGeeks"); 
    } 
    protected void getValue() 
    { 
        System.out.println("GeeksforGeeks"); 
    } 
} 
public class Test 
{ 
    public static void main(String[] args) 
    { 
        GFG obj = new GeeksforGeeks(); 
        obj.getValue(); 
    } 
} 
a) Compilation error
b) Runtime error
c) GeeksforGeeks
d) None of these
A

Ans. (a)
Explanation: A GFG reference variable is used to store GeeksforGeeks object. At compile time, JVM looks for getValue method in GFG class. Detecting the absence of it, JVM throws a compile time error. Refer to static vs dynamic binding in java for more details.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
interface GFG 
{ 
    void myMethod(); 
    void getInfo(); 
} 
abstract class Geeks implements GFG 
{ 
    void getData() 
    { 
        System.out.println("GFG"); 
    } 
} 
public class Test extends Geeks 
{ 
    public void myMethod() 
    { 
        System.out.println("GeeksforGeeks"); 
    } 
    public void getInfo() 
    { 
        System.out.println("Geeks"); 
    } 
    public static void main(String[] args) 
    { 
        Geeks obj = new Test(); 
        obj.getInfo(); 
    } 
} 
a) Geeks
b) Compilation error
c) Runtime error
d) None of these
A
Ans. (a)
Explanation: Class Geeks implements GFG interface. So all the methods declared in GFG interface get replicated for Geeks class. So when getInfo method is called on line 1 in the program above, the compiler checks to see if getInfo method exists in the Geeks class as a variable of type Geeks is being used to reference Test object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly