Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

UNIVERSITY OF LIMPOPO

FACULTY OF SCIENCE AND AGRICULTURE

SCHOOL OF MATHEMATICAL AND COMPUTER SCIENCES

DEPARTMENT OF COMPUTER SCIENCE

DEGREE AND DIPLOMA EXAMINATION MAY/JUNE: 2023

MODULE: SCOA021 PAPER: P

(DATA STRUCTURES AND COMPILER THEORY)

TIME: 3 HOURS MARKS: 100

INTERNAL EXAMINERS: MR TB SHIKWAMBANA


MR PS RAMALEPE

SECOND EXAMINER: DR TI MODIPA

THIS PAPER CONSISTS OF …14…PAGES INCLUDING COVER PAGE

INSTRUCTIONS:

1. This paper consists of two sections (Section A and Section B).


2. Write all the sections and each section on a separate answer book.
3. Answer all the questions.
4. Answer Section A question 1 on the multiple choice answer sheet provided.
5. Start each question on a new page.
6. Write neatly and legibly.
SCOA021 MAY/JUNE EXAMINATION 2023

Section A [Data Structures] [50]

Question 1 [30]

Choose the letter that best answers the question. [1 mark each question].

1. Which of the data types below does not allow duplicates?


a. Set
b. List
c. Vector
d. Stack
e. LinkedList

2. Which of the following data types does not implement the Collection interface?
a. HashSet
b. TreeSet
c. ArrayList
d. LinkedList
e. Map

3. If two objects o1 and o2 are equal, what are the values for o1.equals(o2) and o1.hashCode() ==
o2.hashCode()?
a. true true
b. true false
c. false true
d. false false

4. Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s1 is __________.
a. [1, 2, 2, 3, 5, 6]
b. [1, 2, 3, 5, 6]
c. [1, 5]
d. [2]

5. The output of the following code is ____________.

2|Page
SCOA021 MAY/JUNE EXAMINATION 2023

LinkedHashSet<String> set1 = new LinkedHashSet<>();


set1.add("New York");
LinkedHashSet<String> set2 = (LinkedHashSet<String>)(set1.clone());
set1.add("Atlanta");
set2.add("Dallas");
System.out.println(set2);
a. [New York]
b. [New York, Atlanta]
c. [New York, Atlanta, Dallas]
d. [New York, Dallas]

6. If you want to store non-duplicated objects in the order in which they are inserted, you should use
____________.
a. HashSet
b. LinkedHashSet
c. TreeSet
d. ArrayList
e. LinkedList

7. Which of the following is correct to perform the set intersection of two sets s1 and s2?
a. s1.intersect(s2)
b. s1.join(s2)
c. s1.retainAll(s2)
d. s1.intersection(s2)

8. To empty a Collection or a Map, you use the __________ method.


a. empty
b. clear
c. zero
d. setEmpty

3|Page
SCOA021 MAY/JUNE EXAMINATION 2023

9. Suppose your program frequently tests whether a student is in a soccer team and also need to know
the student’s information such as phone number, address, and age, what is the best data structure to
store the students in a soccer team?
a. ArrayList
b. HashMap
c. TreeMap
d. LinkedList
e. HashSet

10. Analyze the following code:

public class Test {


public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("123", "John Smith");
map.put("111", "George Smith");
map.put("123", "Steve Yao");
map.put("222", "Steve Yao");
}
}
a. After all the four entries are added to the map, "123" is a key that corresponds to the value
"John Smith".
b. After all the four entries are added to the map, "123" is a key that corresponds to the value
"Steve Yao".
c. After all the four entries are added to the map, "Steve Yao" is a key that corresponds to the
value "222".
d. After all the four entries are added to the map, "John Smith" is a key that corresponds to the
value "123".
e. A runtime error occurs because two entries with the same key "123" are added to the map.

11. Analyzing algorithm efficiency is ________.


a. to measure their actual execution time
b. to estimate their execution time
c. to estimate their growth function
4|Page
SCOA021 MAY/JUNE EXAMINATION 2023

12. An input that results in the shortest execution time is called the _____________.
a. best-case input
b. worst-case input
c. average-case input

13. On an average, linear search searches


a. the whole list
b. half of the list
c. just one element in the list
d. one fourth of the list

14. For a sorted list of 1024 elements, a binary search takes at most _______ comparisons.
a. 11
b. 100
c. 512
d. 6

15. O(1) is ________.


a. constant time
b. logarithmic time
c. linear time
d. log-linear time

