NEW
Font size
WorksheetsRecursion in C/Java/Python
Total questions: 50
Worksheet time: 2hrs 36mins
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?
2 0 4 0 8
2 0 4 8 0
2 0 0 4 8
2 0 8 0 4
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?
5
6
7
8
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?
321231444
322113444
312213444
321213444
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;
}
10
1
10 9 8 7 6 5 4 3 2 1 0
10 9 8 7 6 5 4 3 2 1
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;
}
2
24
12
10
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;
}
4
16
8
Error
A recursive function is
A function that calls other functions in a recursive way.
Any function that calls itself is called recursive
A function that has a base case or termination condition
None of the above
Recursion is:
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.
is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls other function in a step.
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.
None of the above
Which of the following is the best definition of a recursive method?
A method that iterates itself exactly 5 times.
A method that invokes itself by name within the method.
A method that will never iterate infinitely.
A method that cannot be called more than once.
What is the definition of recursion?
Recursion is a programming technique you can use to allow a method to have numerous fields in its argument.
Recursion is programming technique you can use in which a method calls itself to solve a problem.
Recursion is a programming language model organized around objects rather than "actions" and data rather than logic
Recursion is a program that translates a source program written in some high-level programming language into machine code.
Name the condition at which the recursive method will stop calling itself.
Base case
Worst Case
Best Case
None of the above
The program may run out of memory in a
non-recursive function call
recursive function call
condition when too many variables are declared
none of the above
Which of the following condition is true?
Recursion is always better than iteration.
Recursion uses more memory as compared to iteration.
Recursion uses less memory as compared to iteration.
Iteration is always better and simpler than recursion.
Which of the following problems can be solved using recursion?
finding Nth number of the Fibonacci sequence
finding the factorial of a number
finding the length of a string
all of the above
Recursion is similar to which of the following?
if-else
switch-case
loops
none of the above
What would happen if the base case if not defined in the recursive method?
Stack Overflow
Stack Underflow
Program Crashes
None of the above
The number of recursive calls is limited to the ____ of the stack.
time
ability
quality
size
Which of the following sorting algorithms use recursion?
Selection Sort
Insertion Sort
Mergesort
What is the returned value of recmethod(5)?
68
70
75
82
What is printed as a result of the call stri("COMPSCI")?
COMPSCI
COMPSC
COMPS
COMP
COM
CO
C
COMPSCI
OMPSCI
MPSCI
PSCI
SCI
CI
I
CO
COM
COMP
COMPS
COMPSC
COMPSCI
C
CO
COM
COMP
COMPS
COMPSC
COMPSCI
Will rec(5) iterate finite times?
Yes
No
What value is returned as a result of the call mystry(x)?
x * (x+1)
2x
2(x-1)
x3 +1
Name the condition at which the recursive method will stop calling itself.
Base case
Worst Case
Best Case
None of the above
The program may run out of memory in a
non-recursive function call
recursive function call
condition when too many variables are declared
none of the above
Which of the following condition is true?
Recursion is always better than iteration.
Recursion uses more memory as compared to iteration.
Recursion uses less memory as compared to iteration.
Iteration is always better and simpler than recursion.
Which data structure is required to perform recursion?
Queue
Stack
LinkedList
Graph
A recursive method is a method in which the solution of a problem depends upon
smaller instance of the same problem
larger instances of the same problem
smaller instance of different problem
larger instances of different problem
Which of the following problems can be solved using recursion?
finding Nth number of the Fibonacci sequence
finding the factorial of a number
finding the length of a string
all of the above
Recursion is similar to which of the following?
if-else
switch-case
loops
none of the above
What would happen if the base case if not defined in the recursive method?
Stack Overflow
Stack Underflow
Program Crashes
None of the above
The number of recursive calls is limited to the ____ of the stack.
time
ability
quality
size
Recursion is memory intensive because
recursive methods declare a lot of variables
previous stack frames are still active when a new call is made
many copies of the code are created
none of the above
What will be the output of the program
90
92
81
91
What is the output of this program?
0
1
120
None of the above
What is the output of this program?
24
30
120
720
What is the action of method mystery2?
a+b
a*b
ab
ba
a!
COMPSC
COMPS
COMP
COM
CO
C
OMPSCI
MPSCI
PSCI
SCI
CI
I
COM
COMP
COMPS
COMPSC
COMPSCI
CO
COM
COMP
COMPS
COMPSC
COMPSCI
What is the action of method mystery3?
a+b
a*b
ab
ba
a!
Why would you use a base case?
So that the code loops forever
So that the code does not loop forever
Because coding
Name the condition at which the recursive method will stop calling itself.
Base case
Worst Case
Best Case
None of the above
The program may run out of memory in a
non-recursive function call
recursive function call
condition when too many variables are declared
none of the above
Which of the following condition is true?
Recursion is always better than iteration.
Recursion uses more memory as compared to iteration.
Recursion uses less memory as compared to iteration.
Iteration is always better and simpler than recursion.
Which of the following problems can be solved using recursion?
finding Nth number of the Fibonacci sequence
finding the factorial of a number
finding the length of a string
all of the above
What would happen if the base case if not defined in the recursive method?
Stack Overflow
Stack Underflow
Program Crashes
None of the above
What is the output of this program?
1
30
120
Runtime error
