go to previous page   go to home page   go to next page hear noise highlighting

Answer:

Triangle() is activated with a parameter of 3.


New Activation

The statement:

    return N + Triangle( N-1 );

must have a value for Triangle(N-1) before the addition can be done. So the first activation of Triangle() causes a second activation of Triangle(), this time with a parameter of 3. See the picture.

Each circle represents an activation. The first activation (with 4) is waiting for the new activation (with 3) to return a result.

two activations

QUESTION 14:

Look at the code again. Think about the latest activation. What does the activation Triangle(3) do?

public int Triangle( int N )
{
  if ( N == 1 )
    return 1;
  else
    return N + Triangle( N-1 );
}

go to previous page   go to home page   go to next page