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


Java Recursion

Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.

Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.


Recursion Example

Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers:

Example

Use recursion to add all of the numbers up to 10.

public class Main { public static void main(String[] args) { int result = sum(10); System.out.println(result); } public static int sum(int k) { if (k > 0) { return k + sum(k - 1); } else { return 0; } } }

Try it Yourself »

Example Explained

When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and returns the result. When k becomes 0, the function just returns 0. When running, the program follows these steps:

10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0

Since the function does not call itself when k is 0, the program stops there and returns the result.



Halting Condition

Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself. Every recursive function should have a halting condition, which is the condition where the function stops calling itself. In the previous example, the halting condition is when the parameter k becomes 0.

It is helpful to see a variety of different examples to better understand the concept. In this example, the function adds a range of numbers between a start and an end. The halting condition for this recursive function is when end is not greater than start:

Example

Use recursion to add all of the numbers between 5 to 10.

public class Main { public static void main(String[] args) { int result = sum(5, 10); System.out.println(result); } public static int sum(int start, int end) { if (end > start) { return end + sum(start, end - 1); } else { return end; } } }

Try it Yourself »

The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming.



R

Ram Sharma · Follow
Coach · 123.60K points

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

Category: JAVA

Be the first to upvote this question

0 Upvote   Share

Share This:  

Share this question in group:

Login required to perform this action.


Create an account | Already have account? Login

Close

Showing 1 - 6 of 6 questions


1

Which of these will happen if recursive method does not have a base case?

a

infinite loop condition occurrence

c

After 10000 executions program will be automatically stopped.

correct answer a

If a recursive method does not have a base case which is necessary to meet the end of condition then an infinite loop occurs which results in stackoverflow exception error.

2

What is Recursion in CSharp defined as?

a

Recursion is another form of class

b

Recursion is another process of defining a method that calls other methods repeatedly

c

Recursion is a process of defining a method that calls itself repeatedly

d

Recursion is a process of defining a method that calls other methods which in turn calls this method

correct answer c

Recursion is the process of defining something in terms of itself. It allows us to define method that calls itself repeatedly until it meets some base case condition.

3

Which of these is not a correct statement?

a

A recursive method must have a base case

b

Recursion always uses stack

c

Recursion is always managed by C# Runtime environment

d

Recursive methods are faster that programmer written loop to call the function repeatedly using a stack

correct answer c

No matter whatever is the programming language recursion is always managed by operating system.

4

Which of these data types is used by operating system to manage the Recursion in Csharp?

correct answer d

Answer: Option (D)

5

What will be the correct output the for the given code snippet?

class maths

{

     int fact(int n)

     {

          int result;

          if(n ==1)

          return1;

          result = fact(n -1)* n;

          return result;

     }

}

class Output

{

     staticvoid main(String args[])

     {

          maths obj =new maths();

          Console.WriteLine(obj.fact(1));

     }

}

correct answer c

fact() calculates recursively the factorial of a number when n turns to be 1, base case is executed consecutively and hence 1 is returned.
Output: 1

6

What will be the correct output for the given code snippet?

class maths

{

     int fact(int n)

     {

          int result;

          if(n ==1)

          return1;

          result = fact(n -1)* n;

          return result;

     }

}

class Output

{

      staticvoid main(String args[])

     {

          maths obj =new maths();

          Console.WriteLine(obj.fact(4)*(3));

     }

}

correct answer c

4! = 4 * 3 *2 * 1 = 24 * 3 = 72.Not factorial of 3 but just multiply the number with 3.
Output : 72

What is the process of defining a method in terms of itself that is a method that calls itself select one a encapsulation B polymorphism C Recursion D abstraction?

Recursion is the process of defining something in terms of itself. It allows us to define method that calls itself repeatedly until it meets some base case condition.

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

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 terms of itself in Java?

The explanation is: Recursion is the process of defining something in terms of itself.

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

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.