Answer the questions on the code given below.
int quiz( int n)
{
if ( n <= 1 )
return n;
else
return (--n % 2) + quiz(n/10);
}Show answer
Answer (a)
Answer (b)
31 past-paper questions on Recursion from ISC Class 12 Computer Science papers (2026-2017), newest first, in full. Questions 21-31 are on this page, 20 to a page. Tap "Show answer" under a question to see its answer. The syllabus covers Concept of recursion, Recursive methods, Base case and Tower of Hanoi.
Nothing matches. Try fewer letters.
Answer the questions on the code given below.
int quiz( int n)
{
if ( n <= 1 )
return n;
else
return (--n % 2) + quiz(n/10);
}Answer (a)
Answer (b)
Answer the following.
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);
}Answer (a)
Answer (b)
Write the program described below.
Answer
import java.util.*;
class Gcd
{
int num1, num2;
Gcd()
{
num1 = 0;
num2 = 0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = sc.nextInt();
System.out.print("Enter second number: ");
num2 = sc.nextInt();
}
int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
void display()
{
System.out.println("GCD of " + num1 + " and " + num2 + " = " + gcd(num1, num2));
}
public static void main(String[] args)
{
Gcd ob = new Gcd();
ob.accept();
ob.display();
}
}The recursive method uses Euclid's algorithm: gcd(x, y) = gcd(y, x % y) until y becomes 0. Sample run: 48 and 18 gives 6.Differentiate between the following.
Answer
Choose the correct option.
int Toy(int n)
{ return (n<=0)? 1: n%10 + Toy(n/10); }With reference to the program code given above, what will the function Toy() return when the value of $n = 56$?Answer
Correct option: (b)
Write the program described below.
Answer
import java.util.*;
class Fibo
{
int start, end;
Fibo()
{
start = 0;
end = 0;
}
void read()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter start: ");
start = sc.nextInt();
System.out.print("Enter end: ");
end = sc.nextInt();
}
int fibo(int n)
{
if (n <= 1)
return n;
return fibo(n - 1) + fibo(n - 2);
}
void display()
{
System.out.println("Fibonacci series from " + start + " to " + end + ":");
int i = 0, t = fibo(0);
while (t <= end)
{
if (t >= start)
System.out.print(t + " ");
i++;
t = fibo(i);
}
System.out.println();
}
public static void main(String[] args)
{
Fibo ob = new Fibo();
ob.read();
ob.display();
}
}fibo(n) is recursive: fibo(0) = 0, fibo(1) = 1, otherwise the sum of the previous two terms. display() prints the terms that lie between start and end. Sample run: start 2, end 50 gives 2 3 5 8 13 21 34.Answer the following question.
Answer
What is the output of the following code?
test ( ) when the value of count is equal to 4 ? Show the dry run / working.
void test (int count)
{
if (count == 0)
System.out.println(" ");
else
{
System.out.println("Bye" + count);
test(--count);
System.out.println(" " + count);
}
}Answer
What is the output of the following code?
check( ) is a part of some class. What will the function check( ) return when the value of (i) n=25 and (ii) n=10. Show the dry run/ working.
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);
}Answer
What is the output of the following code?
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.
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);
}
}Answer
What is the output of the following code?
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:
int magicfun(int n)
{
if (n == 0)
return 0;
else
return magicfun(n / 2) * 10 + (n % 2);
}Answer
Recursion is a unit of the ISC Class 12 Computer Science syllabus.
The syllabus lists these topics: Concept of recursion, Recursive methods, Base case and Tower of Hanoi.
Prashnika has 31 past-paper questions on Recursion, from papers of 2017-2026, asked in 8 different years. By kind: Case based (9), Output (5), Short answer (5) and Program (5). They carry between 1 and 10 marks, and most are worth 3 marks.
To practise, use the Recursion visualiser, the Tower of Hanoi visualiser and the paper generator, which can build a practice paper from this chapter.
The syllabus lists Concept of recursion, Recursive methods, Base case and Tower of Hanoi.
Prashnika has 31 questions on Recursion, from papers of 2017-2026 (board, specimen, improvement and practice papers together).
By kind: Case based (9), Output (5), Short answer (5) and Program (5).