Method Overloading MCQ
What is method overloading in Java?
A. Defining multiple methods with the same name and different return types
B. Defining multiple methods with the same name and different parameter lists
C. Defining multiple methods with different names but the same functionality
D. Defining a method multiple times within the same class
Which of the following is a correct example of method overloading?
A. void add(int a, int b) {} void add(double a, double b) {}
B. int add(int a, int b) {} void add(int a, int b) {}
C. void add(int a) {} void subtract(int a) {}
D. void add(int a) {} int add(int a) {}
Can method overloading be achieved by changing the return type alone?
A. Yes
B. No
C. Only if the methods are static
D. Only if the methods are final
What is the key requirement for method overloading?
A. Different method names
B. Different return types
C. Different access modifiers
D. Different parameter lists
Can overloaded methods have different access modifiers?
A. Yes
B. No
C. Only if they are static
D. Only if they are private
Which of the following correctly shows method overloading?
A. void display(int a) {} void display(String a) {}
B. void display(int a) {} int display(int a) {}
C. void display(int a) {} void show(int a) {}
D. void display() {} void display() {}
Is it possible to overload a method with different exception lists?
A. Yes
B. No
C. Only with checked exceptions
D. Only with unchecked exceptions
Can constructors be overloaded in Java?
A. Yes
B. No
C. Only if they are public
D. Only if they are protected
Which of the following methods is an example of overloading?
A. public void compute(int a) {} public void compute(int b) {}
B. public void compute(int a) {} public void compute(int a, int b) {}
C. public int compute() {} public int compute() {}
D. public void compute(int a) {} public void calculate(int a) {}
Can overloaded methods have different return types if they have the same parameter list?
A. Yes, as long as the method names are different
B. No, method overloading cannot be based on return types alone
C. Yes, in the case of static methods
D. Yes, if they throw different exceptions
Which of the following is NOT a valid reason for method overloading?
A. To perform a similar operation on different data types
B. To define methods with different access levels
C. To handle different numbers of arguments
D. To use different return types
What happens if two overloaded methods have the same parameter types but different parameter names?
A. The program will compile but will throw a runtime exception
B. The program will compile successfully
C. The program will not compile
D. The compiler will choose the method based on the parameter names
How does the compiler distinguish between overloaded methods?
A. By the method names
B. By the return types
C. By the method parameter lists
D. By the access modifiers
Can overloaded methods be defined in different classes?
A. Yes, if one class extends another
B. No, they must be in the same class
C. Yes, if they are in the same package
D. No, only methods within the same class can be overloaded
Is it possible to overload a method with the same number and types of parameters but in a different order?
A. Yes
B. No
C. Only if the method is static
D. Only if the method is final
Which of the following is an example of method overloading?
A. public void print() {} public void print(int x) {}
B. public void print() {} public int print() {}
C. public void print(int x) {} public void show(int x) {}
D. public void print() {} public void display() {}
Which of the following is not a characteristic of method overloading?
A. The methods must have different parameter lists
B. The methods must have the same name
C. The methods can have different return types
D. The methods must belong to the same class
Can overloaded methods have different return types if they have different parameter lists?
A. Yes
B. No
C. Only if they are static
D. Only if they are final
What happens if a class has overloaded methods with parameters that are subclasses of each other?
A. The compiler will choose the most specific method
B. The program will not compile
C. The method with the superclass parameter will always be chosen
D. An ambiguity error will occur
Can method overloading be used with a method that has variable-length arguments?
A. No, it causes ambiguity
B. Yes, but only with one variable-length argument method
C. Yes, but only if the variable-length argument is the last parameter
D. Yes, without any restrictions
Answers:
B. Defining multiple methods with the same name and different parameter lists
A. void add(int a, int b) {} void add(double a, double b) {}
B. No
D. Different parameter lists
A. Yes
A. void display(int a) {} void display(String a) {}
A. Yes
A. Yes
B. public void compute(int a) {} public void compute(int a, int b) {}
B. No, method overloading cannot be based on return types alone
B. To define methods with different access levels
B. The program will compile successfully
C. By the method parameter lists
A. Yes, if one class extends another
A. Yes
A. public void print() {} public void print(int x) {}
D. The methods must belong to the same class
A. Yes
A. The compiler will choose the most specific method
C. Yes, but only if the variable-length argument is the last parameter
No comments:
Post a Comment