Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (2 votes)
3K views

Recursion

The document provides 10 questions related to recursion and recursive functions. The questions cover topics like Fibonacci series, reversing strings, converting numbers to words, changing character case in strings, and more. Sample classes are provided with details of data members and functions to implement the recursive logic. Questions assess understanding of recursion through problems like determining output values, explaining the functions' behaviors, and providing code to define the sample classes.

Uploaded by

Danish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
3K views

Recursion

The document provides 10 questions related to recursion and recursive functions. The questions cover topics like Fibonacci series, reversing strings, converting numbers to words, changing character case in strings, and more. Sample classes are provided with details of data members and functions to implement the recursive logic. Questions assess understanding of recursion through problems like determining output values, explaining the functions' behaviors, and providing code to define the sample classes.

Uploaded by

Danish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Recursion (Output Questions)

(Q1) In the following, function strange is a part of some class. Assume that arguments x and y are greater than
0 when the function is invoked. Show the dry run/working. [5,2007]
int strange( int x, int y)
{
//Assuming x>=0 and y> 0
if( x>=y)
{
x= x-y;
return strange(x, y);
}
else
return x;
}
(i) What will the function strange(20, 6) return?
(ii) What will the function strange(15 , 6 ) return?
(iii) In one line state what the function strange( . . .) is doing.

(Q2) The following function show( ) and 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:
void calling( )
{
int f= 2;
show(n , f);
}
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);
}
(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. [5,2008]

(Q3) The following function trial() and perform() are a part of some class. Answer the following parts given
below. Show the dry run/working.
int trial()
{
if(n==1)
return 2;
else if(n==2)
return 3;
else
return trial(n-2) + trial(n-1);
}
void perform(int p)
{
int x;
for(int i=1;i<=p;i++)
{
x=trial(i);
System.out.print(x+” “);
}
}

i) What will the function trial() return when the value of n is 4? [2]
ii) What will be the output of the function perform () when the value of p is 5? [2]
iii) In one line state what the function trial() is doing, apart from recursion? [ 1,2009]

(Q4) The following function numbers(int) and numbers1(int) are a part of some class. Answer the questions
given below showing the dry run/working:
public void numbers(int n)
{

if(n>0)
{
System.out.print(n+” “);
numbers(n-2);
System.out.print(n+” “);
}
}
public String numbers1(int n)
{
if(n<=0)
return “”;
return(numbers1(n-1)+n+” “;
}
i) What will be the output of the function numbers(int n) when n=5? [2]
ii) What will the function numbers1(int n) return when n=6? [2]
iii) State in one line what is the function numbers1(int) doing apart from recursion? [ 1,2010]

(Q5) The following is a part of some class. What will be the output of the function mymethod( ) when the
value of counter is equal to 3? Show the dry run/ working.
void mymethod(int counter)
{
if(counter= =0)
System.out.println(“ “);
else
{
System.out.println(“Hello “+ counter);
mymethod(- -counter);
System.out.println(“ “+ counter);
}
} [ 5,2011]

(Q6) class Trial1


{
public void witty(String n, int p)
{
if(p<0)
System.out.println("");
else
{
System.out.println(n.charAt(p)+".");
witty(n,p-1);
System.out.print(n.charAt(p));
}
}
} [ 5,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:

(Q7) 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.

public void Recur(int n)


{
if(n>1)
{
System.out.print(n+" ");
if(n%2!=0)
{
n=3*n+1;
System.out.print(n+" ");
}
Recur(n/2);
}
} [5,2013]

(Q8) The following functions are part of some class


void fun1(char s[ ], int x)
{
for(int i=0;i<s.length,i++)
System.out.print(s[i]);
System.out.println();
char temp;
if(x<s.length/2)
{
temp=s[x];
s[x]=s[s.length-x-1];
s[s.length-x-1]=temp;
fun1(s,x+1);
}
}
void fun2(String n)
{
char c[]=new char[n.length()];
for(int i=0;i<c.length;i++)
c[i]=n.charAt(i);
fun1(c,0);
}
i) What will be the output of fun1() when the value of s[]={‘J’,’U’,’N’,’E’} AND X = 1 [ 2]
ii) What will be the output of fun2() when the value of n= “SCROLL’? [2]
iii) State in one line what does the function fun1() do apart from recursion. [ 1,2014 ]

(Q9) 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.
void someFun(int x, int y)
{
if(x>l)
{
if(x%y=0)
{
System.out.print(y+“ ”);
someFun(x/y, y);
}
else
someFun(x, y+1);
}
}
(i) What will be returned by someFun(24,2) ?
(ii) What will be returned by someFun(84,2) ?
State in one line what does the function someFun() do, apart from recursion? [5,2015]

