cse2004y-

download cse2004y-

of 8

Transcript of cse2004y-

  • 7/28/2019 cse2004y-

    1/8

    UNIVERSITY OF MAURITIUS

    FACULTY OF ENGINEERING

    SECOND SEMESTER EXAMINATIONS

    MAY 2010

    PROGRAMMEBSc (Hons) Computer Science & Engineering

    BSc (Hons) Electronics and Computer ScienceBSc (Hons) Information and Communication Technologies

    MODULE NAME Programming Languages and Algorithms

    DATE Monday

    10 May 2010

    MODULE CODE CSE 2004Y(3)

    TIME 09.30 12.30 Hrs DURATION 3 Hours

    NO. OF

    QUESTIONS SET6

    NO. OF QUESTIONS

    TO BE ATTEMPTED5

    INSTRUCTIONS TO CANDIDATES

    There are 2 sections in this paper: Section A and Section B.

    Each section consists of 3 questions.

    Answer any five (5) questions.

    All questions carry equal marks.

  • 7/28/2019 cse2004y-

    2/8

    Page 1 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    SECTION A

    Question 1 (20 marks)

    (a) Give the BNF rule for an identifier in C++. [2 marks]

    (b) Given the following BNF rules that form part of the grammar of the C++language and the rule from Question 1(a) above, write the BNF rule for acondition in C++:

    Digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9Letter a | b | | z | A | B | | ZUnderscore _RelationalOperator > | < | >= |

  • 7/28/2019 cse2004y-

    3/8

    Page 2 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    Question 1 (Contd)

    (d) Name the type of relationship between the objects in each of the following cases:(i) The telephone directory stores the contact details of many people.

    (ii) The Ministry of Finance is part of the Mauritian Government.(iii) A Manager is also an employee.

    [3 marks]

    Question 2 (20 marks)

    (a) Refer to the following grammar G for mathematical operators +, -, *, /, %(modulo) and ** (exponentiation):

    Expr Expr + Term | Expr - Term | Term

    Term Term*Factor | Term/Factor| Term%Factor | Factor

    Factor Primary**Factor | PrimaryPrimary 0|..|9| ( Expr )

    (i) What can you say about the associativity of the operators * and **?[1 mark]

    (ii) Draw theparse tree for the mathematical expression 11 % 2 ** 2 + 4 * 3 - 5.[3 marks]

    (iii) Evaluate the expression given in Question 2(a)(ii). [1 mark]

    (b) (i) What is the main difference between an imperative programming languageand afunctional programming language? [1 mark]

    (ii) In lambda Calculus, given that: V. E1 E2 En means ( V. (E1 E2.En ))

    V1 V2 Vn . E means ( V1 ( V2 .... ( Vn. E)))What is the meaning of the lambda expression x y z. add x y z?

    [1 mark]

    (c) (i) Write a function, length, in Scheme that takes as input a list L and returnsthe number of elements in L. [4 marks]

    (ii) Write a function, merge, in Scheme that takes as input two lists, L1 and L2,and appends the contents of L2 to L1. [5 marks]

    /Contd next page

  • 7/28/2019 cse2004y-

    4/8

    Page 3 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    Question 2 (Contd)

    (iii) Using the functions in Question 2(c)(i) and Question 2(c)(ii), write afunction, merge_longer, in Scheme that takes as input two lists, L1 and L2.

    If L1 is longer than L2 the function appends L2 to L1. Otherwise, itappends L1 to L2. Outputs of sample calls to merge_longer are as givenbelow:

    > (merge_longer '(1 2 3 4) '(5 6))(1 2 3 4 5 6)

    > (merge_longer '(1 2) '(3 4 5 6))(3 4 5 6 1 2)

    [4 marks]

    Question 3 (20 marks)

    (a) (i) Differentiate between a statically-typed and a dynamically-typed language.Give one (1) example of each. [2 marks]

    (ii) Refer to the following code that represents part of a C++ program:

    void main() {1. int num1, num2, sum;2. float percent;3. cout >num1; // Assume the user inputs 35. cout >num2; // Assume the user inputs 57. sum = num1 + num2;8. percent = ((float) num1/num2) * 100;9. cout

  • 7/28/2019 cse2004y-

    5/8

    Page 4 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    Question 3 (Contd)

    (c) Write a Prolog program to represent the directed graph G1 given in figure 1. Youare required to define the edges representing the graph, and a predicate for the

    relation path between two nodes.

    Figure 1: Directed Graph G1

    [5 marks]

    (d) (i) Write a Prolog definition for a predicate app that appends the contents ofa list L2 to a list L1.

    Example: Query:?- app([1, 2, 3], [4, 5],Result).Result = [1, 2, 3, 4, 5].

    [4 marks]

    (ii) Using the predicate app in Question 3(d)(i), write a Prolog program,double_list, that takes as input a list and returns another list whereby thecontents of the given list are doubled.

    Example: Query: ?- double_list([2, 3, 5, 7], Result).Result = [4, 6, 10, 14]

    [5 marks]

  • 7/28/2019 cse2004y-

    6/8

    Page 5 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    SECTION B

    Question 4 (20 marks)

    (a) A sorting algorithm is in O(n2) in the worst case. It takes 5 seconds to sort 10,000

    records, i.e. n = 10,000.(i) What is the predicted time for the algorithm to sort 20,000 records?(ii) Suppose that you are presented with a machine that is 49 times as fast.

    How many records will you be able to process on the new machine in 5seconds? [2 + 2 marks]

    (b) Given the growth rate for an algorithm, in the worst case, is expressed as follows,determine the upper bound of the algorithm:T(n) = 2n3 + 18n2 + 7 [3 marks]

    (c) Solve the following recurrence relation using the substitution method:T(n) = 2T(n/2) + n, for n 2 and T(1) = 0

    [4 marks]

    (d) Sort the following array of integers:34 12 64 7 43 21

    using: (i) Selection sort(ii) Insertion sort

    Note: You are required to clearly illustrate each step.

    [3 + 3 marks]

    (e) Determine the worst case time complexity of the insertion sort algorithm, in terms ofthe number of comparisons.Note: You are required to show all your workings.

    [3 marks]

    Question 5 (20 marks)

    (a) Given the following array of characters:0 1 2 3 4 5 6

    S O R T I N G

    sort it using: (i) Quicksort, using the rightmost element as the pivot element(ii) Mergesort(iii) Straight radix sort, given the integer value of A is 1, that of B is 2,...

    Note: You are required to clearly illustrate each step.[4 + 4 + 4 marks]

    /Contd next page

  • 7/28/2019 cse2004y-

    7/8

    Page 6 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    Question 5 (Contd)

    (b) Determine the worst case time complexity, in terms of the number of comparisons,of each of the following algorithms:

    (i) Quicksort algorithm(ii) Straight radix sort algorithmNote: You are required to show all your workings.

    [3 + 3 marks]

    (c) Name one (1) method that can be used to improve the running time of thequicksort algorithm. [1 mark]

    (d) Briefly explain what is meant by a stable sort. [1 mark]

    Question 6 (20 marks)

    (a) Draw the Red-Black tree that results from inserting the alphabetic keysQ U E S T I O N I S E A S Y in that order into an initially empty Red-Black tree.

    [7 marks]

    (b) Using the alphabetic keys K E Y S, explain the concept of hashing as asearching method. [3 marks]

    (c) Refer to the directed graph G2 below:

    Figure 2: Directed Graph G2

    (i) Draw the adjacency list of G2.(ii) Draw the Depth-First-Search tree of G2, starting from node A.

    [3 + 3.5 marks]

    /Contd next page

  • 7/28/2019 cse2004y-

    8/8

    Page 7 of 7

    PROGRAMMING LANGUAGES & ALGORITHMS CSE 2004Y(3)

    Question 6 (Contd)

    (d) Consider the following undirected weighted graph G3:

    Figure 3: Undirected Weighted Graph G3

    Draw the minimum spanning tree (MST) of G3, using the Kruskals algorithmand find its cost.

    [3.5 marks]

    END OF QUESTION PAPER

    /ph