Directions : Show the output for each of the following code segments.
Once you have written answers to all questions, use JDoodle to check your answers.
1.
public static int bear( int x )
{
if( x > 0 )
return x + bear( x - 1 );
return 0;
}
//client code in the runner / main method
[Link]( bear(6) );
2.
public static int bear( int x )
{
if( x > 0 )
return x + bear( x - 1 );
return 0;
}
//client code in the runner / main method
[Link]( bear(11) );
3.
public static int car( int x )
{
if( x > 0 )
return car( x - 2 ) + 1;
return 0;
}
//client code in the runner / main method
[Link]( car(16) );
4.
public static int car( int x )
{
if( x > 0 )
return car( x - 2 ) + 1;
return 0;
}
//client code in the runner / main method
[Link]( car(26) );
5.
public static int bear( int x )
{
if( x > 0 )
return x + bear( x - 2 );
return 1;
}
//client code in the runner
[Link]( bear( 3 ) );
6.
public static int bear( int x )
{
if( x > 0 )
return x + bear( x - 2 );
return 1;
}
//client code in the runner
[Link]( bear( 8 ) );
7.
public static int car( int x )
{
if( x > 0 )
return car( x - 3 ) + 2;
return -1;
}
//client code in the runner
[Link]( car( 16 ) );
8.
public static int box( int x )
{
if( x > 0 )
return box( x - 2 ) + box( x – 3) + 1;
return 2;
}
//client code in the runner
[Link]( box( 17 ) );