Vous êtes sur la page 1sur 10

Brock University

Final Examination: Winter 2016 Number of Pages: 10


Course: COSC 2P05 Number of Students: 63
Date of Examination: Apr. 11, 2016 Number of Hours: 3
Time of Examination: 7:00 p.m. Instructor: D. Hughes

Instructions:
1) Write all answers on this examination paper.
2) Write your name and student number on this page.
3) Attempt question 1 and any 6 of questions 2 through 8.
4) The paper totals 70 marks.
5) No examination aids, specifically no electronic devices such as calculators,
cell phones, electronic dictionaries; or paper dictionaries are permitted. Use
or possession of unauthorized materials will automatically result in an award
of zero for this examination (this regulation does not preclude special
arrangements being made for students with disabilities). (FHB 5.1.2.A).
6) A mark of 40% must be achieved on this examination in order to obtain a
passing grade in the course.

Name: Student Number:

Quest ion Tot al Mark


1 10

2 10

3 10

4 10

5 10

6 10

7 10

8 10

Tot al 70
COSC 2P05 Apr. 11, 2016 Page 2 of 10

[5x2] 1. In one or two sentences each, define or explain five of the following terms or concepts as
they pertain to the principles of programming languages.

a. orthogonality

b. attribute grammar

c. binding

d. associative array

e. referential transparency

f. encapsulation

g. dynamic dispatch
COSC 2P05 Apr. 11, 2016 Page 3 of 10

Do any 6 of questions 2 through 8


[4x2] 2a. There are four categories of arrays based on subscript bindings: static array, fixed stack-
dynamic array, fixed heap-dynamic array, heap-dynamic array. Define and give one
advantage of each.

[2] 2b. What is the difference between a free union and a discriminated union? What is the effect
on type safety of having free unions in the language (a la C & C++)?
COSC 2P05 Apr. 11, 2016 Page 4 of 10

[2] 3a. What is the difference between a pointer type and a reference type?

[2] 3b. Why are languages that use reference types generally more type-safe than those that use
pointers?

[2] 3c. What is a dangling pointer?

[2] 3d. How does a dangling pointer come about?

[2] 3e. Why do dangling pointers occur in C++ but not in Java?
COSC 2P05 Apr. 11, 2016 Page 5 of 10

4. In the following code written in C-like syntax, what is the value of x after the call to the
subroutine f
static int x, z;
void f ( int y ) {
x = 0;
if ( y > z ) y = 1;
else y = 3;
};
void main ( ) {
z = 1;
x = 2;
f(x);
};

[2] 4a. If pass-by-value is used.

[2] 4b. If pass-by-reference is used.

[2] 4c. If pass-by-value/result is used.

In the following code written in C-like syntax, what is printed


void s ( ) {
int x = 1;
void t ( ) {
print(x);
};
void u ( ) {
int x = 2;
v(t);
};
void v ( sub ) {
int x = 3;
sub();
};
u();
};

[2] 4d. If shallow binding of referencing environments is used?

[2] 4e. If deep binding of referencing environments is used?


COSC 2P05 Apr. 11, 2016 Page 6 of 10

[2] 5a. What is the difference between the static link (static chain) and the dynamic link
(dynamic chain) in the activation record (ARI) stack?

[6] b. Given the following subroutine written in C-like syntax,


void s ( ) {
int w = 1;
void t ( int i ) {
int x = 2;
1 :
}; // t
void u () {
int y = 3;
void v ( int j ) {
t(j);
}; // v
v(y);
}; // u
u();
}; // s

draw the layout of the ARI stack at the point marked 1 above. Draw the links as arrows
and divide the ARIs with a bold line. Label the ARI on the left with the routine name.
routine use link var

s local 1 w

[2] c. Draw the path for a reference (at point 1 above) to the variable w.
COSC 2P05 Apr. 11, 2016 Page 7 of 10

[2] 6a. What is a thread?

[2] 6b. What is the difference between a lightweight task and a heavyweight task?

[3] 6c. How is cooperation synchronization achieved with message passing as in Ada?

[3] 6.d How competition synchronization achieved with monitors as in Java?


COSC 2P05 Apr. 11, 2016 Page 8 of 10

[2] 7a. APL is unusual in that operators all have the same level of precedence. Why do you think
that decision was made in the design of the language?

There is a well-known undetectable error in C evidenced in the following:

if ( x = 0 ) {
y = 2;
}
else {
y = 3;
}

[2] 7b. Describe the unexpected result in executing this statement and why it happens

[2] 7c. Why is this same error detectable in Java?

[2] 7d. What is a dangling else?

[2] 7.e Describe one way the ambiguity in the dangling else is resolved.
COSC 2P05 Apr. 11, 2016 Page 9 of 10

[5] 8a. With the following function definition in Haskell:

question :: [Int] -> Int


question [] = 0
question (x:xs) = x + question xs

What is the output of the following function invocation? (Show intermediate steps).

question [2,5,1]
COSC 2P05 Apr. 11, 2016 Page 10 of 10

[5] 8b. Consider a prolog fact parent(A, B), meaning A is the parent of B. It might be used as
follows:

parent(anne, bob).
parent(alex, bob).
parent(ann, bruce).
parent(ann, brenda).
parent(bob, carl).
parent(bob, christie).
parent(brenda, chris).
etc.

Write a Prolog rule that could be used to determine the cousin family relation, using only the
parent/2 facts, as well as the built-in predicate \= for determining when 2 terms cannot unify (be
equal). For example, cat \= dog is true, while dog \= dog is false.
(Hint: For full marks, you will need to use \=.)

Vous aimerez peut-être aussi