Search Tutorials


Java Method Overloading MCQ Questions And Answers | JavaInUse

Java Method Overloading MCQ Questions And Answers

Q. What is method overloading in Java?

A. Having two methods with the same name but different return types
B. Having multiple methods within a class with the same name but different parameters
C. Having methods that can override each other
D. Having only one method in a class

Q. Can method overloading be achieved by changing the return type of the method?

A. Yes, method overloading can be achieved by changing the return type of the method
B. No, method overloading cannot be achieved by changing the return type of the method
C. It depends on the Java version being used
D. None of the above

Q. Is it required for overloaded methods to have the same return type?

A. Yes, overloaded methods must have the same return type
B. No, overloaded methods can have different return types
C. It depends on the Java version being used
D. None of the above

Q. Can method overloading be achieved by changing the access modifiers of the method?

A. Yes, method overloading can be achieved by changing access modifiers
B. No, method overloading cannot be achieved by changing access modifiers
C. It depends on the Java version being used
D. None of the above

Q. Can method overloading be achieved by changing the exception thrown by the method?

A. Yes, method overloading can be achieved by changing exceptions thrown
B. No, method overloading cannot be achieved by changing exceptions thrown
C. It depends on the Java version being used
D. None of the above

Q. What happens if two methods have the same name and parameters but different return types?

A. The compiler throws an error
B. The methods are considered overloaded
C. The second method is considered overriding the first method
D. The methods cannot have the same name

Q. Can method overloading be achieved by changing the order of parameters in the method?

A. Yes, method overloading can be achieved by changing the parameter order
B. No, method overloading cannot be achieved by changing the parameter order
C. It depends on the Java version being used
D. None of the above

Q. Can method overloading be achieved in the same class or in a subclass?

A. Method overloading can only be achieved in the same class
B. Method overloading can only be achieved in a subclass
C. Method overloading can be achieved in both the same class and in a subclass
D. None of the above

Q. When is method overloading useful in Java programming?

A. When you want to have multiple methods with the same name and return type
B. When you want to perform different tasks based on the number of parameters
C. When you want to have methods that can override each other
D. When you want to have methods with the same name but different parameters

Q. Can methods with the same name and parameters but different return types be defined in the same class?

A. Yes, multiple methods with the same name and parameters but different return types can be defined in the same class
B. No, methods with the same name and parameters must have the same return type in the same class
C. It depends on the access modifiers of the methods
D. None of the above

Q. What is Java method overloading?

A.
    // Method Overloading Example
    public class Calculator {
        public int add(int a, int b) {
            return a + b;
        }
        
        public double add(double a, double b) {
            return a + b;
        }
    }
    
B.
    // Method Overloading Example
    public class Calculator {
        public int add(int a, int b) {
            return a + b;
        }
        
        public int add(int a, int b, int c) {
            return a + b + c;
        }
    }
    
C.
    // Method Overloading Example
    public class Calculator {
        public int add(int a, int b) {
            return a + b;
        }
        
        public String add(String a, String b) {
            return a + b;
        }
    }
    
D.
    // Method Overloading Example
    public class Calculator {
        public double add(double a, double b) {
            return a + b;
        }
        
        public double add(double a, double b, double c) {
            return a + b + c;
        }
    }
    

Q. What is the significance of method overloading in Java?

A.
    // Method Overloading Example
    public class Printer {
        public void print(String message) {
            System.out.println(message);
        }
        
        public void print(int number) {
            System.out.println(number);
        }
    }
    
B.
    // Method Overloading Example
    public class Printer {
        public void print(String message) {
            System.out.println(message);
        }
        
        public void print(String message, int times) {
            for (int i = 0; i < times; i++) {
                System.out.println(message);
            }
        }
    }
    
C.
    // Method Overloading Example
    public class Printer {
        public void print(String message) {
            System.out.println(message);
        }
        
        public void print(String prefix, String message) {
            System.out.println(prefix + message);
        }
    }
    
