Espanillo Midterm Exam Data Structure 2it2
Espanillo Midterm Exam Data Structure 2it2
Espanillo Midterm Exam Data Structure 2it2
MIDTERM EXAMINATION
DATA STRUCTURE AND ALGORITHM
2IT
PART 1. MULTIPLE CHOICE. CHOOSE THE CORRECT LETTER THAT CORRESPOND TO THE
PROBLEM. (2PTS EACH)
1. Consider the following recursive function fun (x, y). What is the value of fun (4, 3)
int fun (int x, int y)
{
if (x == 0)
return y;
return fun (x - 1, x + y);
}
A. 13
B. 12
C. 9
D. 10
Answer: A.
2 {
3 if (n == 0)
4 return 1;
5 else return n *
factorial(n-1);
6
}
A. 1
B. 3
C. 4
D. 5
Answer: D
2 {
3 if (n == 0) {
4 return "*";
5 } else {
7 }
8 }
A. 6
B. 3
C. 4
D. 5
Answer: A.
4. How many recursive calls does the following method contain?
A. 0
B. 1
C. 2
D. 3
Answer: C.
5. How many recursive calls does the following method contain?
2 {
3 if (n == 1) {
4 return 2;
5 } else {
7 }
8 }
A. 0
B. 1
C. 2
D. 3
Answer: B
Answer: D
8. Which of the following piece of code has the functionality of counting the number of
elements in the list?
A. public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext();
}
return size;
}
Answer: A
Reverse a String
S = “Nalla_ Trebor”
N = 12
N = 11
RETURN(Reverse(SUBSTRING(S,2,5) || SUBSTRING(S,1,1) = alla_Trebor || N
N = 11
N = 10
RETURN(Reverse(SUBSTRING(S,2,4) || SUBSTRING(S,2,2) = lla_Trebor || aN
N=1
S = “Nalla_Trebor”
Pass#1:
N = 12
N =11
S = “Nalla_Trebor”
Pass#2:
N = 11
N =10
S = “Nalla_Trebor”
Pass#3:
N = 10
N =9
S = “Nalla_Trebor”
Pass#4:
N=9
N =8
Return ( Reverse(”a_Trebor" || “llaN” ))
Pass#5:
N=8
N=7
Pass#6:
N=7
N=6
Pass#7:
N=6
N=5
Pass#8:
N=5
N=4
Pass#9:
N=4
N=3
Pass#10:
N=3
N=2
Pass#11:
N =2
N=1
Return ( Reverse(”" || “roberT_allaN” ))
Pass#12:
N=1
Answer
Pass 1:
/*ELSE Statement: N > 0 */
Return(10 * Factorial(9))
Pass 2:
/*ELSE Statement: N > 0 */
Return(9 * Factorial(8))
Pass 3:
/*ELSE Statement: N > 0 */
Return(8 * Factorial(7))
Pass 4:
/*ELSE Statement: N > 0 */
Return(7 * Factorial(6))
Pass 5:
/*ELSE Statement: N > 0 */
Return(6 * Factorial(5))
Pass 6:
/*ELSE Statement: N > 0 */
Return(5 * Factorial(4))
Pass 7:
/*ELSE Statement: N > 0 */
Return(4 * Factorial(3))
Pass 8:
/*ELSE Statement: N > 0 */
Return(3 * Factorial(2))
Pass 9:
/*ELSE Statement: N > 0 */
Return(2 * Factorial(1))
Pass 10:
/*ELSE Statement: N > 0 */
Return(1 * Factorial(0))
Pass 11:
/*IF Statement N = 0 */
RETURN (1)
Once again, let us summarize the process by which we arrived at the solution to 10!