Quiz: Java polymorphism and interfaces

This quiz covers key concepts of Java polymorphism and interfaces, focusing on definitions, examples, and practical applications to enhance understanding.

EllaJ2·48 flashcards·48 questions
collegecomputer_scienceprogramming
0
Known
1 / 48
0
Learning
Front

What is polymorphism in Java?

Tap to flip
Back

Polymorphism allows methods to do different things based on the object that it is acting upon. It includes method overriding and overloading.

Tap to flip
Got it
Still learning

Quiz(48 questions)

Question 1 of 48

1. What is the primary purpose of an abstract class in Java?

Terms in this Study Set(48)

Basics of Polymorphism(16)

What is polymorphism in Java?

Polymorphism allows methods to do different things based on the object that it is acting upon. It includes method overriding and overloading.

Define method overriding.

Method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass. - Must have the same name and parameters.

True or False: Overriding can change the return type.

False. The return type must be the same or a subtype of the return type in the superclass method.

Fill in the blank: Method ________ allows a class to have multiple methods with the same name.

Overloading. - Method signature must differ in parameters.

What is the difference between overriding and overloading?

Overriding: Same method name, same parameters, different class. Overloading: Same method name, different parameters, same class.

Cause → Effect: What happens when a subclass overrides a method?

The subclass's version is executed, allowing custom behavior while still adhering to the superclass contract.

Example of method overloading.

Consider: void add(int a, int b) void add(double a, double b) Both named 'add' but differing parameters.

What is dynamic method dispatch?

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime rather than compile-time.

True or False: A subclass can override a private method.

False. Private methods are not visible to subclasses and cannot be overridden.

List benefits of polymorphism.

- Code reusability - Flexibility - Maintainability - Interchangeability of objects

What is the purpose of the 'super' keyword?

'Super' is used to call the superclass's method or constructor, enabling access to overridden methods or parent constructors.

How does Java determine which method to execute?

Java uses the object's runtime type to determine which overridden method to execute, enabling dynamic behavior.

Example of polymorphism in action.

Consider a superclass 'Animal' with a method 'sound()'. Subclasses 'Dog' and 'Cat' override 'sound()' to bark and meow, respectively.

What is compile-time polymorphism?

Compile-time polymorphism occurs when method overloading is resolved at compile time, based on the method signature.

Fill in the blank: Overloaded methods must have different ________.

Parameter lists. - This includes the number or types of parameters.

True or False: Polymorphism is only applicable to methods.

False. Polymorphism can apply to methods and to objects, enabling different behaviors and functions.

Understanding Interfaces(16)

What is an interface in Java?

An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. It cannot contain instance fields or constructors.

Why use interfaces?

Interfaces provide a way to achieve abstraction and multiple inheritance in Java. They enable classes to implement multiple behaviors, ensuring a common protocol for different objects.

How to declare an interface?

To declare an interface, use the 'interface' keyword. Example: ```java interface Vehicle { void drive(); } ```

True or False: Interfaces can have method implementations.

False. Interfaces can have default methods with implementations but traditionally only declare method signatures.

Fill in the blank: A class implements an interface using the ______ keyword.

implements

What does polymorphism allow with interfaces?

Polymorphism allows objects to be treated as instances of their interface type rather than their actual class type, which enables flexibility and dynamic method resolution.

Compare interface and abstract class.

Interface: - No concrete methods (except defaults) - Multiple inheritance Abstract Class: - Can have concrete methods - Single inheritance only

How to implement an interface?

A class implements an interface by providing concrete implementations for all of its methods. Example: ```java class Car implements Vehicle { public void drive() { System.out.println("Driving a car"); } } ```

What is a default method in an interface?

A default method allows an interface to provide a default implementation for a method. This helps in extending interfaces without breaking existing implementations.

True or False: A class can implement multiple interfaces.

True. A class in Java can implement multiple interfaces, allowing it to inherit behaviors from different sources.

What are functional interfaces?

Functional interfaces are interfaces with exactly one abstract method, enabling lambda expressions. Example: ```java @FunctionalInterface interface Calculator { int add(int a, int b); } ```

Cause → Effect: Implementing an interface affects class behavior how?

It forces the class to provide implementations for the interface's methods, ensuring a consistent contract.

How to use an interface in a method parameter?

You can use an interface as a parameter type to create methods that can accept any implementing class. Example: ```java void startVehicle(Vehicle v) { v.drive(); } ```

What happens if a class does not implement all interface methods?

If a class does not implement all methods of an interface, it must be declared abstract, and cannot be instantiated.

Fill in the blank: An interface can extend another ______.

interface

What is the purpose of marker interfaces?

Marker interfaces, like Serializable, are used to provide metadata to classes without defining methods, signaling certain properties to the Java runtime.

Advanced Polymorphism Concepts(16)

What is an abstract class?

An abstract class cannot be instantiated and may contain abstract methods. - Base for subclasses - Can have fields and methods - Supports polymorphism

True or False: Java allows multiple inheritance with classes.

False. Java does not support multiple inheritance with classes to avoid ambiguity, but it allows multiple inheritance through interfaces.