D.
    // Method Overloading Example
    public class Printer {
        public void print(String message) {
            System.out.println(message);
        }
        
        public String print(String message) {
            return message;
        }
    }
    

Q. Which of the following is NOT a valid way to overload a method in Java?

A.
    // Method Overloading Example
    public class MathOperations {
        public int multiply(int a, int b) {
            return a * b;
        }
        
        public double multiply(int a, int b) {
            return a * b;
        }
    }
    
B.
    // Method Overloading Example
    public class MathOperations {
        public int add(int a, int b) {
            return a + b;
        }
        
        public int add(int a, int b, int c) {
            return a + b + c;
        }
    }
    
C.
    // Method Overloading Example
    public class MathOperations {
        public void subtract(int a, int b) {
            System.out.println(a - b);
        }
        
        public void subtract(double a, double b) {
            System.out.println(a - b);
        }
    }
    
D.
    // Method Overloading Example
    public class MathOperations {
        public int divide(int a, int b) {
            return a / b;
        }
        
        public int divide(int a, int b, int c) {
            return a / b / c;
        }
    }
    





Q. Which of the following code snippets demonstrates method overloading in Java?

A.
    // Method Overloading Example
    public class Shapes {
        public double calculateArea(double radius) {
            return Math.PI * radius * radius;
        }
        
        public double calculateArea(double side1, double side2) {
            return side1 * side2;
        }
    }
    
B.
    // Method Overloading Example
    public class Shapes {
        public double calculatePerimeter(double side) {
            return 4 * side;
        }
        
        public double calculatePerimeter(double length, double breadth) {
            return 2 * (length + breadth);
        }
    }
    
C.
    // Method Overloading Example
    public class Shapes {
        public int calculateArea(int length, int breadth) {
            return length * breadth;
        }
        
        public int calculateArea(int side) {
            return side * side;
        }
    }
    
D.
    // Method Overloading Example
    public class Shapes {
        public void drawShape(String shape) {
            // Drawing logic for specific shape
        }
        
        public void drawShape(String color, String shape) {
            // Drawing logic with a specific color
        }
    }
    

Q. Why is method overloading beneficial in Java?

A.
    // Method Overloading Example
    public class Converter {
        public double convert(double miles) {
            return miles * 1.60934;
        }
        
        public double convert(double kilometers) {
            return kilometers / 1.60934;
        }
    }
    
B.
    // Method Overloading Example
    public class Converter {
        public String convertTime(int hours, int minutes) {
            return hours + " hours and " + minutes + " minutes";
        }
        
        public String convertTime(int totalMinutes) {
            int hours = totalMinutes / 60;
            int remainingMinutes = totalMinutes % 60;
            return hours + " hours and " + remainingMinutes + " minutes";
        }
    }
    
C.
    // Method Overloading Example
    public class Converter {
        public void displayNumber(int num) {
            System.out.println(num);
        }
        
        public void displayNumber(double num) {
            System.out.println(num);
        }
    }
    
D.
    // Method Overloading Example
    public class Converter {
        public boolean checkEvenOdd(int num) {
            return num % 2 == 0;
        }
        
        public boolean checkEvenOdd(double num) {
            return num % 2 == 0;
        }
    }
    

Q. Can method overloading be based solely on the return type?

A.
    // Method Overloading Example
    public class Calculator {
        public int add(int a, int b) {
            return a + b;
        }
        
        public double add(int a, int b) {
            return a + b;
        }
    }
    
B.
    // Method Overloading Example
    public class Calculator {
        public int subtract(int a, int b) {
            return a - b;
        }
        
        public int multiply(int a, int b) {
            return a * b;
        }
    }
    
C.
    // Method Overloading Example
    public class Calculator {
        public double divide(double a, double b) {
            return a / b;
        }
        
        public int divide(int a, int b) {
            return a / b;
        }
    }
    
