Recursion
Post Test
1. Which of the following statements are true?
I. A recursive method is a method that calls itself.
II. A recursive solution is always more complex than its equivalent iterative solution.
III. Recursion is a special type of repetition control.
IV. A recursive solution is always more efficient than its equivalent iterative solution.
a. I & II
b. I, II & III
c. I, II & IV
d. I & III
e. II & IV
2. It is appropriate to use recursion if
a. A recursive solution is natural and easy to understand.
b. A recursive solution does not result in excessive duplicate computation.
c. The equivalent iterative solution is too complex.
d. All of the above.
3. The basic
elements of a recursive method are:
I.
A test to stop or continue the recursion.
II.
A test to verify the method logic.
III.
An end case that terminates the program.
IV.
An end case that terminates the recursion.
V.
Recursive call(s) that continues the recursion.
a.
I, II & III
b.
I, II & V
c.
I, IV & V
d.
II, III & V
Comprehension:
4.
You have an iterative Java method, computeTotal().
When is it possible to write a recursive version of computeTotal()?
a. It is always
possible to write a recursive version of computeTotal().
b. It is
generally possible to write a recursive version of computeTotal().
c. It is rarely
possible to write a recursive version of computeTotal().
d. It is never
possible to write a recursive version of computeTotal().
Application:
5.
Given the following recursive Java code that lists
the contents of a directory on your hard drive (including the contents of any
subdirectories):
1 public void directoryListing(File
file){
2
3 String [] fileList;
4 String pathname = file.getAbsolutePath();
5
6 if (file.isFile()) {
7 System.out.println(file.getName());
8 } else {
10 fileList =
file.list();
11 for (int i = 0; i
< fileList.length; i++){
12 File nextFile =
new File(pathname + File.separator
+ fileList[i]);
13 directoryListing(nextFile);
14 } /* for */
15 } /* else */
16 }
Which line contains the test condition?
6.
Given the following recursive Java code that lists
the contents of a directory on your hard drive (including the contents of any
subdirectories):
1 public void directoryListing(File
file){
2
3 String [] fileList;
4 String pathname = file.getAbsolutePath();
5
6 if (file.isFile()) {
7 System.out.println(file.getName());
8 } else {
10 fileList =
file.list();
11 for (int i = 0; i
< fileList.length; i++){
12 File nextFile =
new File(pathname + File.separator
+ fileList[i]);
13 directoryListing(nextFile);
14 } /* for */
15 } /* else */
16 }
Which line contains the end case?
7.
Given the following recursive Java code that lists
the contents of a directory on your hard drive (including the contents of any
subdirectories):
1 public void directoryListing(File
file){
2
3 String [] fileList;
4 String pathname = file.getAbsolutePath();
5
6 if (file.isFile()) {
7 System.out.println(file.getName());
8 } else {
10 fileList = file.list();
11 for (int i = 0; i
< fileList.length; i++){
12 File nextFile =
new File(pathname + File.separator
+ fileList[i]);
13 directoryListing(nextFile);
14 } /* for */
15 } /* else */
16 }
Which line contains the recursive call?
8.
Given the following method, recurse():
int recurse(int v)
{ if (v>6) return recurse(v); return v;
}
Which of
the following statements is true?
a. The method
will always terminate properly.
b. When v is
initialized to a value of 6 or less, the method will never begin to recurse; when v is initialized to a value greater than 6,
it will not terminate.
c. The method
will never terminate properly.
d. The method will terminate only when the
parameter to the method is greater than 6.
9.
Given the following recursive function
definition for compute():
compute(a,
0) = 1
What is the result
of compute(3,4)?
a.
0
b.
81
c.
64
d.
27
e.
12
10. Given the
following recursive function definition, div(), for integers N and M:
div( N, M ) = 0, if N < M
div( N, M ) = 1 + div( N-M, M ), if N
>= M
Assume 0 <= N and 0 < M. Which of the following code segments correctly implements div()?
a)
int
div( int N, int M ) {
if ( N < M )
return N; else return div( N-M, M );}
b)
int
div( int N, int M ) {
if ( N == M )
return N; else return div( N-M, M );}
c)
int
div( int N, int M ) {
if ( N < M )
return 0; else return 1+div( N-M, N ); }
d)
int
div( int N, int M ) {
if ( N < M )
return 0; else return 1+div( N-M, M );}