Java Reference
In-Depth Information
12.
Consider the following method:
public static int
test(
int
x,
int
y)
{
if
(x == y)
return
x;
else if
(x > y)
return
(x + y);
else
return
test(x + 1, y - 1);
}
What is the output of the following statements?
a.
System.out.println(test(5, 10));
b.
System.out.println(test(3, 9));
13.
Consider the following method:
public static int
func(
int
x)
{
if
(x == 0)
return
2;
else if
(x == 1)
return
3;
else
return
(func(x - 1) + func(x - 2));
}
What is the output of the following statements?
a.
System.out.println(func(0));
b.
System.out.println(func(1));
c.
System.out.println(func(2));
d.
System.out.println(func(5));
14.
Suppose that
intArray
is an array of integers and
length
specifies the
number of elements in
intArray
. Also, suppose that
low
and
high
are two
integers such that
0 <= low < length
,
0 <= high < length
, and
low <=
high
. That is,
low
and
high
are two indices in
intArray
. Write a
recursive definition that reverses the elements in
intArray
between
low
and
high
.
15.
Write a recursive definition to multiply two positive integers m and n using
repeated addition.
16.
Consider the following problem: How many ways can a committee of four
people be selected from a group of 10 people? There are many other similar
problems, where you are asked to find the number of ways to select a set of
items from a given set of items. The general problem can be stated as
follows: Find the number of ways r different things can be chosen from a
set of n items, where r and n are nonnegative integers and r
n. Suppose
Search WWH ::
Custom Search