Question: 1. Consider the following method. Is this a correct recursive implementation for factorial? intfactorial(int n) { i f ( n > 1 ) return n

1. Consider the following method. Is this a correct recursive implementation for factorial?

intfactorial(int n) {

i f ( n > 1 )

return n factorial(n1);

else

return 0 ; }

(a) Yes, it calls itself on the n-1 sub-problem.

(b) Yes, it includes both a base case and recursive step.

(c) No, the n > 1 condition is not correct for factorial.

(d) No, the return value is not correct for factorial.

2. Is the following a correct implementation of a method to add a node (containing element) to the end of a list referenced by front?

public void addLast(T element, LinearNode front) {

LinearNode newNode = new LinearNode(element);

while(front.getNext() != null)

front = front.getNext();

newNode = setNext(front);

front = newNode;

}

(a) Yes, if the element isn't null.

(b) Yes, provided that LinearNode has been properly set up.

(c) No, it can't add to a full list.

(d) No, it can't add to an empty list.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!