We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
THE UNIVERSITY OF ZAMBIA
School of Natural Science
Department of Computer Science
Final Exam
CSC 3301: Programming Language
Principles and Paradigms
Date: Wednesday, 14" June 2023
Time: 14:00hrs — 17:00hrs
Duration: 3 Hours
Instructions
1. This paper has a total of five (5) questions
2. You should answer any four (4)
3. Each question carries 25 Marks.
4. No other exam aids are allowed apart from pens and papersQuestion 1
A. Why are there so many programming languages? [2 Marks}
8. What are the four main programming language paradigms and how
Write code to illustrate your answer. [12 Marks]
List and explain at least three main differences between a compiled |:
interpreted language? [9 Marks]
D. What are the advantages in using an intermediate machine for the impler
language; give an example of such a language? [2 Marks]
Question 2
‘A. What is the difference between the lexical struc
illustrate with code examples? [2.5 Marks}
B. Whats the difference between a regular expres
cone is used to specify lexical structure and which one is used to specify syntact
of a program? [5 Marks}
C. Given the following grammar := + | ::= ? class
[10 marks}
are they different?
language and an
mentation of a
ture and syntactic structure of a program;
sion and a context free grammar? Which
tical structure
> * | ()| a] BI ¢
public class Marriage {
private string couple;
public void saySomething(X
idoTdo():
y
publie void sayTdordo(){
Page 21Question 3
A. Many languages make distinctions between the storage
can be based on the type of the object, where its corresponding variable is declared, or
manually specified by a programmer. Common storage duration classes (using C++
ind dynamic. Briefly explain what these
duration of different objects. This
terminology) include static, automatic, thread-local, a
mean. [8 Marks)
B. To avoid the prevalence of memory errors in languages that rely
memory, some languages provide automatic memory management in the form of garbage
collection
‘a, What is garbage collection? (1 Mark]
b. What are the two major schemes use
Consider the following Python piece of code and answer the questions that f
b = object ()
a = None
return
How many objects are created by this function? [1 Marks]
How many references to a and b exist at line 4? [1 Marks]
How many references to a and b exist at line 5? [1 Marks]
‘on programmers to manage
.d for garbage collection? [2 Marks]
follow
oneune
What is Reference counting? [1 Marks]
Explain one advantage and one disadvantage of Reference counting. [2 Marks]
Discuss three disadvantages of garbage collector Finalizers compared to destructors
[3 Marks}
D. Consider the following C++ piece of code and answer the questions that follow?
int x = 3;
lint &y = x:
cout << &x << endl;
lcout << ky << endl;
iy es,
cout << <¢end1;|
doGeone
a. Whats printed at lines 4 and 5? (1 Mark]
b. What is printed at line 7? [1 Mark]
Programming languages differ in how they store variables. Explain the difference
between value semantics and reference semantics. (3 Marks)
Question 4
For questions A-C, assume that you're writing code from the Scheme Interpreter. Your code must be
syntactically correct.
‘A. Consider the following piece of Scheme code and answer the questions that follow
Page 3/1wmypo> i 3"
Beal > '(- 3)
3 > (- 3)
a. Whatis printed at line 1? [1 Mark]
b. Whats printed at line 2? [1 Mark]
What is printed at line 3? [1 Mark]
The following isa list in Scheme (list 1 23.4). Wri
rite a function in Scheme called fetch which when given alist of num
returns the number at the given index in the list [5 Marks]
D. Define a lst called mylst consisting of 8 numbers of your choice: Call function fetch with
mylist as an argument and lett return the fith number in your list. [5 Marks)
for variadic functions.
te this list as nested pairs. [2 Marks]
ers and an index,
E. Programming languages have different implementation:
'a.. Write a variadic function in Scheme that returns the average of its argume!
Marks}
b. Write a variadic function in Scheme that tak
names of a child, and returns the full name of each chil
instance, if a certain Mr. Tembo has the following childr
jevite, and Waza de Measles; given any of these
alist. [5 Marks)
nts. [5
cs in the last name of a father and
id as one list of all names. For
ren: Margaret Mwachiyeya,
Jessica Kazionele, Jonah de Nin‘
children, your function should return the full name of the child as
Question 5
‘Assume you're writing code from the Scheme interpreter
‘A. Write code in Scheme that
‘a. Defines a pair y of two numbers 5 and 6 [2 Marks]
b. Creates another variable z pointing to y [2 Marks]
c. Checks ify and z are equivalent [2 Marks}
d._ Sets the first number in y to 10 [1 Mark]
fe. Prints out the second number in [1 Mark]
Define a function in Scheme that takes ina list of tems and returns the length of the items
[5 Marks}
Functional programming has a few common computational patterns that can be used with
lambda functions and applied to sequences, llustrate with code how each of the following
works
‘a, Map [4 Marks]
b. Reduce [4 Marks]
Filter [4 Marks]
Page 4|i