16. The time complexity for the Towers of Honoi algorithm in the text is ________.
a. O(n)
b. O(n^2)
c. O(n^3)
d. O(2^n)

17. The time complexity for the selection sort algorithm in the text is ________.
a. O(nlogn)
b. O(n^2)
c. O(logn)
5|Page
SCOA021 MAY/JUNE EXAMINATION 2023

d. O(2^n)

18. ______________ approach is the process of solving subproblems, then combining the solutions of
the subproblems to obtain an overall solution. This naturally leads to a recursive solution. However, it
would be inefficient to use recursion, because the subproblems overlap. The key idea behind dynamic
programming is to solve each subproblem only once and store the results for subproblems for later use
to avoid redundant computing of the subproblems.
a. Divide-and-conquer
b. Dynamic programming
c. Brutal-force
d. Backtracking

19. The time complexity for the closest pair of points problem using divide-and-conquer is ________.
a. O(n)
b. O(nlogn)
c. O(logn)
d. O(2^n)

20. ______________ approach divides the problem into subproblems, solves the subproblems, then
combines the solutions of the subproblems to obtain the solution for the entire problem. Unlike the
________ approach, the subproblems in the divide-and-conquer approach don’t overlap. A subproblem
is like the original problem with a smaller size, so you can apply recursion to solve the problem.
a. Divide-and-conquer/dynamic programming
b. Dynamic programming/divide-and-conquer
c. Brutal-force/divide-and-conquer
d. Backtracking/dynamic programming

21. The best-time complexity for insertion sort is _____________.


a. O(1)
b. O(logn)
c. O(n)
d. O(nlogn)
e. O(n*n)

6|Page
SCOA021 MAY/JUNE EXAMINATION 2023

22. The worst-time complexity for insertion sort is _____________.


a. O(1)
b. O(logn)
c. O(n)
d. O(nlogn)
e. O(n*n)

23. The best-time complexity for bubble sort is _____________.


a. O(1)
b. O(logn)
c. O(n)
d. O(nlogn)
e. O(n*n)

24. The time to merge two sorted lists of size n is _________.


a. O(1)
b. O(logn)
c. O(n)
d. O(nlogn)
e. O(n*n)

25. The average-time complexity for merge sort is _________.


a. O(1)
b. O(logn)
c. O(n)
d. O(nlogn)
e. O(n*n)

26. Suppose you choose the first element as a pivot in the list {5 2 9 3 8 4 0 1 6 7}. Using the partition
algorithm in the book, what is the new list after the partition?
a. 5 2 9 3 8 4 0 1 6 7
b. 4 2 3 0 1 5 6 7 9 8
c. 4 2 1 3 0 5 8 9 6 7
d. 2 3 4 0 1 5 9 8 6 7
7|Page
SCOA021 MAY/JUNE EXAMINATION 2023

e. 2 3 4 0 1 5 6 7 8 9

27. The worst-time complexity for quick sort is _________.


a. O(1)
b. O(logn)
c. O(n)
d. O(nlogn)
e. O(n*n)

28. To add a new node, you need to start a process by first placing it as _______ and move it up to
maintain the heap property.
a. the new root
b. the last node in the heap
c. the left child of the root
d. the right child of the root

29. Suppose a heap is stored in an array list as follows: {100, 55, 92, 23, 33, 81}. The parent of 81 is
_______.
a. 100
b. 55
c. 92
d. 23
e. 33

30. Suppose a heap is stored in an array list as follows: {100, 55, 92, 23, 33, 81}. After inserting 103,
what is the content of the array list?
a. {100, 55, 92, 23, 33, 81, 103}
b. {100, 55, 103, 23, 33, 92, 81}
c. {103, 55, 92, 23, 33, 81, 92}
d. {103, 55, 100, 23, 33, 81, 92}
e. {103, 55, 92, 23, 33, 81, 100}

Question 2 [10]

8|Page
SCOA021 MAY/JUNE EXAMINATION 2023

2.1 What is the printout of the following code fragment? [5]

List<String> list = new ArrayList<>();


list.add("A");
list.add("B");
list.add("C");
list.add("D");
for (int i = 0; i < list.size(); i++)
System.out.print(list.remove(i));

2.2 Suppose a list contains {"red", "green", "red", "green"}. What is the list after the following
code? [5]

String element = "red";


for (int i = 0; i < list.size(); i++)
if (list.get(i).equals(element)) {
list.remove(element);
i--;
}

Question 3 [10]

3.1 Write a Java program that lets the user enter any five (5) unordered numbers from a graphical
user interface and displays them in a text area, Use a linked list to store the numbers. Do not
duplicate numbers. Add the buttons Sort and Reverse to sort and reverse the list. [10]

Section B [Compiler Theory] [50]

Question 1 [25]

Write down the correct answer for each of the following questions.

1. What is the name of the process that determines whether a string of tokens can be generated by
a grammar? [01]
A. Analysing
B. Recognizing
C. Translating
D. Parsing

9|Page
SCOA021 MAY/JUNE EXAMINATION 2023

2. The number of tokens in the following C statement is _____


printf("i = %d, &i = %x", i, &i); [01]
A. 3
B. 10
C. 21
D. 26

3. A top down parser generates __________ [01]


A. Rightmost Derivation

B. Right most derivation in reverse

C. Left most derivation


D. Left most derivation in reverse

4. In a syntax directed translation scheme, if value of an attribute of a node is a function


of the values of the attributes of its children, then it is called [1]

A. synthesized attribute
B. inherited attribute
C. canonical attribute
D. none of the above

5. In the context of compiler design, reduction in strength refers to: [01]


A. Code optimization obtained by the use of cheaper machine instructions
B. Reduction in accuracy of the output
C. Reduction in the range of values of inputs variables
D. Reduction in efficiency of the program

6. Dead code elimination in machine code optimization refers to: [01]


A. Removal of a labels
B. Removals of values that never get used
C. Removal of functions which are not involved
D. Removal of a module after its use

7. Peep-hole optimization is a form of _________ [01]


A. loop optimization
B. local optimization
10 | P a g e
SCOA021 MAY/JUNE EXAMINATION 2023

C. constant folding
D. none of these

8. Consider the following grammar G.

S→F⎪H
F→p⎪c
H→d⎪c

Where S, F and H are non-terminal symbols, p, d and c are terminal symbols. Which of the
following statement(s) is/are correct? [02]

S1: LL(1) can parse all strings that are generated using grammar G.
S2: LR(1) can parse all strings that are generated using grammar G.
A. Only S1
B. Only S2
C. Both S1 and S2

D. Neither S1 and S2

9. Write a regular expression for all strings that doesn't contain the substring 110
[02]
A. ((0|10)1)*
B. ((0|10)*1)
C. ((0|10)*1)*
D. None of the above

10. Write a regular definition for all strings of a’s and b’s with an even number of a’s and odd number
of b’s [02]
A. a*ba(bb)*

B. (aa)*b(bb)*

C. (aa)*b(bb)

D. (aa)b(bb)*

Given the following NFA, answer questions 52-55

11 | P a g e
SCOA021 MAY/JUNE EXAMINATION 2023

11. Find the e-closure(move (S0,a)) [03]


A. {1,2,3,4,5,6}
B. {1,2,3,4,5,6,7}
C. {1,2,3,4,5,6,7,8}
D. All of the above

12. Find the e-closure(move(S0,b)) [03]


A. {1,2,3,4,5,6}
B. {1,3,4}
C. {6}
D. All of the above

13. Find the e-closure(move (S1,a)) [03]


A. {1,2,3,4,5,6,7}
B. {1,2,3,4,5,6}
C. {1,2,3,4,5,6,7,8}
D. None of the above

14. Find the e-closure(move (S1,b)) [03]


A. {1,2,3,4,5,6,7}
B. {1,3,4}
C. {6}
D. None of the above

Question 2 [25]

2.1 What is the difference between the compiler and the Interpreter? [02]

2.2 What is the difference between a token and a lexeme? [02]

2.3 With aid of a diagram, list and explain all the phases of a complier. [08]

2.4 Minimize the DFA in Fig 1. Indicate all the groups and subgroups [04]

12 | P a g e
SCOA021 MAY/JUNE EXAMINATION 2023

Fig 1. DFA

2.5 Given the sematic rules in Fig 2, draw an annotated parse tree for the following expression.
[05]

(3+4) x (5+6)n

Fig 2. Semantic rules

2.6 Given Table 1, determine the costs of the following instruction sequences

2.6.1 LD R0, y [02]


LD R1, z
ADD R0, R0, R1
ST x, R0

2.6.2 LD R0, I [02]


MUL R0, R0, 8
LD R1, a(R0)
ST b, R1
13 | P a g e
SCOA021 MAY/JUNE EXAMINATION 2023

Table 1: address modes

14 | P a g e

You might also like