LearnFrenzy
Sign in Take a free test

Java Programming · Topic 6 of 28

Methods and Recursion

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Methods and Recursion · Study material

Tower of Hanoi with recursion

Moving n discs = move n − 1 out of the way, move the largest, move n − 1 back on top. The program prints every move for 3 discs and only the count for 10, 2ⁿ − 1.

public class Main {
    static int moves = 0;
    static boolean verbose = true;

    static void hanoi(int n, char from, char to, char via) {
        if (n == 0) return;
        hanoi(n - 1, from, via, to);                  // move n-1 discs out of the way
        moves++;
        if (verbose) System.out.println("move disc " + n + " from " + from + " to " + to);
        hanoi(n - 1, via, to, from);                  // put them back on top
    }

    public static void main(String[] args) {
        hanoi(3, 'A', 'C', 'B');
        System.out.println(moves + " moves for 3 discs");
        moves = 0;
        verbose = false;                              // 1023 moves: just count them
        hanoi(10, 'A', 'C', 'B');
        System.out.println(moves + " moves for 10 discs (2^n - 1)");
    }
}

Output

move disc 1 from A to C
move disc 2 from A to B
move disc 1 from C to B
move disc 3 from A to C
move disc 1 from B to A
move disc 2 from B to C
move disc 1 from A to C
7 moves for 3 discs
1023 moves for 10 discs (2^n - 1)

Factorial, digit sum, fast power and GCD

The recursion example implements four classic recursive functions, each with a clear base case.

Visualising the call stack

The call-stack example prints "enter" and "leave" with indentation, showing frames being pushed and popped.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Java Programming

Java is one of the most used languages in the world – for enterprise software, Android apps, banking systems and backend services. This section covers core Java from syntax and data types to OOP, strings, arrays, exceptions, collections and multithreading, with clear explanations and practice questions for every topic.

Why it matters

  • Java is the main language in service-company placement tests and many product companies
  • OOP, String, collections and exception questions appear in nearly every Java interview
  • Strong core Java is the base for Spring Boot, Android and backend development
  • Java questions are common in university exams and certification tests

Who it is for

  • School students (Class 11–12 / ICSE computer applications) learning Java
  • College students (B.Tech, BCA, MCA) preparing for exams, labs and projects
  • Freshers preparing for placements, Java developer roles and technical interviews
  • Developers revising core Java before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Java developer interviews
  • Service company tests
  • University exams
  • Certification exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links