Java Polymorphism MCQ

Polymorphism is another cornerstone concept in Object-Oriented Programming (OOP) alongside inheritance, encapsulation, and abstraction. In Java, polymorphism is the capability of a method to do different things based on the object that it is acting upon. Ready to dive deep into this concept? Let's get quizzical!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. What does the word 'Polymorphism' mean in Greek?

a) Many forms
b) Single form
c) No form
d) Final form

Answer:

a) Many forms

Explanation:

'Polymorphism' originates from the Greek words 'poly' (many) and 'morph' (form), literally translating to 'many forms'.

2. Which principle allows different classes to be treated as instances of the same class through inheritance?

a) Encapsulation
b) Abstraction
c) Inheritance
d) Polymorphism

Answer:

d) Polymorphism

Explanation:

Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling generic processing and diverse class behaviors.

3. In Java, what allows us to implement runtime polymorphism?

a) Method overloading
b) Method overriding
c) Constructors
d) Static methods

Answer:

b) Method overriding

Explanation:

Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved during runtime rather than at compile-time. This is achieved through method overriding.

4. Which of these allows compile-time polymorphism?

a) Method overloading
b) Method overriding
c) Both
d) None of the above

Answer:

a) Method overloading

Explanation:

Compile-time polymorphism is achieved when we overload a method. The method call is resolved at compile time based on the method signature.

5. Which statement is true regarding polymorphism?

a) An overridden method can be less restrictive than the superclass method
b) An overridden method can be more restrictive than the superclass method
c) Overloaded methods cannot change the return type
d) Constructors can be overridden

Answer:

b) An overridden method can be more restrictive than the superclass method

Explanation:

When overriding a method, the subclass method cannot have a more restrictive access modifier than the method in the parent class.

6. Can we override static methods in Java?

a) Yes
b) No
c) Sometimes
d) Only in abstract classes

Answer:

b) No

Explanation:

Static methods are bound to a class, not an instance. Thus, they cannot be overridden for polymorphic behavior.

7. In which scenario does Java NOT allow polymorphism?

a) When overriding private methods
b) When using method overloading
c) When using interface methods
d) When using abstract methods

Answer:

a) When overriding private methods

Explanation:

Private methods are confined to their class and are not visible in subclasses, so they cannot be overridden or partake in polymorphism.

8. In polymorphism, a reference variable of the superclass can refer to the object of which classes?

a) Only the superclass
b) Only the subclass
c) Any class
d) The superclass or any of its subclasses

Answer:

d) The superclass or any of its subclasses

Explanation:

A reference variable of a superclass can refer to an object of the superclass itself or any of its subclasses.

9. Which keyword is used to call the superclass method in the overridden method?

a) this
b) super
c) extends
d) instanceof

Answer:

b) super

Explanation:

The super keyword can be used in the subclass to call the method of the superclass, especially useful when overriding methods.

10. What would be the output if you attempt to compile and run a class that has multiple methods with the same name but a different number of parameters?

a) Compilation error
b) Runtime error
c) The method with the correct number of parameters will be executed
d) All the methods will be executed sequentially

Answer:

c) The method with the correct number of parameters will be executed

Explanation:

This scenario describes method overloading. At compile time, Java uses the method signature to determine which method to call. At runtime, the JVM uses the method signature to determine the method to run.



Comments