D.
    // Method Overloading Example
    public class Calculator {
        public void performOperation(int a, int b) {
            // Perform operation logic
        }
        
        public int performOperation(int a, int b, int c) {
            // Perform operation logic
            return a + b + c;
        }
    }
    

Q. What happens if two overloaded methods have the same number and type of parameters but different return types?

A.
    // Method Overloading Example
    public class Printer {
        public void printMessage(String message) {
            System.out.println(message);
        }
        
        public String printMessage(String message) {
            return message;
        }
    }
    
B.
    // Method Overloading Example
    public class Printer {
        public int getMessageLength(String message) {
            return message.length();
        }
        
        public String getMessageLength(String message) {
            return "Length: " + message.length();
        }
    }
    
C.
    // Method Overloading Example
    public class Printer {
        public void printNumber(int number) {
            System.out.println(number);
        }
        
        public double printNumber(int number) {
            return number;
        }
    }
    
D.
    // Method Overloading Example
    public class Printer {
        public String formatMessage(String message) {
            return "Formatted: " + message;
        }
        
        public int formatMessage(String message) {
            return message.length();
        }
    }
    

Q. Is method overloading determined by the return type in Java?

A.
    // Method Overloading Example
    public class Converter {
        public double convert(double pounds) {
            return pounds * 0.453592;
        }
        
        public double convert(int ounces) {
            return ounces * 0.0283495;
        }
    }
    
B.
    // Method Overloading Example
    public class Converter {
        public int round(double number) {
            return (int) Math.round(number);
        }
        
        public double round(double number) {
            return Math.round(number);
        }
    }
    
C.
    // Method Overloading Example
    public class Converter {
        public void displayResult(double result) {
            System.out.println(result);
        }
        
        public String displayResult(double result) {
            return "Result: " + result;
        }
    }
    
D.
    // Method Overloading Example
    public class Converter {
        public boolean isPositive(int number) {
            return number > 0;
        }
        
        public boolean isPositive(double number) {
            return number > 0;
        }
    }
    

Q. Which of the following code snippets demonstrates correct method overloading in Java?

A.
    // Method Overloading Example
    public class Person {

		public void greet() {
            System.out.println("Hello!");
        }
        
        public void greet(String name) {
            System.out.println("Hello, " + name + "!");
        }
    }
    
B.
    // Method Overloading Example
    public class Person {
        public void sayHello() {
            System.out.println("Hello!");
        }
        
        public String sayHello(String name) {
            return "Hello, " + name + "!";
        }
    }
    
C.
    // Method Overloading Example
    public class Person {
        public void speak() {
            System.out.println("Hello!");
        }
        
        public void speak(String name) {
            System.out.println("Hello, " + name + "!");
        }
    }
    
D.
    // Method Overloading Example
    public class Person {
        public void introduce(String name) {
            System.out.println("My name is " + name);
        }
        
        public void introduce(int age) {
            System.out.println("I am " + age + " years old");
        }
    }
    

Q. Can method overloading be based on the parameter names in Java?

A.
    // Method Overloading Example
    public class TextFormatter {
        public String formatText(String txt) {
            return txt.toUpperCase();
        }
        
        public String formatText(String text) {
            return text.toLowerCase();
        }
    }
    
B.
    // Method Overloading Example
    public class TextFormatter {
        public String capitalize(String text) {
            return text.toUpperCase();
        }
        
        public String capitalize(String str) {
            return str.toLowerCase();
        }
    }
    
C.
    // Method Overloading Example
    public class TextFormatter {
        public void printText(String message) {
            System.out.println(message);
        }
        
        public void printText(String text) {
            System.out.println(text);
        }
    }
    
D.
    // Method Overloading Example
    public class TextFormatter {
        public int calculateLength(String input) {
            return input.length();
        }
        
        public double calculateLength(String str) {
            return str.length();
        }
    }