wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Recursion in C/Java/Python

Total questions: 50

Worksheet time: 2hrs 36mins

Name
Class
Date
1.

Consider the following C-program

void foo (int n, int sum)

{

int k=0, j=0;

if (n==0) return;

k = n % 10;

j = n/10;

sum = sum + k;

foo (j, sum);

printf("%d ", k);

}


int main()

{

int a=2048, sum=0;

foo (a, sum);

printf("%d\n", sum);

return 0;

}

What is the output?

a)

2 0 4 0 8

b)

2 0 4 8 0

c)

2 0 0 4 8

d)

2 0 8 0 4

2.

Consider the following C-program

int f(int n)

{

static int i=1;

if(n>5) return n;

n=n+i;

i++;

return f(n);

}


int main()

{

int a;

a=f(1);

printf("%d",a);

return 0;

}

What is the output?

a)

5

b)

6

c)

7

d)

8

3.

Consider the following C-program

void count(int n)

{

static int d = 1;

printf("%d", n);

printf("%d", d);

d++;

if (n > 1) count (n-1);

printf("%d", d);

}


void main()

{

count (3);

}

What is the Output?

a)

321231444

b)

322113444

c)

312213444

d)

321213444

4.

What is the output of the following code?


void my_recursive_function(int n)

{

if(n == 0)

return;

printf("%d ",n);

my_recursive_function(n-1);

}

int main()

{

my_recursive_function(10);

return 0;

}

a)

10

b)

1

c)

10 9 8 7 6 5 4 3 2 1 0

d)

10 9 8 7 6 5 4 3 2 1

5.

What will be the output of the following C code?


main()

{

int n;

n=f1(4);

printf("%d",n);

}

f1(int x)

{

int b;

if(x==1)

return 1;

else

b=x*f1(x-1);

return b;

}

a)

2

b)

24

c)

12

d)

10

6.

Predict output of following program


int fun(int n)

{

if (n == 4)

return n;

else return 2*fun(n+1);

}

int main()

{

printf("%d ", fun(2));

return 0;

}

a)

4

b)

16

c)

8

d)

Error

7.

A recursive function is

a)

A function that calls other functions in a recursive way.

b)

Any function that calls itself is called recursive

c)

A function that has a base case or termination condition

d)

None of the above

8.

Recursion is:

a)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition.

b)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls other function in a step.

c)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having no termination condition.

d)

None of the above

9.

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

a)

A method that iterates itself exactly 5 times.

b)

A method that invokes itself by name within the method.

c)

A method that will never iterate infinitely.

d)

A method that cannot be called more than once.

10.

What is the definition of recursion?

a)

Recursion is a programming technique you can use to allow a method to have numerous fields in its argument.

b)

Recursion is programming technique you can use in which a method calls itself to solve a problem.

c)

Recursion is a programming language model organized around objects rather than "actions" and data rather than logic

d)

Recursion is a program that translates a source program written in some high-level programming language into machine code.

11.

Name the condition at which the recursive method will stop calling itself.

a)

Base case

b)

Worst Case

c)

Best Case

d)

None of the above

12.

The program may run out of memory in a

a)

non-recursive function call

b)

recursive function call

c)

condition when too many variables are declared

d)

none of the above

13.

Which of the following condition is true?

a)

Recursion is always better than iteration.

b)

Recursion uses more memory as compared to iteration.

c)

Recursion uses less memory as compared to iteration.

d)

Iteration is always better and simpler than recursion.

14.

Which of the following problems can be solved using recursion?

a)

finding Nth number of the Fibonacci sequence

b)

finding the factorial of a number

c)

finding the length of a string

d)

all of the above

15.

Recursion is similar to which of the following?

a)

if-else

b)

switch-case

c)

loops

d)

none of the above

16.

What would happen if the base case if not defined in the recursive method?

a)

Stack Overflow

b)

Stack Underflow

c)

Program Crashes

d)

None of the above

17.

The number of recursive calls is limited to the ____ of the stack.

a)

time

b)

ability

c)

quality

d)

size

18.

Which of the following sorting algorithms use recursion?

a)

Selection Sort

b)

Insertion Sort

c)

Mergesort

19.

What is the returned value of recmethod(5)?

a)

68

b)

70

c)

75

d)

82

20.

What is printed as a result of the call stri("COMPSCI")?

a)

COMPSCI

COMPSC

COMPS

COMP

COM

CO

C

b)

COMPSCI

OMPSCI

MPSCI

PSCI

SCI

CI

I

c)

CO

COM

