Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Recursion Practice

Recursive thinking: factorial, Fibonacci, digits, GCD, Tower of Hanoi and more.

1
Factorial (Recursive)
Easy
Write a recursive method long factorial(int n) and use it to print N!.
A single integer N.
A single integer: N!.
Sample Input 1
5
Sample Output 1
120
Sample Input 2
0
Sample Output 2
1
Sample Input 3
1
Sample Output 3
1
Sample Input 4
10
Sample Output 4
3628800
Sample Input 5
20
Sample Output 5
2432902008176640000
0
fact(n) = n * fact(n-1), with fact(0) = 1.
2
Sum of N (Recursive)
Easy
Write a recursive method int sum(int n) that returns 1 + 2 + ... + n, and use it to print the sum.
A single integer N.
A single integer: the sum.
Sample Input 1
10
Sample Output 1
55
Sample Input 2
5
Sample Output 2
15
Sample Input 3
1
Sample Output 3
1
Sample Input 4
100
Sample Output 4
5050
Sample Input 5
999
Sample Output 5
499500
1
sum(n) = n + sum(n-1), with sum(0) = 0.
3
Fibonacci (Recursive)
Easy
Write a recursive method int fib(int n) that returns F(N), where F(0)=0, F(1)=1, and use it to print the answer.
A single integer N.
A single integer: F(N).
Sample Input 1
6
Sample Output 1
8
Sample Input 2
10
Sample Output 2
55
Sample Input 3
0
Sample Output 3
0
Sample Input 4
1
Sample Output 4
1
Sample Input 5
15
Sample Output 5
610
0
fib(n) = fib(n-1) + fib(n-2).
4
Power (Recursive)
Easy
Write a recursive method long power(int b, int e) and use it to print B^E.
A single line with two integers B and E.
A single integer: B^E.
Sample Input 1
2 10
Sample Output 1
1024
Sample Input 2
5 3
Sample Output 2
125
Sample Input 3
0 0
Sample Output 3
1
Sample Input 4
0 5
Sample Output 4
0
Sample Input 5
10 10
Sample Output 5
10000000000
0
pow(b, e) = b * pow(b, e-1), with pow(b, 0) = 1.
5
Print N to 1 (Recursive)
Easy
Write a recursive method void printDesc(int n) that prints n, n-1, ..., 1 each on its own line, and call it from main.
A single integer N.
N lines: N, N-1, ..., 1.
Sample Input 1
3
Sample Output 1
3
2
1
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
5
4
3
2
1
Sample Input 4
2
Sample Output 4
2
1
Sample Input 5
7
Sample Output 5
7
6
5
4
3
2
1
1
printDesc(n): if n == 0 return; print n; printDesc(n-1).
1
Sum of Digits (Recursive)
Medium
Write a recursive method int sumDigits(int n) and use it to print the digit sum of N.
A single integer N.
A single integer: the digit sum.
Sample Input 1
1234
Sample Output 1
10
Sample Input 2
999
Sample Output 2
27
Sample Input 3
0
Sample Output 3
0
Sample Input 4
1000000000
Sample Output 4
1
Sample Input 5
987654321
Sample Output 5
45
1
sumDigits(n) = n % 10 + sumDigits(n / 10), base n == 0.
2
Count Digits (Recursive)
Medium
Write a recursive method int countDigits(int n) and use it to print the number of digits in N.
A single integer N.
A single integer: the digit count.
Sample Input 1
12345
Sample Output 1
5
Sample Input 2
7
Sample Output 2
1
Sample Input 3
0
Sample Output 3
1
Sample Input 4
1000000000
Sample Output 4
10
Sample Input 5
999
Sample Output 5
3
1
countDigits(n) = 1 + countDigits(n / 10), base n == 0.
3
GCD (Recursive)
Medium
Write a recursive method int gcd(int a, int b) and use it to print the GCD.
A single line with two integers A and B.
A single integer: gcd(A, B).
Sample Input 1
48 18
Sample Output 1
6
Sample Input 2
12 8
Sample Output 2
4
Sample Input 3
17 5
Sample Output 3
1
Sample Input 4
100 75
Sample Output 4
25
Sample Input 5
1000000 500000
Sample Output 5
500000
1
gcd(a, b) = gcd(b, a % b), base b == 0 → a.
1
Tower of Hanoi
Hard
Write a recursive method void hanoi(int n, char from, char helper, char to) and print each move in the format "Move disk from X to Y". The pegs are A (source), B (helper), C (target).
A single integer N.
2^N - 1 lines, each "Move disk from X to Y".
Sample Input 1
1
Sample Output 1
Move disk from A to C
Sample Input 2
2
Sample Output 2
Move disk from A to B
Move disk from A to C
Move disk from B to C
Sample Input 3
3
Sample Output 3
Move disk from A to C
Move disk from A to B
Move disk from C to B
Move disk from A to C
Move disk from B to A
Move disk from B to C
Move disk from A to C
Sample Input 4
4
Sample Output 4
Move disk from A to B
Move disk from A to C
Move disk from B to C
Move disk from A to B
Move disk from C to A
Move disk from C to B
Move disk from A to B
Move disk from A to C
Move disk from B to C
Move disk from B to A
Move disk from C to A
Move disk from B to C
Move disk from A to B
Move disk from A to C
Move disk from B to C
1
hanoi(n, from, helper, to): move n-1 from→helper; print from→to; move n-1 helper→to.
2
Check Palindrome (Recursive)
Hard
Write a recursive method boolean isPalindrome(String s) and use it to print "Palindrome" or "Not Palindrome".
A single line containing a string S.
"Palindrome" or "Not Palindrome".
Sample Input 1
racecar
Sample Output 1
Palindrome
Sample Input 2
hello
Sample Output 2
Not Palindrome
Sample Input 3
a
Sample Output 3
Palindrome
Sample Input 4
abba
Sample Output 4
Palindrome
Sample Input 5
12321
Sample Output 5
Palindrome
1
Compare first and last chars, recurse on the middle.
Competitive MCQs — Java Recursion
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 34
Q1
What is a recursive method?
Correct!
Wrong — correct answer is .
Recursion solves problems by calling the method on smaller inputs.
Q2
What is the output of this code?
java
1
2
3
4
5
static int fact(int n) {
    if (n <= 1) return 1;
    return n * fact(n - 1);
}
System.out.println(fact(5));
Correct!
Wrong — correct answer is .
fact(5) = 5×4×3×2×1 = 120.
Q3
What is the output of this code?
static int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
System.out.println(fact(5));
Correct!
Wrong — correct answer is .
fact(5) = 5×4×3×2×1 = 120.
Q4
What stops infinite recursion?
Correct!
Wrong — correct answer is .
The base case provides a stopping condition.
Q5
What error occurs with unbounded recursion?
Correct!
Wrong — correct answer is .
Each call uses stack space; too deep → StackOverflowError.
Q6
What is the output of this code?
java
1
2
3
4
5
static int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
System.out.println(fib(5));
Correct!
Wrong — correct answer is .
Fibonacci: 0,1,1,2,3,5 — fib(5)=5.
Q7
What is the output of this code?
static int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
System.out.println(fib(5));
Correct!
Wrong — correct answer is .
Fibonacci: 0,1,1,2,3,5 — fib(5)=5.
Q8
What is the output of this code?
java
1
2
3
4
5
static int sum(int n) {
    if (n == 0) return 0;
    return n + sum(n - 1);
}
System.out.println(sum(4));
Correct!
Wrong — correct answer is .
sum(4) = 4+3+2+1+0 = 10.
Q9
What is the output of this code?
static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
System.out.println(sum(4));
Correct!
Wrong — correct answer is .
sum(4) = 4+3+2+1+0 = 10.
Q10
Which of these is typically solved elegantly with recursion?
Correct!
Wrong — correct answer is .
Tower of Hanoi is the classic recursive problem.
Q11
How many calls does fib(3) make (including the initial call)?
Correct!
Wrong — correct answer is .
fib(3) calls fib(2) and fib(1); fib(2) calls fib(1)+fib(0) → 5 calls total.
Q12
What is the output of this code?
java
1
2
3
4
5
6
static void down(int n) {
    if (n == 0) return;
    System.out.print(n + " ");
    down(n - 1);
}
down(3);
Correct!
Wrong — correct answer is .
Prints 3, recurses 2, recurses 1, then stops.
Q13
What is the output of this code?
static void down(int n) {
if (n == 0) return;
System.out.print(n + " ");
down(n - 1);
}
down(3);
Correct!
Wrong — correct answer is .
Prints 3, recurses 2, recurses 1, then stops.
Q14
Every recursive method can be rewritten using:
Correct!
Wrong — correct answer is .
Recursion and iteration are equivalent in expressive power.
Q15
What is the output of this code?
static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
System.out.println(sum(3));
Correct!
Wrong — correct answer is .
sum(3) = 3 + 2 + 1 + 0 = 6.
Q16
What is the output of this code?
static void count(int n) {
if (n == 0) return;
System.out.print(n + " ");
count(n - 1);
}
count(3);
Correct!
Wrong — correct answer is .
The method prints n then recurses until zero.
Q17
What error occurs with very deep recursion?
Correct!
Wrong — correct answer is .
Each call consumes stack; too much depth overflows it.
Q18
What is the output of this code?
static int pow(int b, int e) {
if (e == 0) return 1;
return b * pow(b, e - 1);
}
System.out.println(pow(2, 4));
Correct!
Wrong — correct answer is .
pow(2, 4) multiplies 2 four times: 16.
Q19
What is a base case in recursion?
Correct!
Wrong — correct answer is .
The base case returns directly without another recursive call.
Q20
What happens if a recursive method has no base case?
Correct!
Wrong — correct answer is .
Without a stop condition, calls nest until the stack overflows.
Q21
What is the output of this code?
static int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
System.out.println(fib(6));
Correct!
Wrong — correct answer is .
Fibonacci: 0,1,1,2,3,5,8 — fib(6) is 8.
Q22
Which problem is classically solved by recursion?
Correct!
Wrong — correct answer is .
Tower of Hanoi maps naturally onto recursive moves.
Q23
What is the output of this code?
static void up(int n) {
if (n == 0) return;
up(n - 1);
System.out.print(n + " ");
}
up(3);
Correct!
Wrong — correct answer is .
The print happens after recursion unwinds, so numbers appear ascending.
Q24
Which data structure does recursion rely on?
Correct!
Wrong — correct answer is .
Pending calls are stacked and popped as returns occur.
Q25
What is the output of this code?
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
System.out.println(gcd(10, 15));
Correct!
Wrong — correct answer is .
gcd(10,15) → gcd(15,10) → gcd(10,5) → gcd(5,0) = 5.
Q26
What is recursion depth?
Correct!
Wrong — correct answer is .
Depth grows with each pending call before any return.
Q27
What is the output of this code?
static int f(int n) {
return n <= 1 ? 1 : n * f(n - 1);
}
System.out.println(f(3));
Correct!
Wrong — correct answer is .
f(3) = 3 × f(2) = 3 × 2 × f(1) = 6.
Q28
How many calls does f(3) make in this code?
static int f(int n) {
if (n <= 1) return 1;
return f(n - 1) + f(n - 1);
}
f(3);
Correct!
Wrong — correct answer is .
Each call spawns two until the base case: 1 + 2 + 4 = 7 total.
Q29
What is direct recursion?
Correct!
Wrong — correct answer is .
Direct recursion is a self-call in the method body.
Q30
What is indirect recursion?
Correct!
Wrong — correct answer is .
Indirect recursion cycles through other methods back to the first.
Q31
What is the output of this code?
static int f(int n) {
if (n < 10) return n;
return f(n / 10);
}
System.out.println(f(12345));
Correct!
Wrong — correct answer is .
Repeated division drops digits until only the first digit 1 remains.
Q32
Which is TRUE about recursive vs iterative solutions?
Correct!
Wrong — correct answer is .
Recursion and iteration can express the same computations.
Q33
What is the output of this code?
static int f(int n) {
if (n == 1) return 1;
return 1 + f(n - 1);
}
System.out.println(f(5));
Correct!
Wrong — correct answer is .
Four recursive steps plus the base return total 5.
Q34
Which keyword prevents recursion from ever stopping?
Correct!
Wrong — correct answer is .
Recursion stops only when a base case is reached.