0% found this document useful (0 votes)
7 views

IT 340-Programming Language Design Concepts7

This document contains an examination paper for a programming language design concepts course. It has four questions covering topics like BNF grammars, Scheme programming, Prolog, and Python. Question 1 deals with structured programming, lex/yacc, BNF grammars and parse trees. Question 2 is on Scheme programming concepts like functional paradigm, mathematical expressions, functions, and lists. Question 3 covers Prolog topics such as backward chaining, rules, functions on lists. Question 4 has questions on Python concepts like tuples, expressions, functions, and classes.

Uploaded by

jathurshanm3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

IT 340-Programming Language Design Concepts7

This document contains an examination paper for a programming language design concepts course. It has four questions covering topics like BNF grammars, Scheme programming, Prolog, and Python. Question 1 deals with structured programming, lex/yacc, BNF grammars and parse trees. Question 2 is on Scheme programming concepts like functional paradigm, mathematical expressions, functions, and lists. Question 3 covers Prolog topics such as backward chaining, rules, functions on lists. Question 4 has questions on Python concepts like tuples, expressions, functions, and classes.

Uploaded by

jathurshanm3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

/

Sri Lanka Institute of Information Technology

B. Sc. Special Honours Degree in


Information Technology
Final Examination
Year 3, Semester 1 (20 18)

IT340 - Programming Language Design Concepts

Duration: 3 Hours

Instruction to Candidates:
• There are four questions available.
• Total Marks 100.
• This paper c\mtains 7 pages with Cover Page.
• This is a close book examination.
Question 1 (25 marks)

a) Briefly explain the structured program principle and an example where it get violated in
FORTRAN.
(3 marks)

b) Write a simple lex & yacc program to develop a syntax checker to identify a simple
java program as given below.
class Exercise{
public static void main(String args[]) {
int num = 34;
} '

(10 Marks)

c) Consider the BNF grammar to define some arithmetic expressions and write the answer
for parts given below.

<expr> : := <factor> <opr> <expr> I <factor>


<factor> : := x I y I z I <parexp>
<parexp> : := (<expr>)
<opr> : : = +I-

i) Draw syntax graph for the following BNF grammar rules. (2 marks)

a. <expr> : := <factor> <opr> <expr> I <factor>


b. <parexp> : : = ( <expr>)

ii) Show a parse tree and leftmost derivation for the following statement.
(6 marks)
x +(z-y)

d) Write the E'BNF grammar to the following BNF grammar below.


i) <blk> - begin <stmt> end
<stmt> : := <cmd> I <cmd> ; <stmt>

ii) <sn> : := +<num> -<num> I <num>


(4 marks)

Page 2 of7
Question 2 (30 marks)

a) Compare and contrast the procedural paradigm and the functional paradigm.
(3 marks)

b) Convert the following mathematical expression to scheme. (3 marks)


8 * 2 ** 4 I 6 + 10 + 2

c) Write a f).mction to determine if the person is an adult or not when the age is given.
(3 marks)
Example: (adult? 3 5) Should return T

d) Using the scheme function definition given below answer the questions.

(define (fun lst)

COND
((NULL? lst) ' ())
(ELSE (CONS (fun (CDR lst ) ) ( CAR lst ) )

) )

i) What does this Scheme function do? Explain briefly. (3 marks)

ii) What will be the output when the function is executed with
\
(fun (32 19 45 90)) (2 marks)

e) Write a scheme function to find the last element in the list and return the square value
of the last element.
Example:last (3 10 78 5) shouldreturn25 (4marks)

Page 3 of7
f) Write a scheme function to create a list containing integers when the lower bound and the
upper bound is given.

Example: (build ( 3 8) ) should return ( 3 4 5 6 7 8) (6 marks)

g) Write a scheme function to eliminate consecutive duplicates of list elements.


Hint: you can use eq in build function in scheme to compare two elements are equal or
not.
Example: (compress 1 (a a a a b c c a a d e e e e)) should return
1
(a b c a d e)
(6 marks)

Page 4 of7
Question 3 (30 marks)

a) Briefly describe the forward chaining in a Prolog Program. (3 marks)

b) Consider the following rules and facts. Find the goal "loves(Who, What)." using
backward chaining.
Facts:
Fl: cat(fubby).
F2: black_spots(fubby).
F3: dog(figaro).
F4: white_spots(figaro).
F5: black_spots(toby).

Rules:
Rl: owns(mary, Pet):- cat(Pet), black_spots(Pet).
R2: loves(Who, What):-owns(Who, What).
(4 marks)

c) Write a prolog function to return the element before the last element ofthe list.
Example: last_but_one (X, [26, 12, 10, 88, 67] )X should return 88
(4 marks)
d) Write a prolog function to rotate elements to the left in the list.
Example: rotatelist ([10,22,65,50],R) Rshouldreturn
[22, 65, 50, 10]
(5 marks)

e) Write a prolog function to eliminate consecutive duplicate elements in the list.


Example: remove Dup ( [ 1, 1, 1, 5, 5, 5, 10, 4, 4] , S) S Should return
[1,5,10,4]
(5 marks)
f) Write a prclog function to find the average of the elements in the list.
Example: average(A,[l,3,9,20,7]) A should return 8
(5 marks)

Page 5 of7
g) Given the following knowledge base and find the output of the following queries.
female (amy) .
female(johnette).
male(anthony).
male(bruce).
male(ogden).
parentof(amy,johnette).
parentof(amy,anthony).
parent?f(amy,bruce).
parentof(ogden,johnette).
parentof(ogden,anthony).
parentof(ogden,bruce).
siblingof(X,Y) :- parentof(Z,X),parentof(Z,Y),X \=Y.
brotherof(X,Y) :- parentof(Z,X) ,male(X),parentof(Z,Y),X \=Y.

(4 marks)
1. ?-female (Amy).
2. ?- brotherof (anthony, X).
3. ?- paren tof (ogden, j ohnet te) .
4. ?-siblingof(Y,johnette).

Page 6 of7
Question 4 (15 marks)

a) Consider the tuple Tup = ("Kandy", 20.5, 87,"10.7") in python and provide answers for
the following questions.
(2 marks)
i) Write a statement to print all the elements starting from the second element.
ii) Write a statement to modify the value 87 so it's value is increased by two.

b) Find the output generated by the Python interpreter for the following expressions.
(3 marks)

i) -11 % 3
ii) 35 I 2 * 3 % 3
iii) 167 II 4

c) Write a python function called sum to calculate the sum when the list is passed as a
parameter. Call the function and display the result. (5 marks)

d) Write a python class called Rectangle which has a function called "getArea". The getArea
function should accept width and length as parameters and return the area of the rectangle.
Create an object from the Rectangle class and call the "getArea" method.
(5 marks)

***End of Exam Paper***

Page7of7

You might also like