0% found this document useful (0 votes)
19 views10 pages

Recursion Op 12th

The document contains a series of programming problems involving recursive functions in Java. Each problem asks for the output of a specific function given certain inputs, along with a dry run or working explanation. The problems span multiple years and cover various concepts in recursion and function behavior.

Uploaded by

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

Recursion Op 12th

The document contains a series of programming problems involving recursive functions in Java. Each problem asks for the output of a specific function given certain inputs, along with a dry run or working explanation. The problems span multiple years and cover various concepts in recursion and function behavior.

Uploaded by

Ashish Rajput
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2020

The following function check () is a part of some class. What will the function check () return
when the value of?
(i) N=25
(ii) N=10

Show the dry run/working.

static int check (int n)


{
if(n<=1)
return 1;
if(n%2==0)
return 1 + check(n/2);
else
return 1 + check(n/2+1);
}
public static void main (String args[])
{
[Link](check(25));
[Link](check(10));
}

2019

The following function Mystery () is a part of some class. What will the function Mystery ()
return when the value of num=43629, x=3 and y=4 respectively? Show the dry run/working.

static int Mystery (int num, int x, int y)


{
if(num<10)
return num;
else
{
int z = num % 10;
if(z%2 == 0)
return z*x + Mystery (num/10, x, y);
else
return z*y + Mystery(num/10, x, y);
}
}
public static void main(String args[])
{
[Link](Mystery(43629,3,4));
}
2017

The following function magicfun () is a part of some class. What will the function magicfun ()
return, when the value of n=7 and n=10, respectively? Show the dry run/working: [5]

static int magicfun (int n)


{
if(n == 0)
return 0;
else
return magicfun(n/2) * 10 + (n%2);
}
public static void main(String args[])
{
[Link](magicfun());
}
2016

The following function Check () is a part of some class. What will the function Check () return
when the values of both ‘m’ and ‘n’ is equal to 5? Show the dry run/working. [5]

static int Check (int m, int n)


{
if(n == 1)
return -m--;
else
return ++m+Check (m, --n);
}
public static void main(String args[])
{
[Link](Check(5,5));
}
2015
The following function is a part of some class. Assume ‘x’ and ‘y’ are positive
integers, greater than 0. Answer the given questions along with dry run / working.

static void someFun (int x, int y)


{
if(x>1)
{
if(x%y==0)
{
[Link](y+" ");
someFun(x/y, y);
}
else
someFun(x, y+1);
}
}
public static void main(String args[])
{
someFun(24,2);
}
(i) What will be returned by someFun (24,2)?
(ii) What will be returned by someFun (84,2)?
(iii) State in one line what does the function someFun () do, apart from recursion?
2014
The following functions are part of some class
i) What will be the output of fun1() when the value of s[] ={‘J’,’U’,’N’,’E’} AND X= 1?
ii) What will be the output of fun2() when the value of n =” SCROLL”?
iii) State in one line what does the function fun1() do apart from recursion

static void fun1(char s[], int x)


{
[Link](s);
char temp;
if(x<[Link]/2)
{
temp=s[x];
s[x] = s[[Link]-x-1];
s[[Link]-x-1]=temp;
fun1(s, x+1);
}
}
static void fun2(String n)
{
char c[]=new char[[Link]()];
for(int i=0;i<[Link]; i++)
c[i]=[Link](i);
fun1(c,0);
}
public static void main(String args[])
{
char c[]={'J','U','N','E'};
fun1(c,1);
fun2("SCROLL");
}
2013
The following function Recur is a part of some class. What will be the output of the function
Recur () when the value of n is equal to 10. Show the dry run / working. [5]

static void Recur (int n)


{
if (n>1)
{
[Link] (n + " " );
if(n%2 !=0)
{
n = 3* n + 1;
[Link](n + " ");
}
Recur (n/2);
}
}
public static void main(String args[])
{
Recur(10);
}
2012
The following function witty() is a part of some class. What will be the output of the function
witty( ) when the value of n is ‘SCIENCE’ and the value of p is 5. Show the dry run/working:

static void witty ( String n, int p)


{
if (p<0)
[Link](" ");
else
{
[Link]( [Link](p) + ".");
witty(n,p-1);
[Link]([Link](p));
}
}
public static void main(String args[])
{
witty("SCIENCE",5);
}
2011
The following is a part of some class. What will be the output of the function mymethod()
when the value of the counter is equal to 3? Show the dry run/working. [5]
static void mymethod (int counter)
{
if (counter == 0)
[Link]. println (" ");
else
{
[Link] ("Hello" +counter);
mymethod (--counter);
[Link] (" " +counter);
}
}
public static void main(String args[])
{
mymethod(3);
}

2010
The following functions numbers (int) and numbers1 (int) are a part of some class. Answer
the questions given below showing the dry run/working:
static void numbers (int n)
{
if (n > 0)
{
[Link]. print(n + " " );
numbers (n-2);
[Link](n + " ");
}
}
static String numbers1 (int n)
{
if (n <= 0)
return " ";
return numbers1(n-1) + n + " ";
}
public static void main(String args[])
{
numbers(5);
[Link]();
[Link](numbers1(6));
}
(i) What will be the output of the function numbers (int n) when n = 5?
(ii) What will the function numbers1 (int n) return when n = 6?
2009
The following function trial () & perform () are a part of some class. Answer the following
parts given below. Show the dry run/working.

