Java Methods (Functions) Practice Questions with Solutions

Methods are one of the most important concepts in Java programming. A method is a reusable block of code that performs a specific task. Instead of writing the same code repeatedly, you can place it inside a method and call it whenever needed.

Methods make programs:

  • More organized
  • Easier to read
  • Easier to maintain
  • Reusable
  • Less repetitive

Every Java program uses methods. In fact, the main() method is itself a method.

Methods are widely used in:

  • Banking Applications
  • Student Management Systems
  • Inventory Software
  • Android Applications
  • Enterprise Applications
  • Web Applications
  • APIs
  • Automation Tools
  • Data Processing Programs

A Java method generally consists of:

  • Access Modifier
  • Return Type
  • Method Name
  • Parameters (Optional)
  • Method Body

Example:

public static void displayMessage() {

    System.out.println("Welcome to Java!");

}

The method can then be called as:

displayMessage();

In this chapter, you’ll solve beginner-friendly and interview-oriented Java method practice questions. Each question includes a complete Java solution, sample input/output, explanation, and concepts covered to strengthen your understanding of Java methods. Java Methods (Functions) practice questions with solutions help to understand the concepts.


1. Java Program to Create and Call a Method

Problem Statement

Write a Java program to create a method that prints “Welcome to Java Programming” and call it from the main() method.

Java Solution

public class Main {

    static void displayMessage() {

        System.out.println("Welcome to Java Programming");

    }

    public static void main(String[] args) {

        displayMessage();

    }

}

Sample Output

Welcome to Java Programming

Explanation

A method named displayMessage() is created using the void return type because it doesn’t return any value.

The method is called from the main() method.

Concepts Covered

  • Method Declaration
  • Method Calling
  • static Method
  • void Return Type

2. Java Program to Add Two Numbers Using a Method

Problem Statement

Write a Java program to create a method that accepts two integers and prints their sum.

Java Solution

public class Main {

    static void addNumbers(int a, int b) {

        System.out.println("Sum = " + (a + b));

    }

    public static void main(String[] args) {

        addNumbers(25, 35);

    }

}

Sample Output

Sum = 60

Explanation

The method receives two integer parameters and prints their sum.

Concepts Covered

  • Method Parameters
  • Method Calling
  • Arithmetic Operations

3. Java Program to Find the Square of a Number Using a Method

Problem Statement

Write a Java program to create a method that returns the square of a given number.

Java Solution

public class Main {

    static int square(int number) {

        return number * number;

    }

    public static void main(String[] args) {

        int result = square(8);

        System.out.println("Square = " + result);

    }

}

Sample Output

Square = 64

Explanation

The method returns the square of the given number using the return statement.

The returned value is stored in a variable and displayed.

Concepts Covered

  • Return Type
  • return Statement
  • Method Calling
  • Arithmetic Operations

4. Java Program to Find the Maximum of Two Numbers Using a Method

Problem Statement

Write a Java program to create a method that accepts two numbers and returns the larger number.

Java Solution

public class Main {

    static int findMaximum(int firstNumber, int secondNumber) {

        if (firstNumber > secondNumber) {

            return firstNumber;

        } else {

            return secondNumber;

        }

    }

    public static void main(String[] args) {

        int maximum = findMaximum(85, 42);

        System.out.println("Largest Number = " + maximum);

    }

}

Sample Output

Largest Number = 85

Explanation

The method compares both numbers.

  • If the first number is larger, it returns the first number.
  • Otherwise, it returns the second number.

The returned value is stored inside the maximum variable.

Concepts Covered

  • Methods
  • Return Type
  • if-else Statement
  • Method Parameters

5. Java Program to Check Whether a Number is Even or Odd Using a Method

Problem Statement

Write a Java program to create a method that checks whether a number is even or odd.

Java Solution

public class Main {

    static void checkEvenOdd(int number) {

        if (number % 2 == 0) {

            System.out.println("Even Number");

        } else {

            System.out.println("Odd Number");

        }

    }

    public static void main(String[] args) {

        checkEvenOdd(27);

    }

}

Sample Output

Odd Number

Explanation

The method receives a number as a parameter.

Using the modulus operator (%):

  • Remainder = 0 → Even
  • Otherwise → Odd

Since the method only displays the result, it uses the void return type.

Concepts Covered

  • void Method
  • Method Parameters
  • Modulus Operator
  • if-else Statement

6. Java Program to Find the Factorial of a Number Using a Method

Problem Statement

Write a Java program to create a method that calculates the factorial of a given number and returns the result.

Java Solution

public class Main {

    static long factorial(int number) {

        long result = 1;

        for (int i = 1; i <= number; i++) {

            result *= i;

        }

        return result;

    }

    public static void main(String[] args) {

        System.out.println("Factorial = " + factorial(5));

    }

}

Sample Output

Factorial = 120

Explanation

The method multiplies all numbers from 1 to the given number and returns the factorial.

Example:

5! = 5 × 4 × 3 × 2 × 1 = 120