How does an abstract class differ from an interface?

Abstract class: can have method implementations, fields; Interface: only method signatures before Java 8, default methods allowed now.

What happens if a subclass does not implement all abstract methods?

The subclass must be declared abstract. It cannot be instantiated until all abstract methods are implemented.

Fill in the blank: Abstract classes can define ______ methods.

Both abstract and concrete methods.

Use case for interfaces?

To define a contract for classes without forcing a class hierarchy. - Achieves multiple inheritance - Promotes code flexibility

True or False: Interfaces can have state (fields).

False. Interfaces cannot have instance fields; all fields are implicitly public, static, and final.

How does Java handle polymorphism with interfaces?

Classes implementing the same interface can be used interchangeably, allowing for dynamic method binding at runtime.

What is a common problem with multiple inheritance?

The Diamond Problem: Ambiguities occur when a subclass inherits from two classes with conflicting method implementations.

Example of polymorphism with an abstract class.

Consider an abstract class `Shape` with a method `draw()`. Subclasses like `Circle` and `Square` implement `draw()` differently.

Comparison: abstract class vs interface.

Abstract class: can contain state and implemented methods; Interface: purely a contract, no state.

Cause: A class implements multiple interfaces.

Effect: The class inherits the abstract methods of all interfaces and must provide implementations.

What is the purpose of using abstract classes?

To provide a common base while allowing subclasses to customize specific behaviors and methods.

True or False: Abstract classes can be final.

False. Abstract classes cannot be declared final, as they need to be subclassed.

What is the main advantage of polymorphism?

It allows for code flexibility and reusability by enabling objects to be treated as instances of their parent class or interface.

Fill in the blank: Polymorphism enables ______ code.

More maintainable and flexible code.

Questions in this Study Set(48)

1. What is the primary purpose of an abstract class in Java?

A.To provide common code for subclasses
B.To implement multiple inheritance
C.To restrict access to certain methods
D.To create objects directly

2. What does polymorphism allow in Java programming?

A.Methods to behave differently based on the object type
B.Classes to inherit from multiple superclasses
C.Variables to change types at runtime
D.Methods to be defined without a body

3. What keyword is used to declare an interface in Java?

A.interface
B.implements
C.class
D.public

4. Which of the following statements about interfaces is true?

A.Interfaces can contain method implementations
B.Interfaces can have instance fields
C.Interfaces support multiple inheritance
D.Interfaces must be declared abstract

5. What is method overriding?

A.Creating a new method in the superclass
B.Defining a new method in the subclass with the same name as in the superclass
C.Changing the return type of a superclass method
D.Making a method private in the subclass

6. Which statement about interfaces is FALSE?

A.They can contain default methods.
B.They can have multiple implementations.
C.They can have concrete methods.
D.They can extend other interfaces.

7. What must a subclass do if it does not provide implementations for all abstract methods?

A.Declare itself as abstract
B.Be marked as final
C.Provide default implementations
D.Allow direct instantiation

8. True or False: The return type of an overridden method can be different from the superclass method.

A.True
B.False
C.Only if the subclass is abstract
D.Only for static methods

9. What is one primary benefit of using interfaces in Java?

A.Code cannot be reused.
B.It allows for single inheritance only.
C.It enables multiple inheritance of behavior.
D.It enforces a strict class hierarchy.

10. Which of the following is NOT a feature of interfaces?

A.Can contain default methods
B.Can have static methods
C.Can include instance fields
D.Can define method signatures

11. What does method overloading refer to?

A.Having multiple methods in a subclass
B.Defining multiple methods with the same name but different parameters
C.Changing method implementation in the superclass
D.Creating methods that cannot be inherited

12. In the context of interfaces, what does the term 'implementation' refer to?

A.The method signature
B.The act of declaring an interface
C.Providing concrete methods for an interface
D.The behavior of an abstract class

13. How does polymorphism enhance code flexibility?

A.By allowing class instantiation without constructor
B.By enabling method overriding
C.By treating objects as their parent class or interface
D.By preventing code duplication

14. Which of the following statements correctly differentiates overriding and overloading?

A.Overriding is for different classes; overloading is within the same class.
B.Overriding allows different parameters; overloading does not.
C.Overriding is done at compile time; overloading is at runtime.
D.Both are the same concepts.

15. Which of the following is an example of a functional interface?

A.An interface with two abstract methods
B.An interface that extends another interface
C.An interface with one abstract method
D.An interface with default methods only

16. Fill in the blank: An abstract class can define both ______ methods.

A.abstract and concrete
B.static and final
C.private and protected
D.default and static

17. What happens when a subclass overrides a method from its superclass?

A.The superclass method is executed instead
B.The subclass's version is executed
C.A compilation error occurs
D.It leads to a runtime exception

18. How does polymorphism relate to interfaces?

A.It allows classes to have multiple inheritance.
B.It enables treating objects as their interface type.
C.It restricts method implementations.
D.It eliminates the need for method overloading.

19. Which scenario illustrates polymorphism using an abstract class?

