As noted in the 2004 AB MC explanations, this problem is more appropriately solved using the technique presented in analyzing recursive methods. The trace below is presented as a demonstration of tracing. It is not a suggestion that this problem should be traced.
This trace uses the technique demonstrated in tracing recursive methods and some of the material from the more complex stack based trace with nested recursive calls. Familiarity with the material from those resources is assumed, including the notation.
Step 1: Initial call
Initial call stack
m(2, 3)
Step 2: mystery(2, 3)
- Checks if
2 != 0
, which istrue
, so the entire condition istrue
- Computes
mat[2][3]
, which is11
,+ ____
(something unknown) - Calls
mystery(2 - 1, 3 - 1)
, which ismystery(1, 2)
Resulting call stack
m(1, 2)
m(2, 3) 11 + ____
Step 3: mystery(1, 2)
- Checks if
1 != 0
, which istrue
, so the entire condition istrue
- Computes
mat[1][2]
, which is6
,+ ____
- Calls
mystery(1 - 1, 2 - 1)
, which ismystery(0, 1)
Resulting call stack
m(0, 1)
m(1, 2) 6 + ____
m(2, 3) 11 + ____
Step 4: mystery(0, 1)
- Checks if
0 != 0
, which isfalse
, or1 != 0
, which istrue
, so the entire condition istrue
- Computes
mat[0][1]
, which is1
,+ ____
- Calls
mystery(0 - 1, 1 - 1)
, which ismystery(-1, 0)
Resulting call stack
m(-1, 0)
m(0, 1) 1 + ____
m(1, 2) 6 + ____
m(2, 3) 11 + ____
Step 5: mystery(-1, 0)
- Checks if
-1 != 0
, which istrue
, so the entire condition istrue
- Accesses
mat[-1][0]
, which throws anArrayIndexOutOfBoundsException