Search Header Logo

Recursion Function

Authored by Nursyahirah Tarmizi

Computers

University

Used 1+ times

Recursion Function
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

30 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is the best definition of a RECURSIVE method?

A method that iterates itself exactly 5 times.

A method that cannot be called more than once.

A method that invokes itself by name within the method.

A method that will never iterate infinitely.

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Every line in a C program should end with a _______

;

:

,

.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Commonly used input function is _____

scanf()

printf()

total()

above all

4.

FILL IN THE BLANK QUESTION

1 min • 2 pts

The process in which a function calls itself directly or indirectly is called _______

5.

FILL IN THE BLANK QUESTION

1 min • 2 pts

The method copies the address of an argument/ parameter into the formal parameter is call by ______

6.

MULTIPLE CHOICE QUESTION

45 sec • 1 pt

Media Image

The code given shows a function namely print_message to print out a message. The code in red box is a function declaration which is also known as function ______

function prototype

function call

function definition

above all

7.

MULTIPLE CHOICE QUESTION

1 min • 2 pts

Media Image

Guess the output?

3

18

6

Error

Answer explanation

The recursion case defined in the nSum() function of the above example is:

int res = n + nSum(n - 1)

The base condition defined for nSum() function is:

if (n = = 0) {

return 0;

}

In the nSum() function, Recursive Case is:

int res = n + nSum(n - 1);

In the program, n = 3, so as nSum(3)'s recursive case, we get:

int res = 3 + nSum(2);

In nSum(2), the recursion case will be the same, but n = 2, so nSum(2) will be:

int res = 2 + nSum(1);

Similarly, for nSum1:

int res = 1 + nSum(0);

As such, for nSum(3), we get:

int res = 3 + 2 + 1 + nSum(0);

As the base condition is n == 0, nSum(0) will return 0.

So the recursion will stop here and the final value returned by the function will be:

int res = 3 + 2 + 1 + 0;

= 0

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?

Discover more resources for Computers