A.A class implementing multiple interfaces
B.A class extending another class directly
C.A superclass reference holding a subclass object
D.A class with static methods only

20. Which of the following is an example of method overloading?

A.void calculate(int a, int b)
B.void calculate(int a)
C.void calculate(double a, double b)
D.All of the above

21. If a class does not provide implementations for all methods of an interface, what must it be declared as?

A.Final
B.Abstract
C.Static
D.Private

22. What happens when a class implements multiple interfaces?

A.It can have multiple constructors
B.It inherits all abstract methods from those interfaces
C.It cannot have any fields
D.It must define a max size

23. What is dynamic method dispatch?

A.Resolving method calls at compile time
B.Using method references for lambda expressions
C.Deciding which method to execute at runtime
D.A technique to overload methods

24. What does the 'implements' keyword do in Java?

A.It declares a new interface.
B.It extends a class.
C.It indicates that a class is implementing an interface.
D.It defines a static method.

25. True or False: An abstract class can be instantiated directly.

A.True
B.False
C.Depends on the implementation
D.Only if it has concrete methods

26. True or False: A subclass can override a private method of its superclass.

A.True
B.False
C.Only if the subclass is abstract
D.Only if the private method is static

27. What is the primary purpose of a marker interface?

A.To provide method implementations
B.To serve as a type that provides metadata
C.To extend another interface
D.To enforce method signatures

28. What is the main drawback of multiple inheritance in Java?

A.It requires more memory
B.It leads to the Diamond Problem
C.It complicates the constructor chain
D.It is not supported in other languages

29. Which of the following is NOT a benefit of polymorphism?

A.Code reusability
B.Increased performance
C.Flexibility
D.Maintainability

30. Which of the following can NOT be included in an interface?

A.Default methods
B.Static methods
C.Concrete methods
D.Method signatures

31. How do interfaces support dynamic method binding?

A.By allowing method overloading
B.By using reflection
C.By enabling classes to implement multiple signatures
D.By allowing classes to be interchangeable

32. What is the purpose of the 'super' keyword in Java?

A.To access superclass members
B.To prevent method overriding
C.To declare an abstract method
D.To define a new subclass

33. In what way can an interface be used in method parameters?

A.To enforce type safety
B.To allow flexibility in method implementation
C.To specify multiple return types
D.To define constants

34. Fill in the blank: An abstract class cannot be marked as ______.

A.final
B.abstract
C.public
D.protected

35. How does Java determine which method to execute when polymorphism is involved?

A.By the method name at compile time
B.By the object's compile-time type
C.By the object's runtime type
D.By the class hierarchy

36. How can a class inherit from multiple interfaces?

A.By using the extends keyword
B.By implementing each interface with the implements keyword
C.By defining abstract methods
D.By declaring them as final

37. Which statement correctly compares an abstract class and an interface?

A.An abstract class cannot contain fields, but an interface can
B.Both can have concrete method implementations
C.An abstract class can have state, while an interface cannot
D.Both can be instantiated directly

38. What is compile-time polymorphism?

A.Polymorphism via method overriding
B.Polymorphism achieved through method overloading
C.Polymorphism that occurs only at runtime
D.Polymorphism that cannot be implemented in Java

39. What is the relationship between an interface and an abstract class in Java?

A.Both can have instance variables.
B.Both can implement methods.
C.Interfaces cannot have concrete methods, while abstract classes can.
D.Both support multiple inheritance.

40. What is the purpose of using interfaces in Java?

A.To define a contract that classes must follow
B.To allow for direct instantiation
C.To manage memory usage
D.To simplify class hierarchies

41. Fill in the blank: Overloaded methods must have different ________.

A.Return types
B.Access modifiers
C.Parameter lists
D.Method bodies

42. Which keyword is used to extend an interface?

A.extends
B.implements
C.inherits
D.interface

43. True or False: Polymorphism can lead to more maintainable code.

A.True
B.False
C.Only if used with interfaces
D.Only with abstract classes

44. True or False: Polymorphism is applicable only to methods in Java.

A.True
B.False
C.Only for static methods
D.Only for instance methods

45. What happens if an interface is modified to add a new method?

A.Existing implementations break.
B.All existing classes are updated automatically.
C.It requires all implementing classes to provide an implementation.
D.It has no impact on existing classes.

46. Which of the following statements correctly describes a difference between an abstract class and an interface in Java?

A.An abstract class can have method implementations, while an interface cannot have any before Java 8.
B.Both abstract classes and interfaces can contain static methods only.
C.An interface can have instance variables, whereas an abstract class cannot.
D.An abstract class must implement all methods defined in an interface.

47. Which statement best describes dynamic method dispatch in Java?

A.It resolves method calls at runtime based on the object's actual type.
B.It resolves method calls at compile time based on the method signature.
C.It allows a subclass to access all methods of its superclass.
D.It prevents method overriding in subclasses.

48. Which of the following statements about a default method in an interface is TRUE?

A.It provides a default implementation for a method in an interface.
B.It requires all implementing classes to override it immediately.
C.It can only be declared in abstract classes, not interfaces.
D.It cannot be used with functional interfaces.

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.