0% found this document useful (0 votes)
30 views20 pages

Recursive Mcq1

Uploaded by

VISHAGAN M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views20 pages

Recursive Mcq1

Uploaded by

VISHAGAN M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Recursive Functions

Find the output of the program.

#include <stdio.h>
int fun(int x, int y)
{
if (x == 0)
return y;
else
return fun(x - 1, x + y);
}
int main()
{
int a = fun(5, 2);
printf("%d", a);
return 0;
}

A. 10
B. 15
C. 14
D. 17

ANSWER: D
How many times the function factorial will be executed?

#include <stdio.h>
int factorial(int);
int main()
{
int n = 6, f;
f = factorial(n);
printf("%d! = %ld\n", n, f);
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}

A. 1
B. 6
C. 7
D. infinite

ANSWER: C

This is an example of recursion. The function ‘factorial’ will be called 7 times to compute the
factorial of 6.

2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output?

#include <stdio.h>
int f(int n, int k)
{
if (n == 0)
return 0;
else if (n % 2)
return f(n / 2, 2 * k) + k;
else
return f(n / 2, 2 * k) - k;
}
int main()
{
printf("%d", f(20, 1));
return 0;
}

A. 5
B. 8
C. 9
D. 20

ANSWER: C

f(20,1) = 9.
f(10,2) - 1 = 9
f(5,4) - 2 = 10
f(2,8) + 4 = 12
f(1,16) - 8 = 8
f(0,32) + 16 = 16

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 3


What will be the output of the following C program?

#include <stdio.h>
int fun(int x)
{
int fun1;
if (x > 100)
fun1 = x - 10;
else
fun1 = fun(fun(x + 11));
return fun1;
}
int main()
{
int y;
y = fun(95);
printf("%d", y);
return 0;
}

A. 95
B. 91
C. 90
D. 106

ANSWER: B

f(95)–>f(f(106)) = f(96)–>f(f(107)) = f(97)–>f(f(108)) = f(98)–>f(f(109)) = f(99)–>f(f(110)) =


f(100)–>f(f(111))=f(101)=91
So the correct option is (c)

4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


How many times Hello world will be printed?

#include <stdio.h>
int main()
{
printf("Hello world\n");
main();
return 0;
}

A. Infinite times
B. 32767
C. 65535
D. Till stack overflows

ANSWER: D

A call stack or function stack is used for several related purposes, but the main reason for having
one is to keep track of the point to which each active subroutine should return control when it
finishes executing. A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After
stack memory is full. It shows stack overflow error.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 5


What will be the output of the following C code?

#include <stdio.h>
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. 8
C. 16
D. Error

ANSWER: C

6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Consider the following code snippet:

void function()
{
function();
}
int main()
{
function();
return 0;
}

What will happen when the above snippet is executed?

A. The code will be executed successfully and no output will be generated


B. The code will be executed successfully and random output will be generated
C. The code will show a compile time error
D. The code will run for some time and stop when the stack overflows

ANSWER: D

Every function call is stored in the stack memory. In this case, there is no terminating condition
(base case). So, function() will be called continuously till the stack overflows and there is no
more space to store the function calls. At this point of time, the program will stop abruptly.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 7


Consider the same recursive C function that takes two arguments

unsigned int func(unsigned int n, unsigned int r)


{
if (n > 0)
return (n % r + func(n / r, r));
else
return 0;
}

What is the return value of the function foo when it is called as func(240, 7)?

A. 10
B. 12
C. 14
D. 16

ANSWER: B

func(240, 7) will return 2 + func(34, 7). All subsequent recursive calls (including func(34, 7))
will return 6 + func(4, 7) except the last call func(4, 7) . The last call func(4, 7) returns 4. So, the
value returned by func(240, 7) is 2 + 6 + 4=12.

8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What does the following function do?

int fun(unsigned int n)


{
if (n == 0 || n == 1)
return n;
if (n % 5 != 0)
return 0;
return fun(n / 5);
}

A. It returns 1 when n is a multiple of 5, otherwise returns 0


B. It returns 1 when n is a power of 5, otherwise returns 0
C. It returns 0 when n is a multiple of 5, otherwise returns 1
D. It returns 0 when n is a power of 5, otherwise returns 1

ANSWER: B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 9


What is the output of the following C program?

#include<stdio.h>
int func(int n, int sum)
{
int k = 0, j = 0;
if (n == 0)
return;
k = n % 10;
j = n / 10;
sum = sum + k;
func(j, sum);
printf("%d,", k);
}
int main() {
int a = 2048, sum = 0;
func(a, sum);
printf("%d ", sum);
return 0;
}

