0% found this document useful (0 votes)
53 views8 pages

Abbas

The document contains a series of programming questions focused on recursion, asking for dry runs, outputs, and explanations of various recursive functions in a class context. Each question includes specific functions and scenarios, requiring the reader to analyze the code and provide the expected results and descriptions of the functions' behaviors. The document also includes class specifications for various programming tasks related to recursion.

Uploaded by

rizviabbas752
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)
53 views8 pages

Abbas

The document contains a series of programming questions focused on recursion, asking for dry runs, outputs, and explanations of various recursive functions in a class context. Each question includes specific functions and scenarios, requiring the reader to analyze the code and provide the expected results and descriptions of the functions' behaviors. The document also includes class specifications for various programming tasks related to recursion.

Uploaded by

rizviabbas752
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/ 8

Recursion (Output Questions)

(Ql) 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 ( 1 5 , 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: [5,2008]
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.
(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(int n)
{
if(n= =l)
return 2;
else if(n= =2)
return 3;
else
return trial(n-2) + trial(n-l);
}
void perform(int p)
{

1
int x;
for(int i=l;i<=p;i++){
x=trial(i);
System.out.print(x+" ");
}
}

i) What will the function trial(int) return when the value of n is 4?


ii) What will be the output of the function perform () when the value of p is 5?
iii) In one line state what the function trial(int) is doing, apart from recursion? [5, 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(numbersl(n-l)+n+" ";
}
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?
iii) State in one line what is the function numbers1 (int) doing apart from recursion? [5,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) 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:

2
public void witty(String n, int p)
{
if(p<0)
System.out.println();
else
{
System, out. println(n. charAt(p)+ ".");
witty(n,p-l);
System, out. print(n. charAt(p));
}
} [ 5,2012 ]
(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>l)
{
System.out.print(n+"");
if(n%2 != 0)
{
n=3*n+l;
System.out.print(n+" ");
}
Recur(n/2);

} [5,2013]
}
(Q8) The following functions are part of some class
void fun1 (char s[ ], int x)
{
System.out.println(s);
char temp;
if(x<s.length/2)
{
temp=s[x];
s[x]=s[s. length-x-1];
s [s. length-x-1]=temp;
funl(s,x+l);
}
}
void fun2( String n)
{
char c[]=new char[n.length()];
for(int i=0;i<c.length;i++)
c[i]=n.charAt(i);
funl(c,0);
}
i) What will be the output of funl() when the value of s[]={"SKY"} 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 funl() do apart from recursion. [ 5,2014 ]

3
(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) ?
iii. 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 ‘n’ 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-l);
}

(Qll) 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 + (n % 2);
}
(Q12) 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. [5,2019]
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);
}
}
Recursion (Programs)
(Ql) A class recursion has been defined to find the Fibonacci series up to a limit. Some of the members of
the class are given below:
Class name recursion [10,2005]
4
Data members/ instance variables :
a, b, c, limit : integer type Member functions/methods
Member functions :
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 up to 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 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) & 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/methods :
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(char) 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.
c) Differentiate between finite and infinite recursion. [ 10,2010]

5
(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 octO : 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 = (l)2 + (0)2 + (0)2=l+0 + 0 = l
Hence 28 is a happy number.
Example :
12 = (l)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 function sum sq digits(int)
and displays an appropriate message.
Specify the class Happy giving details of the constructor ), void getnum( int), int sum 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.

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

(Q8) 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
Series Sum(int xx,int nn) : constructor to assign x=xx, n=nn
double findfact(int m) : to return the factorial of m using recursive technique
double power(int a,int b) : to return ab using recursive technique
void calculate() : to calculate the sum of the series by invoking the recursive
functions,
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.
b) State the two differences between iteration and recursion. [10,2014]

(Q9) A class Admission contains the admission numbers of 100 students. Some of the data members /
member functions are given below:
Class name Admission [10,2015]
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 -1.
Specify 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.

(Q10) 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]
1 2 3
Example: 135 = l +3 + 5 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)
7
and void check(). Define the main() function to create an object and call the functions accordingly to enable
the task.

(Qll)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 to
void accept() : 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.

(Q12) 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
Data members/instance variables :
num : to store the number
Methods/Member functions :
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 check() : 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]

(Q13) Design a class ArmNum to check if a given number is an Armstrong number or not. Some of the
members of the class are given below: [10,2019]
[A number is said to be Armstrong if sum of its digits raised to the power of length of the number is equal to
the number
Example : 371= 33 + 73 +13
1634 = 14 +64 34+44
54748 = 55 + 45 + 75 + 45 + 85
Thus 371. 1634 and 54748 are all examples of Armstrong numbers. ]

Class name ArmNum


Data members/instance variables :
n : to store the number
1 : to store the length of the number
Methods/Member functions :
ArmNum (int nn) : parameterized constructor to initialize the data member n=nn
int sum_pow(int i) : returns the sum of each digit raised to the power of the length of the no.
using recursive technique, eg. 34 will return 32 + 42 (as length of the no. is 2)
void isArmstrong() : checks whether the given number is an Armstrong number by invoking the
function sum_pow() and displays the result with an appropriate message
Specify the class ArmNum giving details of the constructor(), int sum_pow(int) and void isArmstrong( ).
Define a main() function to create an object and call the functions accordingly to enable the task.
8

You might also like