Exam

Name___________________________________

TRUE/FALSE.  Write 'T' if the statement is true and 'F' if the statement is false. 

1) 

A recursive method without a base case leads to infinite recursion. 

1) 

_______ 

2) 

The following method lacks a base case.
	public int noBaseCase(int x)
	{
	if(x > 0)
	return noBaseCase(x - 1) + 1;
	else return noBaseCase(x - 2) + 2;
	} 

2) 

_______ 

3) 

Traversing a maze is much easier to do iteratively than recursively. 

3) 

_______ 

4) 

Some problems are easier to solve recursively than iteratively. 

4) 

_______ 

5) 

The following two methods will both compute the same thing when invoked with the same value of x. That is, method1(x) = = method2(x).
	public int method1(int x)
	{
	if(x > 0) return method1(x - 1) + 1;
	else return 0;
	}

	public int method2(int x)
	{
	if(x > 0) return 1 + method2(x - 1);
	else return 0;
	} 

5) 

_______ 

6) 

Consider the following recursive sum method
	public int sum(int x)
	{
	if(x = = 0) return 0;
	else return sum(x - 1) + 1;
	}
	If the base case is replaced with "if(x = = 1) return 1;"  the method will still 	compute the same thing. 

6) 

_______ 

7) 

The recursive method to solve the Towers of Hanoi is usable only if the parameter for the number of disks is 7 or smaller. 

7) 

_______ 

8) 

Since iterative solutions often use loop variables and recursive solutions do not, the recursive solution is usually more memory efficient (uses less memory) than the equivalent iterative solution. 

8) 

_______ 

9) 

A derived class has access to all of the methods of the parent class, but not to any of the instance data of the parent class. 

9) 

_______ 

10) 

If class AParentClass has a public instance variable x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but cannot redefine x to be a different type. 

10) 

______ 

11) 

In an interface, the programmer has the choice of providing a method body or not. 

11) 

______ 

12) 

A class that implements an interface must implement every method in the interface. 

12) 

______ 

13) 

A class representing a car might implement the List interface. 

13) 

______ 

14) 

If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if Cint c = new C1( ); is performed at one point of the program, then a later instruction c.whichIsIt( ); will invoke the whichIsIt method defined in C1. 

14) 

______ 

For the question(s) below, assume that Poodle and Retriever are derived classes of Dog and that Dog d = new Dog(...), Poodle p = new Poodle(...), and Retriever r = new Retriever(...); where the ... are the necessary parameters for the classes.

15) 

The statements d = p; d = r; demonstrate polymorphism. 

15) 

______ 

16) 

The statement r = (Retriever)p; is valid. 

16) 

______ 

17) 

Every object in Java, no matter what class it came from, has a method called toString. 

17) 

______ 

18) 

The equals method will always return false when comparing two objects of different classes. 

18) 

______ 

19) 

An abstract class can only be instantiated if at least half of its methods are defined. 

19) 

______ 

20) 

Polymorphism works with inheritance only; it does not work with interfaces. 

20) 

______ 

21) 

Assume the method below is called initially with i = 0
	public int question(String a, char b, int i)
	{
	if (i = = a.length( )) return 0;
	else if (b = = a.charAt(i)) return question(a, b, i+1) + 1;
	else return question(a, b, i+1);
	}
	The method returns 1 if char b appears in String a at least once, and 0 otherwise. 

21) 

______ 

22) 

The recursive method below would execute approximately log 2 n times for an initial parameter n.
	public void logcode(int n)
	{
	if (n > 1) logcode(n / 2);
	} 

22) 

______ 

23) 

A recursive algorithm is superior to an iterative algorithm because it is computationally more efficient. 

23) 

______ 

24) 

The method below computes the value of x % y (x mod y) recursively, assuming that x and y not negative numbers.
	public int recursiveMod(int x, int y)
	{
	if (x < y) return x;
	else return recursiveMod(x - y, y);
	} 

24) 

______ 

25) 

Aside from writing recursive methods, another way that recursion is often used is to define mathematical functions. 

25) 

______ 

26) 

Merge sort and quick sort are more time efficient than insertion sort and selection sort. 

26) 

______ 

27) 

Recursion can be used when sorting an array of elements. 

27) 

______ 

28) 

A recursive sort typically divides the list into smaller sublists which are sorted recursively. 

28) 

______ 

29) 

The pivot value selected in a quick sort should be the smallest element in the list. 

29) 

______ 

30) 

A recursive method that doesn't return anything with result in infinite recursion. 

30) 

______ 

31) 

If method f1 calls method f2 calls method f3, we have indirect recursion. 

31) 

______ 

 

1) 

TRUE 

2) 

TRUE 

3) 

FALSE 

4) 

TRUE 

5) 

TRUE 

6) 

FALSE 

7) 

FALSE 

8) 

FALSE 

9) 

FALSE 

10) 

FALSE 

11) 

FALSE 

12) 

TRUE 

13) 

FALSE 

14) 

FALSE 

15) 

TRUE 

16) 

FALSE 

17) 

TRUE 

18) 

FALSE 

19) 

FALSE 

20) 

FALSE 

21) 

FALSE 

22) 

TRUE 

23) 

FALSE 

24) 

TRUE 

25) 

TRUE 

26) 

TRUE 

27) 

TRUE 

28) 

TRUE 

29) 

FALSE 

30) 

FALSE 

31) 

FALSE