Concepts Covered

  • Methods
  • Return Type
  • for Loop
  • Factorial Logic

7. Java Program to Check Whether a Number is Prime Using a Method

Problem Statement

Write a Java program to create a method that determines whether a number is prime.

Java Solution

public class Main {

    static boolean isPrime(int number) {

        if (number <= 1) {

            return false;

        }

        for (int i = 2; i <= number / 2; i++) {

            if (number % i == 0) {

                return false;

            }

        }

        return true;

    }

    public static void main(String[] args) {

        if (isPrime(29)) {

            System.out.println("Prime Number");

        } else {

            System.out.println("Not a Prime Number");

        }

    }

}

Sample Output

Prime Number

Explanation

The method checks divisibility from 2 to number / 2.

If the number is divisible by any value, it returns false; otherwise, it returns true.

Concepts Covered

  • Boolean Methods
  • Return Statement
  • Prime Number Logic
  • for Loop

8. Java Program to Reverse a Number Using a Method

Problem Statement

Write a Java program to create a method that returns the reverse of a given number.

Java Solution

public class Main {

    static int reverseNumber(int number) {

        int reverse = 0;

        while (number != 0) {

            int digit = number % 10;

            reverse = reverse * 10 + digit;

            number /= 10;

        }

        return reverse;

    }

    public static void main(String[] args) {

        System.out.println("Reverse Number = " + reverseNumber(12345));

    }

}

Sample Output

Reverse Number = 54321

Explanation

The method extracts the last digit using % 10, appends it to the reversed number, and removes the last digit using / 10.

Finally, it returns the reversed number.

Concepts Covered

  • Methods
  • while Loop
  • Return Statement
  • Reverse Number Logic

9. Java Program to Find the Sum of Digits Using a Method

Problem Statement

Write a Java program to create a method that returns the sum of digits of a number.

Java Solution

public class Main {

    static int sumOfDigits(int number) {

        int sum = 0;

        while (number != 0) {

            sum += number % 10;

            number /= 10;

        }

        return sum;

    }

    public static void main(String[] args) {

        System.out.println("Sum of Digits = " + sumOfDigits(9876));

    }

}

Sample Output

Sum of Digits = 30

Explanation

Each digit is extracted using the modulus operator and added to the sum variable.

Example:

9 + 8 + 7 + 6 = 30

Concepts Covered

  • Methods
  • while Loop
  • Arithmetic Operations
  • Return Statement

10. Java Program to Print a Multiplication Table Using a Method

Problem Statement

Write a Java program to create a method that prints the multiplication table of a given number.

Java Solution

public class Main {

    static void multiplicationTable(int number) {

        for (int i = 1; i <= 10; i++) {

            System.out.println(number + " x " + i + " = " + (number * i));

        }

    }

    public static void main(String[] args) {

        multiplicationTable(6);

    }

}

Sample Output

6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Explanation

The method receives a number as a parameter and prints its multiplication table from 1 to 10.

Since the method only displays output, it uses the void return type.

Concepts Covered

  • Methods
  • for Loop
  • Method Parameters
  • Multiplication Table

11. Java Program to Calculate the Power of a Number Using a Method

Problem Statement

Write a Java program to create a method that calculates the power of a number.

Java Solution

public class Main {

    static int power(int base, int exponent) {

        int result = 1;

        for (int i = 1; i <= exponent; i++) {

            result *= base;

        }

        return result;

    }

    public static void main(String[] args) {

        System.out.println("Result = " + power(2, 5));

    }

}

Sample Output

Result = 32

Explanation

The method multiplies the base number repeatedly according to the exponent value.

Example:

2⁵ = 2 × 2 × 2 × 2 × 2 = 32

Concepts Covered

  • Methods
  • Return Type
  • for Loop
  • Power Calculation

12. Java Program to Find the Greatest Common Divisor (GCD) Using a Method

Problem Statement

Write a Java program to create a method that returns the Greatest Common Divisor (GCD) of two numbers.

Java Solution

public class Main {

    static int gcd(int firstNumber, int secondNumber) {

        while (secondNumber != 0) {

            int temp = secondNumber;

            secondNumber = firstNumber % secondNumber;

            firstNumber = temp;

        }

        return firstNumber;

    }

    public static void main(String[] args) {

        System.out.println("GCD = " + gcd(24, 36));

    }

}

Sample Output

GCD = 12

Explanation

The program uses the Euclidean Algorithm, one of the fastest methods for finding the GCD.

It repeatedly calculates the remainder until it becomes zero.

Concepts Covered

  • Methods
  • Euclidean Algorithm
  • while Loop
  • Return Statement

13. Java Program to Find the Least Common Multiple (LCM) Using a Method

Problem Statement

Write a Java program to create a method that returns the Least Common Multiple (LCM) of two numbers.

Java Solution

public class Main {

    static int gcd(int firstNumber, int secondNumber) {

        while (secondNumber != 0) {

            int temp = secondNumber;

            secondNumber = firstNumber % secondNumber;

            firstNumber = temp;

        }

        return firstNumber;

    }

