So, what the question asks is what will the output of this method be when you pass it a 3.
I plugged it into JCreator and got 010203010 and I am utterly confused how.
public static void printX(int n){
if (n<= 0)
System.out.print(0);
else{
printX(n-1);
System.out.print(n);
printX(n-2);
}
}
So my logic with this is, So it will call 3-1 which will call 2-1 which will call 1-1 which will print out a zero, print out a one, and then print out another zero because 1-2 is less than 0.
So we have 010 so far on this first "plug in" on the recursive way back up.
So here is where it confuses me, it doesn't really return anything, it just prints, so calling printX(2-1) now, where the hell does it get its value? All I can think it will do is call itself again and again and just calling it self one less time, and reaching the base case like 4 or 5 different times.
printx(n-1)outputs it prints outnand then it repeats the same recursion ofprint(n-1). It does this in EVERY instance except the ones that hit the base case ofn <= 0where it just prints0. It's pretty much like if you had 3System.out.println("Im Here");in code you'd expect the two last to execute after the first had finished.