COMP

COMPS

COMPSC

COMPSCI

d)

C

CO

COM

COMP

COMPS

COMPSC

COMPSCI

21.

Will rec(5) iterate finite times?

a)

Yes

b)

No

22.

What value is returned as a result of the call mystry(x)?

a)

x * (x+1)

b)

2x

c)

2(x-1)

d)

x3 +1

23.

Name the condition at which the recursive method will stop calling itself.

a)

Base case

b)

Worst Case

c)

Best Case

d)

None of the above

24.

The program may run out of memory in a

a)

non-recursive function call

b)

recursive function call

c)

condition when too many variables are declared

d)

none of the above

25.

Which of the following condition is true?

a)

Recursion is always better than iteration.

b)

Recursion uses more memory as compared to iteration.

c)

Recursion uses less memory as compared to iteration.

d)

Iteration is always better and simpler than recursion.

26.

Which data structure is required to perform recursion?

a)

Queue

b)

Stack

c)

LinkedList

d)

Graph

27.

A recursive method is a method in which the solution of a problem depends upon

a)

smaller instance of the same problem

b)

larger instances of the same problem

c)

smaller instance of different problem

d)

larger instances of different problem

28.

Which of the following problems can be solved using recursion?

a)

finding Nth number of the Fibonacci sequence

b)

finding the factorial of a number

c)

finding the length of a string

d)

all of the above

29.

Recursion is similar to which of the following?

a)

if-else

b)

switch-case

c)

loops

d)

none of the above

30.

What would happen if the base case if not defined in the recursive method?

a)

Stack Overflow

b)

Stack Underflow

c)

Program Crashes

d)

None of the above

31.

The number of recursive calls is limited to the ____ of the stack.

a)

time

b)

ability

c)

quality

d)

size

32.

Recursion is memory intensive because

a)

recursive methods declare a lot of variables

b)

previous stack frames are still active when a new call is made

c)

many copies of the code are created

d)

none of the above

33.

What will be the output of the program

a)

90

b)

92

c)

81

d)

91

34.
What is returned as a result of the call mystery(4,6)?
a)
2
b)
3
c)
4
d)
1
35.

What is the output of this program?

a)

0

b)

1

c)

120

d)

None of the above

36.
What is the returned value of recMethod(5)?
a)
68
b)
70
c)
75
d)
82
37.

What is the output of this program?

a)

24

b)

30

c)

120

d)

720

38.

What is the action of method mystery2?

a)

a+b

b)

a*b

c)

ab

d)

ba

e)

a!

39.
What is printed as a result of the call stringMaker("COMPSCI")?
a)
COMPSCI
COMPSC
COMPS
COMP
COM
CO
C
b)
COMPSCI
OMPSCI
MPSCI
PSCI
SCI
CI
I
c)
CO
COM
COMP
COMPS
COMPSC
COMPSCI
d)
C
CO
COM
COMP
COMPS
COMPSC
COMPSCI
40.

What is the action of method mystery3?

a)

a+b

b)

a*b

c)

ab

d)

ba

e)

a!

41.
Which expression represents the result of calling recurs(11,4)? 
a)
x + y
b)
x * y
c)
xy-2
d)
yx+2
42.

Why would you use a base case?

a)

So that the code loops forever

b)

So that the code does not loop forever

c)

Because coding

43.
What value is returned as a result of the call mysterious(x)?
a)
x * (x+1)
b)
2x
c)
2(x-1)
d)
x+1
44.

Name the condition at which the recursive method will stop calling itself.

a)

Base case

b)

Worst Case

c)

Best Case

d)

None of the above

45.

The program may run out of memory in a

a)

non-recursive function call

b)

recursive function call

c)

condition when too many variables are declared

d)

none of the above

46.

Which of the following condition is true?

a)

Recursion is always better than iteration.

b)

Recursion uses more memory as compared to iteration.

c)

Recursion uses less memory as compared to iteration.

d)

Iteration is always better and simpler than recursion.

47.

Which of the following problems can be solved using recursion?

a)

finding Nth number of the Fibonacci sequence

b)

finding the factorial of a number

c)

finding the length of a string

d)

all of the above

48.

What would happen if the base case if not defined in the recursive method?

a)

Stack Overflow

b)

Stack Underflow

c)

Program Crashes

d)

None of the above

49.
What value is returned as a result of the call mysterious(x)?
a)
x * (x+1)
b)
2x
c)
2(x-1)
d)
x+1
50.

What is the output of this program?

a)

1

b)

30

c)

120

d)

Runtime error