static int trial(int n)


{
if(n==1)
return 2;
else if (n==2)
return 3;
else
return trial(n-2)+trial(n-1);
}
static void perform(int p)
{
int x;
for(int i=1;i<=p;i++)
{
x=trial(i);
[Link](x+" ");
}
}
public static void main(String args[])
{
[Link](trial(4));
perform(5);
}

(i) what will the function trial () return when the value of n is 4?
(ii) what will the function perform () return when the value of p is 5?
(iii) In one-line state what the function trial () is doing, apart from recursion?
2008
The following function show() & calling are a part of some class. Assume that the parameter
n is greater than 1 when the function is invoked. It returns the value 1 when true otherwise
it returns 0.
Show the dry run/working.

static void calling(int i)


{
int f=2;
int n=0;
show(n,f);
}
static int show(int n, int f)
{
if(n==f)
return 1;
if(n%f==0||n==1)
return 0;
else
return show(n,f+1);
}
public static void main(String args[])
{
[Link](show(27,2));
}

(i) what will the function show() return when the value of n is 11?
(ii) what will the function show() return when the value of n is 27?
(iii) in one line state what the function show() is doing, apart from recursion?
2007
In the following, function strange is a part of some class. Assume that argument x & y are
greater than 0 when the function is invoked. Show the dry run/working.

static int stranger(int x, int y)


{
// assuming x>=0 & y>0
if(x>=y)
{
x=x-y;
return stranger(x, y);
}
else
{
return x;
}
}
public static void main(String args[])
{
[Link](stranger(20,5));
[Link](stranger(15,6));
}

(i) What will function strange (20,5) return?


(ii) What will function strange (15,6) return?
(iii) In one-line state, what the function strange(..) is doing?
The following function find( ) and perform( ) are a part of some class. Show the dry run/working.
static int find(int n, int p)
{
if(n == 0)
return p;
else
return find(p%n,n);
}
static void perform(int m)
{
int q = 14;;
int x = find(q++, ++m);
[Link](x);
}
public static void main(String args[])
{
[Link](find(12,8));
perform(20);
}

(i) What will the function find(12,8) return?


(ii) What will be the output of the function perform( ) when the value of m is 20?
(iii) In one line state what the function find( ) is doing, apart from recursion.

Common questions

Powered by AI

fun1() will output "JUEN". It reverses parts of the sequence around the middle, swapping elements at increasing indices until it reaches the midpoint. It visually demonstrates a simple string reversal logic by swapping characters in place.

stranger(20,5) returns 0, and stranger(15,6) returns 3. The function mimics the modulo operation recursively, subtracting y from x until x is less than y, then returning x. It effectively computes x modulo y using recursion.

Recur(10) outputs "10 16 5 8 4 2 ". It generates a specific sequence: starting at 10, if odd, it multiplies by 3 and adds 1; recursively calls with half the number until n drops below 2. It's a demonstration of the Collatz Conjecture.

When \( m \) and \( n \) are both 5, the function Check() returns 19. It works through a series of increments and recursive calls: \( Check(5,5) = 6 + Check(6,4) = 7 + Check(7,3) = 8 + Check(8,2) = 9 + Check(9,1) = -9 \). The recursion ends when \( n == 1 \) with the base case \( -m-- \). The operations stack results, accumulating to 19.

For \( n = 7 \), the function magicfun() returns 111. It computes the binary representation of the integer by recursively dividing by 2, accumulating the binary digits. For \( n = 10 \), magicfun() returns 1010, again reflecting the binary form of 10 through recursive divisions. The computed results simulate the conversion of the number to its binary form.

show(11) returns 1, indicating 11 is prime. The function checks divisibility by each incrementing factor, returning 0 if a factor other than 1 or itself divides it evenly. It performs a recursive prime validation.

witty("SCIENCE",5) displays "C.S.C.I.E.", then prints "ECNICS". It utilizes recursion to print characters in reverse order, prefixed with periods, and then displays them normally post recursion, showing reverse order mechanics and character printing.

The function Mystery() will return 66 for num=43629, x=3, y=4. It iterates through each digit of the number, checking if it's even or odd and multiplying it by x or y, respectively, while recursively processing the rest of the number. For num=43629: \( 9 \times 4 + 2 \times 3 + 6 \times 3 + 3 \times 4 + 4 \times 3 = 36 + 6 + 18 + 12 + 12 = 66 \).

For \( N = 25 \), the function will return 6. It works by checking if the number is even or odd. If even, it divides by 2; if odd, it divides by 2, adds 1, and performs another check. \( check(25) \) proceeds as: 25 is odd, thus calling \( check(13) \), further leading to \( check(7) \), \( check(4) \), \( check(2) \), and finally \( check(1) \), reaching the base case with each step incrementing the return value by 1. For \( N = 10 \), the function returns 5 following similar logic: \( check(10) \) calls \( check(5) \), followed by \( check(3) \), \( check(2) \), and finally \( check(1) \), each recursion step incrementing the final return value.

someFun(24,2) prints the sequence "2 2 2 3 ". It divides 24 by 2 until no longer divisible, producing the prime factors incrementally. For someFun(84,2), it prints "2 2 3 7 ", showing the complete factorization of 84. someFun() implements a recursive method to find and print the prime factors of a number.

You might also like