Java Polymorphism and Interfaces Quiz
15 Questions
100 Views

Java Polymorphism and Interfaces Quiz

Created by
@GuiltlessCyan

Questions and Answers

A polymorphic reference is one that can refer to _______________ type(s) of object(s).

multiple

In Java, polymorphic method binding occurs ____________________.

at run-time

In Java, a(n) ___________________ is a collection of constants and abstract methods.

interface

Which of the following is true regarding the method call spot.speak() with Animal spot = new Dog();?

<p>It will print a Dog's sound</p> Signup and view all the answers

What will the following method call a.compareTo(b) return if Object a is larger than Object b?

<p>It will return a number greater than 0</p> Signup and view all the answers

In order to create a class that implements an interface, the __________________ keyword is used.

<p>implements</p> Signup and view all the answers

What will happen if you try to call a.wagTail() when a is declared as Animal a = new Dog();?

<p>It will result in a compile-time error</p> Signup and view all the answers

The commitment to execute certain code to carry out a method invocation is referred to as ______________.

<p>binding</p> Signup and view all the answers

It is possible to create an object by instantiating the Animal interface.

<p>False</p> Signup and view all the answers

A parameter to a method can be polymorphic.

<p>True</p> Signup and view all the answers

An interface cannot declare any instance variables.

<p>True</p> Signup and view all the answers

If a class implements an interface, it cannot extend another class.

<p>False</p> Signup and view all the answers

How does inheritance support polymorphism?

<p>Both classes containing the same signature, the parent can be polymorphic.</p> Signup and view all the answers

What is the difference between a class and an interface?

<p>Class can be instantiated and interfaces cannot.</p> Signup and view all the answers

Describe the Comparable interface.

<p>A single method called compareTo that returns an x &gt; 0, x = 0, x &lt; 0.</p> Signup and view all the answers

Study Notes

Polymorphism

  • Polymorphic reference can refer to multiple types of objects.
  • In Java, polymorphic method binding occurs at run-time, meaning the method invoked is determined by the object’s actual type.

Interfaces

  • An interface in Java consists of constants and abstract methods.
  • A class implementing an interface uses the "implements" keyword.

Method Overriding

  • If a Dog class overrides a method from a parent Animal class, calling spot.speak() will invoke the Dog's overridden method.
  • If the Dog class implements the Animal interface with an additional method wagTail, trying to call a.wagTail() will result in a compile-time error because wagTail() is not defined in the Animal interface.

Object Comparison

  • The compareTo method will return a number greater than 0 if the first object is larger than the second, zero if they are equal, or less than 0 if the first is smaller.

Binding and Instantiation

  • Binding refers to the commitment to execute certain code for a method invocation.
  • It is false that an object can be instantiated from an interface; interfaces cannot be instantiated.
  • It is true that methods' parameters can be polymorphic, allowing for various object types.

Class vs. Interface

  • A class can be instantiated, while an interface cannot.
  • A class that implements an interface can still extend another class.

Comparable Interface

  • The Comparable interface includes a method called compareTo that returns three possible values: greater than 0, equal to 0, or less than 0, indicating the comparative result between objects.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Test your understanding of Java polymorphism and interfaces with this quiz. It covers key concepts such as polymorphic references, method binding at run-time, and the structure of interfaces in Java. Perfect for any Java programming student!

More Quizzes Like This

Use Quizgecko on...
Browser
Browser