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
LinearNode
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started