What is the process of defining a method in terms of itself that is a method that calls itself 1 point polymorphism abstraction encapsulation recursion?

In this tutorial, you will learn about Java recursive function, its advantages and disadvantages.

In Java, a method that calls itself is known as a recursive method. And, this process is known as recursion.

A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.


How Recursion works?

What is the process of defining a method in terms of itself that is a method that calls itself 1 point polymorphism abstraction encapsulation recursion?
Working of Java Recursion

In the above example, we have called the recurse() method from inside the main method. (normal method call). And, inside the recurse() method, we are again calling the same recurse method. This is a recursive call.

In order to stop the recursive call, we need to provide some conditions inside the method. Otherwise, the method will be called infinitely.

Hence, we use the if...else statement (or similar approach) to terminate the recursive call inside the method.


Example: Factorial of a Number Using Recursion

class Factorial {

    static int factorial( int n ) {
        if (n != 0)  // termination condition
            return n * factorial(n-1); // recursive call
        else
            return 1;
    }

    public static void main(String[] args) {
        int number = 4, result;
        result = factorial(number);
        System.out.println(number + " factorial = " + result);
    }
}

Output:

4 factorial = 24

In the above example, we have a method named factorial(). The factorial() is called from the main() method. with the number variable passed as an argument.

Here, notice the statement,

return n * factorial(n-1);

The factorial() method is calling itself. Initially, the value of n is 4 inside factorial(). During the next recursive call, 3 is passed to the factorial() method. This process continues until n is equal to 0.

When n is equal to 0, the if statement returns false hence 1 is returned. Finally, the accumulated result is passed to the main() method.


Working of Factorial Program

The image below will give you a better idea of how the factorial program is executed using recursion.

What is the process of defining a method in terms of itself that is a method that calls itself 1 point polymorphism abstraction encapsulation recursion?
Factorial Program using Recursion

Advantages and Disadvantages of Recursion

When a recursive call is made, new storage locations for variables are allocated on the stack. As, each recursive call returns, the old variables and parameters are removed from the stack. Hence, recursion generally uses more memory and is generally slow.

On the other hand, a recursive solution is much simpler and takes less time to write, debug and maintain.

Recommended Reading: What are the advantages and disadvantages of recursion?

Java MCQs on overloading methods & argument passing in Java Programming Language.

1. What is the process of defining two or more methods within same class that have same name but different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) none of the mentioned

Answer: a
Clarification: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.

2. Which of these can be overloaded?
a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned

Answer: c
Clarification: None.

3. Which of these is correct about passing an argument by call-by-value process?
a) Copy of argument is made into the formal parameter of the subroutine
b) Reference to original argument is passed to formal parameter of the subroutine
c) Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
d) Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument

Answer: a
Clarification: When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.

4. What is the process of defining a method in terms of itself, that is a method that calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion

Answer: d
Clarification: None.

5. What will be the output of the following Java code?

  1. class San
  2. {
  3.  public void m1 (int i,float f)
  4.  {
  5.   System.out.println(" int float method");
  6.  }
  7.  
  8.  public void m1(float f,int i);
  9.   {
  10.   System.out.println("float int method");
  11.   }
  12.  
  13.   public static void main(String[]args)
  14.   {
  15.     San s=new San();
  16.         s.m1(20,20);
  17.   }
  18. }

a) int float method
b) float int method
c) compile time error
d) run time error

Answer: c
Clarification: While resolving overloaded method, compiler automatically promotes if exact match is not found. But in this case, which one to promote is an ambiguity.

6. What will be the output of the following Java code?

  1.     class overload 
  2.     {
  3.         int x;
  4.  	int y;
  5.         void add(int a) 
  6.         {
  7.             x =  a + 1;
  8.         }
  9.         void add(int a, int b)
  10.         {
  11.             x =  a + 2;
  12.         }        
  13.     }    
  14.     class Overload_methods 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             overload obj = new overload();   
  19.             int a = 0;
  20.             obj.add(6);
  21.             System.out.println(obj.x);     
  22.         }
  23.    }

a) 5
b) 6
c) 7
d) 8

Answer: c
Clarification: None.
output:

$ javac Overload_methods.java
$ java Overload_methods
7

7. What will be the output of the following Java code?

  1.     class overload 
  2.     {
  3.         int x;
  4.  	int y;
  5.         void add(int a)
  6.         {
  7.             x =  a + 1;
  8.         }
  9.         void add(int a , int b)
  10.         {
  11.             x =  a + 2;
  12.         }        
  13.     }    
  14.     class Overload_methods 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             overload obj = new overload();   
  19.             int a = 0;
  20.             obj.add(6, 7);
  21.             System.out.println(obj.x);     
  22.         }
  23.     }

a) 6
b) 7
c) 8
d) 9

Answer: c
Clarification: None.
output:

$ javac Overload_methods.java
$ java Overload_methods
8

8. What will be the output of the following Java code?

  1.    class overload 
  2.    {
  3.         int x;
  4.  	double y;
  5.         void add(int a , int b) 
  6.         {
  7.             x = a + b;
  8.         }
  9.         void add(double c , double d)
  10.         {
  11.             y = c + d;
  12.         }
  13.         overload() 
  14.         {
  15.             this.x = 0;
  16.             this.y = 0;
  17.         }        
  18.     }    
  19.     class Overload_methods 
  20.     {
  21.         public static void main(String args[])
  22.         {
  23.             overload obj = new overload();   
  24.             int a = 2;
  25.             double b = 3.2;
  26.             obj.add(a, a);
  27.             obj.add(b, b);
  28.             System.out.println(obj.x + " " + obj.y);     
  29.         }
  30.    }

a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4

Answer: d
Clarification: For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
output:

$ javac Overload_methods.java
$ java Overload_methods 
4 6.4

9. What will be the output of the following Java code?

  1.     class test 
  2.     {
  3.         int a;
  4.         int b;
  5.         void meth(int i , int j) 
  6.         {
  7.             i *= 2;
  8.             j /= 2;
  9.         }          
  10.     }    
  11.     class Output 
  12.     {
  13.         public static void main(String args[])
  14.         {
  15.             test obj = new test();
  16. 	    int a = 10;
  17.             int b = 20;             
  18.             obj.meth(a , b);
  19.             System.out.println(a + " " + b);        
  20.         } 
  21.     }

a) 10 20
b) 20 10
c) 20 40
d) 40 20

Answer: a
Clarification: Variables a & b are passed by value, copy of their values are made on formal parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively.
output:

$ javac Output.java
$ java Output
10 20

10. What will be the output of the following Java code?

a) 10 20
b) 20 10
c) 20 40
d) 40 20

Answer: b
Clarification: Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 10 respectively.
output:

What is the process of defining a method in terms of itself that is a method that calls itself called?

Recursion is the process of defining something in terms of itself. Recursion is sometimes called circular definition. A function that calls itself is said to be recursive. Example.

What is the process of defining a method in terms?

The process of defining a method in terms of itself is called RECURSION. AND THE FUNCTIONS ARE CALLED AS 'RECURSIVE FUNCTIONS'.

What is the process of defining a method in a subclass having same name and type signature?

Explanation: When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.

What is the process of defining two or more methods within same class that have same name but different parameters declaration?

The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.