Question 1 What will be the output of the following program?
Sigmoid technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output of the following program?
public class Main {
public static void main(String[] args) {
System.out.println(Recur(6));
}
public static int Recur(int num){
if(num==2) return 2;
if(num==1) return 1;
return Recur(num-1)*Recur(num-2);
}
}
Select an option:
Show answer & explanation
Answer: B. B) 32
Evaluating the recurrence: Recur(1)=1, Recur(2)=2, Recur(3)=21=2, Recur(4)=22=4, Recur(5)=42=8, Recur(6)=84=32.
Step-by-step Derivation:
Step 1: Base cases: Recur(1) = 1, Recur(2) = 2.
Step 2: Recur(3) = Recur(2) * Recur(1) = 2 * 1 = 2.
Step 3: Recur(4) = Recur(3) * Recur(2) = 2 * 2 = 4.
Step 4: Recur(5) = Recur(4) * Recur(3) = 4 * 2 = 8.
Step 5: Recur(6) = Recur(5) * Recur(4) = 8 * 4 = 32.