CISCE ISC (Science) ISC Class 12 Important Questions for Computer Science (Theory) - Recursion in Java
Advertisements
Advertisements
Chapter wise important questions with answers and solutions for Computer Science (Theory) in CISCE ISC (Science) ISC Class 12.
Give one reason, why iteration is better than recursion.
अध्याय: [11] Recursion in Java
Differentiate between direct recursion and indirect recursion.
अध्याय: [11] Recursion in Java
A class Fibo has been defined to generate the Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, ……. (Fibonacci series are those in which the sum of the previous two terms is equal to the next term).
Some of the members of the class are given below:
| Class name | Fibo |
| Data member/instance variable: | |
| start | integer to store the start value |
| end | integer to store the end value |
| Member functions/methods: | |
| Fibo( ) | default constructor |
| void read( ) | to accept the numbers |
| int fibo(int n) | return the nth term of a Fibonacci series using recursive technique |
| void display( ) | displays the Fibonacci series from start to end by invoking the function fibo() |
Specify the class Fibo, giving details of the Constructor, void read( ), int fibo(int), and void display( ). Define the main() function to create an object and call the functions accordingly to enable the task.
अध्याय: [11] Recursion in Java
A class Gcd has been defined to find the Greatest Common Divisor of two integer numbers. Some of the members of the class are given below:
| Class name | Gcd |
| Data member/instance variable: | |
| num1 | integer to store the first number |
| num2 | integer to store the second number |
| Member functions/methods: | |
| Gcd( ) | default constructor |
| void accept( ) | to accept the numbers |
| int gcd(int x,int y) | return the GCD of the two numbers x and y using recursive technique |
| void display( ) | displays the result with an appropriate message |
Specify the class Gcd, giving details of the Constructor, void accept( ), int gcd(int,int), and void display( ). Define the main() function to create an object and call the functions accordingly to enable the task.
अध्याय: [11] Recursion in Java
The following function task() is a part of some class. Assume ‘m’ and ‘n’ are positive integers greater than 0. Answer the questions given below along with dry / run working.
int task(int m, int n)
{ if(m=n)
return m;
else if (m>n)
return task(m-n, n);
else
return task(m, n-m)
}
- What will the function task() return when the value of m=30 and n=45?
- What function does task() perform, apart from recursion?
अध्याय: [11] Recursion in Java
Assertion: Recursive data structure follows the LIFO principle.
Reason: Execution of recursive code follows the concepts of data structure Queue.
अध्याय: [11] Recursion in Java
The following function is a part of some class:
int jolly(int[] x, int n, int m)
{
if (n<0)
return m;
else if(n<x.length)
m=(x[n]>m)?x[n]:m;
return jolly(x, --n, m);
}
- What will be the output of jolly() when the value of x[ ]={6,3,4,7,1}, n=4 and m=0?
- What function does jolly() perform, apart from recursion?
अध्याय: [11] Recursion in Java
Design a class DeciHex to accept a positive integer in decimal number system from the user and display its hexadecimal equivalent.
Example 1:
Decimal number= 25
Hexadecimal equivalent= 19
Example 2:
Decimal number =28
Hexadecimal equivalent = 1C
Some of the members of the class are given below.
| Class name | DeciHex |
| Data members/instance variables: | |
| num | stores the positive integer |
| hexa | string to store the hexadecimal equivalent of num |
| Methods / Member functions: | |
| DeciHex() | constructor to initialise the data members with legal initial values |
| void getNum() | to accept a positive integer |
| void convert(int n) | to find the hexadecimal equivalent of the formal parameter 'n' using the recursive technique |
| void display() | to display the decimal number and its hexadecimal equivalent by invoking the function convert() |
Specify the class DeciHex giving details of the constructor( ), void getNum( ), void convert(int) and void display(). Define a main() function to create an object and call all the functions accordingly to enable the task.
अध्याय: [11] Recursion in Java
Assertion: Recursion utilises more memory as compared to iteration.
Reason: Time complexity of recursion is higher due to the overhead of maintaining the function call stack.
अध्याय: [11] Recursion in Java
The following is a function of class Armstrong. This recursive function calculates and returns the sum of the cubes of all the digits of num, where num is an integer data member of the class Armstrong.
[A number is said to be Armstrong if the sum of the cubes of all its digits is equal to the original number].
There are some places in the code marked by ?1?, ?2?,?3? which may be replaced by a statement/expression so, that the function works properly.
public int sumOfPowers(int num)
{
if(num == 0)
return ?1?
int digit = ?2?;
return(int)Math.pow(digit, 3) + ?3?;
}
- What is the expression or statement at ?1?
- What is the expression or statement at ?2?
- What is the expression or statement at ?3?
अध्याय: [11] Recursion in Java
A class LCM has been defined to find the Lowest Common Multiple of two integers.
Some of the data members and member functions are given below:
| Class name | LCM |
| Data members/instance variables: |
|
| n1 | to store an integer number |
| n2 | to store an integer number |
| large | integer to store the largest from n1,n2 |
| sm | integer to store the smallest from n1,n2 |
| l | to store lcm of two numbers |
| Methods / Member functions: | |
| LCM( ) | default constructor to initialize data members with legal initial values |
| void accept() | to accept n1 and n2 |
| int getLCM() | returns the lcm of n1 and n2 using the recursive technique |
| void display( ) | to print the numbers n1, n2 and lcm |
Specify the class LCM giving details of the constructor( ), void accept( ), int getLCM() and void display( ). Define a main ( ) function to create an object and call the member functions accordingly to enable the task.