(Q10) The following function Check( ) is a part of some class. What will the function Check() return when the
values of both ‘m’ and V are equal to 5? Show the dry run / working. [5,2016]
int Check (int m, int n)
{
if (n = = 1)
return --m;
else
return + + m + Check (m,n);
}

(Q11) 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,2017]
int magicfun( int n)
{
if( n= = 0)
return 0;
else
return magicfun(n/2). * 10 + (p % 2);
}

Recursion (Programs)
(Q1) A class recursion has been defined to find the Fibonacci series upto a limit. Some of the members of the
class are given below:
Class name : recursion [10,2005]
Data members/ instance variables :
a, b, c, limit : integer type Member functions/methods
recursion( ) : constructor to assign a, b, c with appropriate values.
void input( ) : to accept the limit of the series
int fib(int n) : to return the nth Fibonacci term using recursive technique
void generate_fibseries( ) : to generate fibonacci series upto the given limit

(a) Specify the class recursion, giving the details of the constructor, int fib( ), void generate_fibseries( ). You
may assume other functions are written for you and you need not write the main function.
(b) Why recursive functions result into slower execution of the program?

(Q2) Class Convert has been defined to express digits of an integer in words. [10,2006]
The details of the class are given below:
Class name : Convert
Data members:
n : integer whose digits are to be expressed in words.
Member functions:
Convert( ) : constructor to assign 0 to n.
void inpnum( ) : to accept the value of n.
void extdigit(int) : to extract the digits of n using the Recursive technique.
void num_to_words(int ) : to display the digits of an integer n in words.
Specify the class Convert, giving details of the constructor and the functions, void inpnum( ), void
extdigit(int) and void num_to_words( ). The main function need not be written. [10,2007]

(Q3) A class Revstr defines a recursive function to reverse a string and check whether it is a 10 Palindrome.
The details of the class are given below:
Class name : Revstr
Data members/Instance variables :
Str : stores the string
Revst : stores the reverse of the string
Member functions/methods :
void getStr( ) : to accept the string
void recReverse( int ) : to reverse the string using the Recursive Technique
void check( ) : to display the original string, its reverse and whether the string is a Palindrome or not.
Specify the class Revstr giving the details of the functions void getStr( ), void recReverse(int) and void check(
). The main function need not be written. [10,2008]

(Q4) Design a class Change to perform string related operations. The details of the class are given below:
Class name : Change
Data members
str : stores the word
newstr : stores the changed word
len : stores the length of the word Member functions
Change() : default constructor
void inputword() : to accept a word
char caseconvert(char ch) : converts the case of the character and returns it
void recchange(int): extracts characters using recursive technique and changes its case using
caseconvert() and forms a new word
void display() : displays both the words

b) Specify the class Change, giving details of the constructor(), member functions void inputword(), char
caseconvert(char ch), void recchange(int) and void display(). Define the main function to create an object and
call the functions accordingly to enable the above change in the given word. [8]
c) Differentiate between finite and infinite recursion. [ 2,2010]
(Q5) A class DeciOct has been defined to convert a decimal number into its equivalent octal number. Some of
the members of the class are given below:
Class name : DeciOct
Data members
n : stores the decimal number
oct : stores the equivalent octal number
Member functions:
DeciOct() : constructor to initialize the data members to 0
void getnum(int nn) : assigns nn to n
void deci_oct() : calculates the octal equivalent of n and stores it in oct using the recursive technique
void show() : displays the decimal number n calls the function deci_oct() and displays its octal
equivalent
a) Specify the class DeciOct, giving details of the constructor(), void getnum(int), void deci_oct() and void
show(). Also define a main function to create an object and call the functions accordingly to enable the task.
b) State any two disadvantages of using recursion. [ 10,2011]

(Q6) A happy number is a number in which the eventual sum of the square of the digits of the number is
equal to 1. [10,2012]
Example :
28 = (2)2 + ( 8 )2 = 4 + 64 = 68
68 = (6)2 + ( 8)2 = 36 + 64 = 100
100 = ( 1 )2 + ( 0 )2 + ( 0 )2 = 1 + 0 + 0 = 1
Hence 28 is a happy number.
Example :
12 = (1)2 + (2)2 = 1 + 4 = 5 Hence 12 is not a happy number.
Design a class Happy to check if a given number is a happy number. Some of the members of the class are
given below:
Class Name : Happy
Data Members
n: stores the number
Member functions:
Happy( ) : constructor to assign 0 to n
void getnum(intnn) : to assign the parameter value to the number n = nn
int sum_sq_digits(int x) : returns the sum of the square of the digits of the number x, using the recursive
technique.
void ishappy() : checks if the given number is a happy by calling the functionsum_sq_digits(int) and
displays an appropriate message.
Specify the class Happy giving details of the constructor( ), void getnum( int), intsum_sq_digits(int) and void
ishappy(). Also define a main function to create an object and call the methods to check for a happy number.

