Java OOP

 

SHORT ANSWER QUESTIONS

INTERFACES & POLYMORPHISM

 

QUESTIONS


1-To specify that a class will use an interface, we need to use the ______________ keyword on the class header line.

 

2-Consider the following class header:

 

public class G implements H

 

We know that G is a(n) ____________ and H is a(n) ____________.

 

3-An interface is simply a list of _____________ headers without bodies (though they can also contain variables that are constant).

 

4-True or false.  A class can implement multiple interfaces.  ___________

5-A class that implements an interface needs to implement _______ (none, some, or all) of methods in the interface.


6-Assume the class Box implements the interface Checkable.  Which of the following is allowed because of polymorphism?  Remember that you can never create an object of an interface’s type.

 

a)                      Checkable c = new Box();

b)                      Box b = new Checkable();

 

7-Which of the following states will make class Coco implement both interfaces Comparable and Iterable?

 

a)                      public class Coco implements Comparable implements Iterable

b)                      public class Coco implements Comparable, Iterable

c)                      public class Coco implements Comparable and Iterable

d)                      public class Coco implements Comparable && Iterable

 

8-What happens if a class tries to implement two interfaces that each have the same method header in them?  (Try it!)

 

a)                      An error occurs.

b)                      It works fine.

c)                      Java gets confused.

 

9-Class B implements interface A.  Which of the following is a permitted polymorphic reference in Java?

 

a)                      A ref = new A();

b)                      B ref = new B();

c)                      B ref = new A();

d)                      A ref = new B();


10-Can an interface have a constructor?

11-We can use ___________________ on the line above a method header to specify to Java that that method is part of the interface which the class is implementing.

 

12-True or false.  The use of @Override is optional.

 

13-Find the error in the code below.

 

public interface Somethingable

{

     public methodA();

     public methodB();

}

 

14-Find the error in the code below.

 

public interface Somethingable

{

     public void methodA(int a);

     public int methodB();

}

public class Coconut implements Somethingable

{

 

}

 

15-Find the error in the code below.

 

public interface Somethingable

{

     public void methodA(int a);

     public int methodB();

}

public class Coconut implements Somethingable

{

     public void methodA()

     {}

    

     public int methodB()

     {

           return 1;

     }

}

 

16-Find the error in the code below.

 

public interface Somethingable

{

     public void methodA(int a);

     public int methodB();

}

public class Coconut implements Somethingable

{

     @Override

     public void methodA(int z)

     {}

    

     @Override

     public int methodB()

     {

           return 1;

     }

    

     @Override

     public void methodC()

     {}

}

 

SOLUTIONS

 

Click here.