Question
Which of the following scenarios illustrates runtime
polymorphism?Solution
Runtime polymorphism (or dynamic method dispatch) occurs when a method's implementation is determined at runtime. This is achieved using method overriding in OOP. A derived class overrides a method of the base class, and the method to execute is determined by the type of the object being referenced at runtime. Example: class Animal { void sound() { System.out.println("Animal makes a sound"); }} class Dog extends Animal { void sound() { System.out.println("Dog barks"); }} public class Main { public static void main(String[] args) { Animal animal = new Dog(); // Base class reference pointing to a derived class object animal.sound(); // Outputs: Dog barks }} Why Other Options Are Incorrect: 1. Method with the same name but different parameters within the same class: This is compile-time polymorphism (method overloading), not runtime polymorphism. 2. Default arguments in methods: Default arguments provide flexibility but are unrelated to polymorphism. They simplify method calls without changing the behavior based on the object’s runtime type. 3. Static methods for shared functionality: Static methods are class-level and cannot participate in runtime polymorphism, as they are resolved at compile time. 4. Constructor with a parameter list in the derived class: Constructors are not polymorphic as they do not inherit or override behavior in OOP.
Find the remainder when the number (196n + 75) is divided by 14.
There are five numbers A, B, C, D and E. The ratio between the numbers of B and D is (y+1) : 3 respectively. The number C is 12.5% more than the number ...
Sum of squares of three consecutive numbers is 1589. Find the sum of first and third number.
The arithmetic mean of two values ‘a’ and ‘b’ is 16, and their geometric mean is 10√2. Find the positive difference between the numbers, given...
Find the difference between of the greatest among the numbers 0.82, 0.802, 0.85, 0.085 with the smallest among the numbers 0.3, 0.03, 0.203, 2.03
A bag contains coins of ₹1, ₹2, and ₹5 denominations in the ratio 5:2:3, respectively. If the total value of the coins is ₹1,224, how many ₹5 ...
Find the range of the given data:
65, 85, 45, 20, 35, 90, 100, 60, 55
What is the ratio of the sum of the squares of the first 8 natural numbers to the sum of the first 12 odd natural numbers?
A number exceeds 45 times its reciprocal by 5/6. Find the number.
Find the sum of the first 40 natural numbers.