Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code example 15-1 public interface IPrintable { void Print(); } public class Pri

ID: 3603467 • Letter: C

Question

Code example 15-1

public interface IPrintable

{

    void Print();

}

public class Printer

{

    public static void Print(IPrintable p)

    {

        MessageBox.Show("Print");

        p.Print();

    }

}

public class Order : IPrintable

{

    public void Print()

    {

        MessageBox.Show("Order");

    }

}

public class Transaction

{

}

public class Rental : Transaction, IPrintable

{

    public void Print()

    {

        MessageBox.Show("Rental");

    }

}

5. (Refer to code example 15-1.) Which of the following statements will definitely not compile?

a.

IPrintable p = new Order();

b.

Rental rental = new Rental();

IPrintable p = rental;

c.

IPrintable p = new IPrintable();

d.

Transaction t = new Rental();

6. (Refer to code example 15-1.) What happens when the code that follows is executed?

Rental r = new Rental();

Printer.Print(r);

a.

“Print” is displayed in a message box.

b.

“Rental” is displayed in a message box.

c.

“Print” is displayed in a message box and then “Rental” is displayed in a second message box.

d.

A runtime error occurs because the Print method can’t accept Rental objects.

7. (Refer to code example 15-1.) What happens when the code that follows is executed?

Order order = new Order();

order.Print();

a.

“Print” is displayed in a message box.

b.

“Order” is displayed in a message box.

c.

“Print” is displayed in a message box and then “Order” is displayed in a second message box.

d.

A runtime error occurs because the Print method can’t accept Rental objects.

8. A method that accepts an interface as an argument can accept any object that

a.

implements that interface

b.

defines the same methods as the interface

c.

implements the interface or defines the same methods as the interface

d.

is created from that interface

9. A variable that stores an object that implements an interface can be declared as which of the following data types?

a.

The class that defines the object

b.

The interface that the object implements

c.

Any subclass of the class that defines the object

d.

All of the above

e.

A and C only

10. Which of the following is false about interfaces?

a) An interface describes a set of methods that can be called on an object, providing a

default implementation for the methods.

b) An interface describes a set of methods that can be called on an object, without providing concrete implementation for the methods.

c) Interfaces are useful when attempting to assign common functionality to possibly

unrelated classes.

d) Once a class implements an interface, all objects of that class have an is-a relationship with the interface type.

a.

IPrintable p = new Order();

b.

Rental rental = new Rental();

IPrintable p = rental;

c.

IPrintable p = new IPrintable();

d.

Transaction t = new Rental();

Explanation / Answer

5. c. IPrintable p = new IPrintable();

6. c.“Print” is displayed in a message box and then “Rental” is displayed in a second message box.

7. b.“Order” is displayed in a message box.

8. a. implements that interface

9. d. All of the above

10. a) An interface describes a set of methods that can be called on an object, providing a default implementation for the methods.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote