You are on page 1of 10

NAME:_Karen D.

Espanillo____________ DATE:___oct 8___________


YEAR & PROGRAM:_2021 BSIT____________ SCORE:_____________

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. Which line has the recursive call?


public static int
1 factorial (int n)

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

3. Which line has the recursive call?


1 public String starString(int n)

2 {

3 if (n == 0) {

4 return "*";

5 } else {

6 return starString(n - 1) + starString(n - 1);

7 }

8 }

A. 6
B. 3
C. 4
D. 5
Answer: A.
4. How many recursive calls does the following method contain?

public static int fibonacci(int n)


1
{
2
if (n == 0)
3
return 0;
4
else if (n == 1)
5
return 1;
6
else return fibonacci(n-1) + fibonacci(n-2);
7
}
8

A. 0
B. 1
C. 2
D. 3
Answer: C.
5. How many recursive calls does the following method contain?

1 public static int multiplyEvens(int n)

2 {

3 if (n == 1) {

4 return 2;

5 } else {

6 return 2 * n * multiplyEvens(n - 1);

7 }

8 }

A. 0
B. 1
C. 2
D. 3
Answer: B

6. What is the functionality of the following code?


7. public void function(Node node)
8. {
9. if(size == 0)
10. head = node;
11. else
12. {
13. Node temp,cur;
14. for(cur = head; (temp = cur.getNext())!=null; cur = temp);
15. cur.setNext(node);
16. }
17. size++;
18. }
A. Inserting a node at the beginning of the list
B. Deleting a node at the beginning of the list
C. Inserting a node at the end of the list
D. Deleting a node at the end of the list
Answer: C
7. Which of these is not an application of a linked list?
A. To implement file systems.
B. For separate chaining in hash-tables
C. To implement non-binary trees.
D. Random Access of elements

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;
}

B. public int length(Node head)


{
int size = 0;
Node cur = head;
while(cur!=null)
{
cur = cur.getNext();
size++;
}
return size;
}
C. public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext();
}
}
D.public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext().getNext();
}
return size;
}

Answer: A

9. How do you insert an element at the beginning of the list?


A. public void insertBegin(Node node)
{
node.setNext(head);
head = node;
size++;
}
B. public void insertBegin(Node node)
{
head = node;
node.setNext(head);
size++;
}
C. public void insertBegin(Node node)
{
Node temp = head.getNext()
node.setNext(temp);
head = node;
size++;
}
D. public void insertBegin(Node node)
{
Node temp = head.getNext()
node.setNext(temp);
node = head;
size++;
}
Answer: A
10. Which of the following is false about a doubly linked list?
a) We can navigate in both the directions
b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) Implementing a doubly linked list is easier than singly linked list
Answer: D

PART II. APPLICATION OF REVERSE STRING. (15PTS)


Let us assume that: S = “Nalla_Trebor”
Using the pseudocode. Try to simulate the given string and possible output of the string.
Reverse(S)
{
Set N to the length of string S
if (N = 1) then
{
RETURN (S)
}
Else
{
Decrement N
RETURN (Reverse (SUBSTRING(S,2,N) || SUBSTRING(S,1,1)
}
}

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

Return ( Reverse(”alla_Trebor" || “N” ))

S = “Nalla_Trebor”
Pass#2:
N = 11
N =10

Return ( Reverse(”lla_Trebor" || “aN” ))

S = “Nalla_Trebor”
Pass#3:
N = 10
N =9

Return ( Reverse(”la_Trebor" || “laN” ))

S = “Nalla_Trebor”
Pass#4:
N=9
N =8
Return ( Reverse(”a_Trebor" || “llaN” ))
Pass#5:
N=8
N=7

Return ( Reverse(”_Trebor" || “allaN” ))

Pass#6:
N=7
N=6

Return ( Reverse(”Trebor" || “_allaN” ))

Pass#7:
N=6
N=5

Return ( Reverse(”rebor" || “T_allaN” ))

Pass#8:
N=5
N=4

Return ( Reverse(”ebor" || “rT_allaN” ))

Pass#9:
N=4
N=3

Return ( Reverse(”bor" || “erT_allaN” ))

Pass#10:
N=3
N=2

Return ( Reverse(”or" || “berT_allaN” ))

Pass#11:
N =2
N=1
Return ( Reverse(”" || “roberT_allaN” ))
Pass#12:

N=1

PART III. APPLICATION OF FACTORIAL NUMBERS. (15PTS.)


Let us assume that N = 10
Using the pseudocodes. Try to simulate the given integer and possible output of the program.
Factorial (N)
{
if (N = 0)
{
RETURN (1)
}
Else
{
RETURN (N*(Factorial (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!

1.10 * Factorial (9)


2.10 * 9 * Factorial(8)
3.10 * 9 * 8 * Factorial(7)
4.10 * 9 * 8 * 7 * Factorial(6)
5.10 * 9 * 8 * 7 * 6 * Factorial(5)
6.10 * 9 * 8 * 7 * 6 * 5 * Factorial(4)
7.10 * 9 * 8 * 7 * 6 * 5 * 4 * Factorial(3)
8.10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * Factorial(2)
9.10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * Factorial(1)
10.10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * Factorial(0)
11.10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 *
12.3,628,800

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END of EXAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You might also like