    static int lcm(int firstNumber, int secondNumber) {

        return (firstNumber * secondNumber) / gcd(firstNumber, secondNumber);

    }

    public static void main(String[] args) {

        System.out.println("LCM = " + lcm(12, 18));

    }

}

Sample Output

LCM = 36

Explanation

The Least Common Multiple is calculated using the formula:

LCM = (a × b) / GCD

The program first finds the GCD and then calculates the LCM.

Concepts Covered

  • Methods
  • Method Calling Another Method
  • GCD
  • LCM

14. Java Program to Demonstrate Method Overloading

Problem Statement

Write a Java program to demonstrate method overloading.

Java Solution

public class Main {

    static int add(int a, int b) {

        return a + b;

    }

    static int add(int a, int b, int c) {

        return a + b + c;

    }

    public static void main(String[] args) {

        System.out.println(add(10, 20));

        System.out.println(add(10, 20, 30));

    }

}

Sample Output

30
60

Explanation

Method overloading allows multiple methods to have the same name but different parameter lists.

The compiler automatically chooses the correct method based on the arguments.

Concepts Covered

  • Method Overloading
  • Return Type
  • Parameters
  • Compile-Time Polymorphism

15. Java Program to Print Fibonacci Series Using a Method

Problem Statement

Write a Java program to create a method that prints the Fibonacci series up to N terms.

Java Solution

public class Main {

    static void fibonacci(int terms) {

        int first = 0;
        int second = 1;

        for (int i = 1; i <= terms; i++) {

            System.out.print(first + " ");

            int next = first + second;

            first = second;

            second = next;

        }

    }

    public static void main(String[] args) {

        fibonacci(10);

    }

}

Sample Output

0 1 1 2 3 5 8 13 21 34

Explanation

The method generates each Fibonacci number by adding the previous two numbers.

Example:

0 1 1 2 3 5 8 13 21 34

Concepts Covered

  • Methods
  • for Loop
  • Fibonacci Logic
  • Variables

Chapter Summary

In this chapter, you learned one of the most important concepts in Java programming—Methods (Functions). Methods help divide a program into smaller, reusable blocks of code, making applications easier to understand, maintain, and debug.

Throughout this chapter, you practiced solving real-world Java programs using methods, including:

  • Creating and calling methods
  • Methods with parameters
  • Methods with return values
  • Void methods
  • Finding the maximum of two numbers
  • Checking even and odd numbers
  • Calculating factorials
  • Checking prime numbers
  • Reversing numbers
  • Finding the sum of digits
  • Printing multiplication tables
  • Calculating powers
  • Finding GCD and LCM
  • Method overloading
  • Printing Fibonacci series

These examples demonstrate how methods improve code reusability and reduce duplication. Methods are extensively used in Java desktop applications, Android apps, web applications, enterprise software, APIs, and automation projects.

Understanding methods is also essential before learning advanced Java topics such as Object-Oriented Programming (OOP), Classes and Objects, Constructors, Inheritance, and Polymorphism.


Key Takeaways

  • A method is a reusable block of code.
  • Methods improve readability and maintainability.
  • Java methods can accept parameters.
  • Methods can return values using the return statement.
  • The void keyword is used when no value needs to be returned.
  • Static methods can be called without creating an object.
  • Method overloading allows multiple methods with the same name but different parameter lists.
  • Recursive methods solve problems by calling themselves.
  • Methods reduce code duplication and improve modular programming.
  • Mastering methods is essential for object-oriented programming and coding interviews.

Frequently Asked Questions (FAQs)

1. What is a method in Java?

A method is a reusable block of code that performs a specific task. It can be called multiple times from different parts of a program.


2. What is the difference between a method and a function in Java?

In Java, the terms method and function are often used interchangeably. Technically, Java uses the term method because every method belongs to a class.


3. What is the syntax of a Java method?

returnType methodName(parameters) {

    // method body

}

Example:

static int add(int a, int b) {

    return a + b;

}

4. What is a void method?

A void method performs a task but does not return any value.

Example:

static void display() {

    System.out.println("Hello Java");

}

5. What is a return type in Java?

The return type specifies the type of value returned by a method.

Examples:

  • int
  • double
  • boolean
  • String
  • char
  • void

6. What is method overloading?

Method overloading allows multiple methods to have the same name but different parameter lists.

Example:

add(int a, int b)

add(int a, int b, int c)

The compiler selects the appropriate method based on the number and type of arguments.


7. Why are methods important in Java?

Methods help to:

  • Reuse code
  • Reduce duplication
  • Improve readability
  • Simplify debugging
  • Organize large applications into smaller modules

8. Where are Java methods used?

Methods are used in:

  • Desktop Applications
  • Android Applications
  • Enterprise Software
  • Banking Systems
  • Inventory Management Systems
  • Web Applications
  • APIs
  • Automation Tools
  • Competitive Programming
  • Coding Interviews

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top