(Q6) An emirp number is a number which is prime backwards and forwards. Example: 13 and 31 are both
prime numbers. Thus 13 is an emirp number.
Design a class Emirp to check if a given number is Emirp number or nt. Some of the members of the class are
given below:
Class Name : Emirp
Data Members
n : stores the number
rev : stores the reverse of the number
f : stores the divisor
Member functions
Emirp(int nn) : to assign n=nn, rev=0, and f=2
int isprime(int x) : check if the number is prime using the recursive technique and return 1 if prime
otherwise return 0.
void isEmirp() : reverse the given number and check if both the original number and the reverse
number are prime, by invoking the function isprime(int) and display the result with an appropriate message.

Specify the class Emirp giving details of the constructor(int), int isprime(int) and void isEmirp(). Define the
main function to create an object and call the methods to check for Emirp number. [10,2013]

(Q7) A class SeriesSum is designed to calculate the sum of the following series:-
Sum= x2/1! + x4/3!+x6/5!+….xn/(n-1)!
Some of the members of the class are given below:
Class name : SeriesSum
Data members
x : to store an integer number
n : to store number of terms
sum : double variable to store the sum of the series

Member functions
SeriesSum(intxx,intnn) : constructor to assign x=xx, n=nn
doublefindfact(int m) : to return the factorial of m using recursive technique
void calculate() : to calculate the sum of the series by invoking the recursive functions repeatedly
void display() : to display the sum of the series

a) Specify the class SeriesSum, giving details of the constructor(int,int), double findfact(int), double
findpower(int,int), void calculate() and void display(). Define the main() function to create an object and call
the functions accordingly to enable the task. [8]
b) State the two differences between iteration and recursion. [2,2014]

(Q8) A class Admission contains the admission numbers of 100 students. Some of the data [10] members /
member functions are given below:
Class name Admission
Data member/instance variable:
Adno[ ] : integer array to store admission numbers
Member functions/methods:
Admission() : constructor to initialize the array elements
void fillArray() : to accept the elements of the array in ascending order
int binSearch(int 1, int u, int v) : to search for a particular admission number (v)
using binary search and recursive technique and returns 1 if found otherwise returns -1Specify the class
Admission giving details of the constructor, void fillArray( ) and int binSearch(int, int, int). Define the main()
function to create an object and call the functions accordingly to enable the task. [10,2015]

(Q9) A disarium number is a number in which the sum of the digits to the power of their respective position is
equal to the number itself. [10,2016]
Example: 135 = i'+32 + 53 Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not. Some of the members of the
class are given below:
Class name : Disarium
Data member/instance variable :
int num : stores the number
int size : stores the size of the number
Member functions/methods :
Disarium(int nn) : parameterized constructor to initialize the data members n = nn and size = 0
void countDigit( ) : counts the total number of digits and assigns it to size
int sumOfDigit(int n,int p): (returns the sum of the digits of the number(n) to the power of their respective
positions(p) using recursive technique
void check( ) : checks whether the number is a disarium number and displays the result with an
appropriate message
Specify the class Disarium giving the details of the constructor ), void countDigit( ), int sumofDigits(int, int)
and void check(). Define the main() function to create an object and call the functions accordingly to enable
the task.

(Q10)A class Palin has been defined to check whether a positive number is a Palindrome number or not. The
number ‘N’ is palindrome if the original number and its reverse are same. Some of the members of the class
are given below:
Class name : Palin [10,2017]
Data members/instance variables:
num : integer to store the number
revnum : integer to store the reverse of the number
Methods/Member functions:
Palin( ) : constructor to initialize data members with legal initial values
void accept( ) : to accept the number
int reverse(int y) : reverses the parameterized argument y and stores it in ‘revnum’
using recursive technique
void check( ) : checks whether the number is a Palindrome by invoking the function
reverse( ) and display the result with an appropriate message
Specify the class Palin giving the details of the constructor ), void accept( ), int reverse( int) and void check( ).
Define the main() function to create an object and call the functions accordingly to enable the task.

(Q11) Design a class Perfect to check if a given number is a perfect number or not. [A number is said to be
perfect if sum of the factors of the number excluding itself is equal to the original number]
Example : 6 = 1+ 2 +3 (where 1,2 and 3 are factors of 6, excluding itself)
Some of the members of the class are given below:
class: Perfect
num:to store the number
Perfect(int nn) : parameterized constructor to initialize the data member num=nn
int sum_of_factors(int i) : returns the sum of the factors of the number(num), excluding itself, using
recursive technique
void chcck() : checks whether the given number is perfect by invoking the function sum_of_factors( ) and
displays the result with an appropriate message
Specify the class Perfect giving details of the constructor ), int sum_of_factors(int) and void chcck( ). Define a
main( ) function to create an object and call the functions accordingly to enable the task. [10,2018]

You might also like