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?
- A.
59
- B.
95
- C.
69
- 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.
Call CBSE(60): since 60 < 100, it calls CBSE(70).
CBSE(70): calls CBSE(80).
CBSE(80): calls CBSE(90).
CBSE(90): calls CBSE(100).
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.