Consider the following JAVA program: public class First { public static int…

2017

Consider the following JAVA program:

public class First {

public static int CBSE (int x) {

if (x < 100)x = CBSE (x+10);  

           return (x-1);

           }

    public static void main(String[]args){

           System.out.print(First.CBSE(60));

    }

}

What does this program print?

  1. A.

    59

  2. B.

    95

  3. C.

    69

  4. D.

    99

Attempted by 172 students.

Show answer & explanation

Correct answer: B

Final output: 95

Explanation: The method keeps calling itself with x increased by 10 until x is no longer less than 100. At that point the recursion stops and returns x-1, and each returning call subtracts 1 from the value it receives.

  1. Call CBSE(60): since 60 < 100, it calls CBSE(70).

  2. CBSE(70): calls CBSE(80).

  3. CBSE(80): calls CBSE(90).

  4. CBSE(90): calls CBSE(100).

  5. CBSE(100): 100 is not less than 100, so it returns 100 - 1 = 99.

  • CBSE(100) returns 99.

  • CBSE(90) receives 99, then returns 99 - 1 = 98.

  • CBSE(80) receives 98, then returns 97.

  • CBSE(70) receives 97, then returns 96.

  • CBSE(60) receives 96, then returns 95.

Therefore, System.out.print(First.CBSE(60)) prints 95.

Explore the full course: Nta Ugc Net Paper 2

Loading lesson…