A. 8 ,4, 0, 2, 14
B. 8, 4, 0, 2, 0
C. 2, 0, 4, 8, 14
D. 2, 0, 4, 8, 0

ANSWER: D

sum has no use in func(), it is there just to confuse. Function func() just prints all digits of a
number. In main, there is one more printf statement after func(), so one more 0 is printed after
all digits of n.

10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


The following function is an example of recursion in C. If the argument passed to the function
(i.e. n) is 5, find the output.

int fun2(int n)
{
if (n == 0)
return 0;
fun2(n / 2);
printf("%d", n % 2);
return 0;
}

A. 2.5
B. 101
C. 2.0000
D. 1

ANSWER: B

The function finds the binary equivalent of the number n. Thus, the output is 101.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 11


What will be the output of the following code segment?

void function(int n)
{
if (n == 0)
return;
printf("%d ", n);
function(n - 1);
}
int main()
{
function(10);
return 0;
}

A. 10
B. 1
C. 10 9 8 . . . 1
D. 10 9 8….0

ANSWER: C

It will print from 10 to 1.

12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Consider the same recursive C function that takes two arguments:

unsigned int func(unsigned int n, unsigned int r)


{
if (n > 0)
return (n % r + func(n / r, r));
else
return 0;
}

What is the return value of the function func when it is called as func(513, 2)?

A. 0
B. 2
C. 4
D. 6
E. 8
ANSWER: B

func(513, 2) will return 1 + func(256, 2). All subsequent recursive calls (including func(256, 2))
will return 0 + func(n/2, 2) except the last call func(1, 2) . The last call func(1, 2) returns 1. So,
the value returned by func(513, 2) is 1 + 0 + 0…. + 0 + 1=2.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 13


How many stars will be printed if the following program is called from the main function with
argument n=5?

void func(int n)
{
int i = 0;
if (n > 1)
func(n - 1);
for (i = 0; i < n; i++)
printf(" * ");
}

A. 5
B. 10
C. 15
D. 20
E. 25
ANSWER: C

Solution: The func is called 5 times and each times ‘*’ is printed n number of times. Thus,
n(n+1)/2 i.e. 15 times ‘*’ will be printed.

14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include<stdio.h>
int main()
{
main();
return 0;
}
A. Runtime error
B. Compilation error
C. 0
D. None of the mentioned
ANSWER: A

Explanation:
Yes it’s a runtime error, here the main() function repeatedly called the main() and the program
never ends.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 15


What will be the output of the C program?
#include <stdio.h>
void fun(int);
int main()
{
int a = 3;
fun(a);
return 0;
}
void fun(int n)
{
if (n > 0)
{
fun(--n);
printf("%d ", n);
}
}
A. 1 2 3
B. No Output
C. 0 1 2
D. 0
ANSWER: C

Explanation:
Recursive function will always work as a stack FILO (Fist In Last Out).
How it works
Here fun(--n) is a calling function inside fun(int n)
irrespective of recursive call, the value will be stored in the memory location as shown below
i.e) statement below fun( --n ) will execute for every time when the function is called,
irrespective of recursion above it.
while displaying output, it simply reverse its order as it stores the value in stack.

16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the C program?
#include <stdio.h>
int main()
{
int i;
i = main();
printf("%d", i);
int main()
{
int a;
a = 5 * 5;
return a;
}
return 0;
}
A. Compilation error
B. Runtime error
C. address
D. 25
ANSWER: B

Explanation:
main(); function is called repeatedly and the program never ends until your system memory
crash.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 17


How many times the program will print "IndiaREC"?
#include<stdio.h>
int main()
{
printf("IndiaREC");
main();
return 0;
}
A. Infinite times
B. 32767 times
C. 65535 times
D. Till stack overflows
ANSWER: D

Explanation:

A call stack or function stack is used for several related purposes, but the main reason for having
one is to keep track of the point to which each active subroutine should return control when it
finishes executing.

A stack overflow occurs when too much memory is used on the call stack.

Here function main() is called repeatedly and its return address is stored in the stack. After
stack memory is full. It shows stack overflow error.

18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


In C all functions except main() can be called recursively.
A. True
B. False
ANSWER: B

Explanation:

Any function including main() can be called recursively.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 19


Usually recursion works slower than loops.
A. Yes
B. No
ANSWER: A

Explanation:

When a recursive call is made, the function/process clones itself and then process that function.
This leads to time and space constrains.

In a loop, there is no recursive call involved that saves a lot of time and space too.

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy