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. Arrange the following applet life cycles methods in sequence 

    A. start() 

    B. paint() 

    C. init() 

    D. stop() 

    Choose the correct answer from the options given below:

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

  3. JAVA compiler translates source code into _______.

  4. 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

  5. The application in java, which is used for sending small applications to run at client side is __________.

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