This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What do you mean by Method Overloading in Java?
Option C
In Java, a method is a block of code that performs a specific task. One of the core features of Java is Polymorphism, which allows objects or methods to take multiple forms.
Method overloading is a type of compile-time polymorphism where a class has two or more methods with the same name but different parameter lists.
It allows programmers to use the same method name for related actions, improving readability and organization.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(15, 25));
System.out.println(calc.add(15, 12, 30));
System.out.println(calc.add(2.5, 7.5));
}
}
Q: 2What is Polymorphism in Java?
Option A
The word Polymorphism means Many Forms. It allows objects or methods to take multiple forms, enabling flexibility and reusability in code. Polymorphism improves code flexibility, readability, and maintainability. There are two main types of polymorphism in Java:
Compile-Time Polymorphism (Method Overloading):
Runtime Polymorphism (Method Overriding):
Q: 3What is method hiding?
Option B
In Java, Method Hiding occurs when a subclass defines a static method with the same signature as a static method in its superclass. Since static methods belong to the class rather than to individual objects, they are hidden, not overridden.
E.g.:
class A
{
static void show()
{
System.out.println("A show()");
}
}
class B extends A
{
static void show()
{
System.out.println("B show()");
}
}
class TestExample
{
public static void main(String args[])
{
A obj1 = new B();
B obj2 = new B();
obj1.show();
obj2.show();
}
}
OUTPUT
A show()
B show()
Here, both A and B have a static method named show(). The method in B hides the method in A.
Notice an important difference from method overriding. With an overridden instance method, Java uses the actual object type at runtime. However, with static methods, the method that is called is determined by the reference or class type.
| Method Overloading | Method Overriding | Method Hiding |
|---|---|---|
| It occurs when multiple methods in the same class have the same name but different parameter lists. | It occurs when a subclass provides its own implementation of an inherited instance method with the same signature. | It occurs when a subclass defines a static method with the same signature as a static method in the superclass. |
| It is mainly associated with compile-time polymorphism. | It is associated with runtime polymorphism. | It is related to static methods, which are resolved based on the reference/class type. |
| The methods may have different numbers or types of parameters. | The method must have the same method signature as the superclass method. | The static methods have the same method signature. |
| It can occur within the same class. | It requires a superclass and subclass relationship. | It requires a superclass and subclass relationship. |
| Return type alone cannot be used to overload a method. | The return type must be compatible when overriding a method. | The method remains static in both the superclass and subclass. |
| The compiler determines which overloaded method to call. | The appropriate overridden method is generally determined at runtime. | The method is selected according to the reference/class type, not the actual object. |
Method Overloading:
class MyCalculator
{
void add(int a, int b)
{
System.out.println(a + b);
}
void add(int a, int b, int c)
{
System.out.println(a + b + c);
}
}
Method Overriding:
class A
{
void show()
{
System.out.println("A");
}
}
class B extends A
{
void show()
{
System.out.println("B");
}
}
Method Hiding:
class A
{
static void show()
{
System.out.println("A");
}
}
class B extends A
{
static void show()
{
System.out.println("B");
}
}
Q: 4Which is an example of compile-time polymorphism?
Option B
In Java, Polymorphism means that the same method name can represent different behavior depending on the situation. Polymorphism is commonly divided into Compile-Time Polymorphism and Runtime Polymorphism.
Method Overloading is an example of Compile-Time Polymorphism. In method overloading, multiple methods have the same name but different parameter lists. The compiler determines which method should be called by examining the number, type, or order of arguments during compilation.
E.g.:
class MyCalculator
{
int add(int a,int b)
{
return a+b;
}
int add(int a,int b,int c)
{
return a+b+c;
}
double add(double a,double b)
{
return a+b;
}
public static void main(String args[])
{
MyCalculator calc = new MyCalculator();
System.out.println(calc.add(11,22));
System.out.println(calc.add(11,22,33));
System.out.println(calc.add(11.5,22.5));
}
}
OUTPUT
33
66
34.0
The Method Overriding and Dynamic Method Dispatch are associated with Runtime Polymorphism, because the overridden method is selected according to the actual object during execution.
Q: 5Method overriding can be prevented by using final as a modifier at _________.
Option B
In Java, Method Overriding occurs when a subclass provides its own implementation of an inherited method with the same method signature.
The final keyword can be used to prevent a method from being overridden by a subclass. When a method is declared with the final modifier, its implementation cannot be replaced in a subclass.
E.g.:
class Parent
{
final void display()
{
System.out.println("Parent Class Method");
}
}
class Child extends Parent
{
void display() // Compilation Error
{
System.out.println("Child Class Method");
}
}
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.