All Exams Test series for 1 year @ ₹349 only
Question

Consider the following recursive function F() in Java that takes an integer value and returns a string value :

public static String F(int N) {

if ( N <= 0) return "-";

return F(N - 3) + N + F(N - 2) + N;

}

The value of F(5) is :

The correct answer is

- 2- 25 -3 -1 -135

Step 1: The base case returns “-”. Hence F(1) = “-1-1” and F(2) = “-2-2”.

Step 2: F(3) = F(0) + “3” + F(1) + “3” = “-3-1-13”.

Step 3: F(5) = F(2) + “5” + F(3) + “5” = “-2-25-3-1-135”. Concatenation proceeds from left to right because the first operand is a string.

Was this answer helpful?

Important Questions from Java

  1. Which of the following statement(s) is/are TRUE regarding Java Servelets?

    (a) A Java Servlet is a server-side component that runs on the webserver and extends the capabilities of a server.

    (b) A Servelet can use the user interface classes like AWT or Swing.

  2. What is the output of the following JAVA program?

    class Test

    {

    public static void main (String [ ] args)

    {

    Test obj = new Test ();

    obj.start ();

    }

    void start ()

    {

    String stra="do”;

    String strb = method (stra);

    System.out.print(“:” + stra + strb);

    }

    String method (String stra)

    {

    stra=stra+”good”;

    System.out.print (stra);

    return “good”;

    }

    }

  3. A java program that is embedded in HTML document and runs in the context of java capable browser is known as ______.

  4. JAVA compiler translates source code into _______.

  5. Which of the following statement(s) is/are correct regarding java programming?

    I. Place a semicolon immediately after a method header

    II. Define a method outside the braces of a class definition

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App