0% found this document useful (0 votes)
58 views4 pages

6com1045 Coursework Test Questions

This document contains 13 multiple choice questions about programming language concepts. It covers topics like the von Neumann architecture, concrete and abstract syntax, imperative features, encapsulation, concurrency vs parallelism, and functional programming concepts like anonymous functions, higher-order functions, polymorphism and tail recursion. It also discusses language evolution in Java and solving threading issues using synchronization and software transaction memory.

Uploaded by

AliH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

6com1045 Coursework Test Questions

This document contains 13 multiple choice questions about programming language concepts. It covers topics like the von Neumann architecture, concrete and abstract syntax, imperative features, encapsulation, concurrency vs parallelism, and functional programming concepts like anonymous functions, higher-order functions, polymorphism and tail recursion. It also discusses language evolution in Java and solving threading issues using synchronization and software transaction memory.

Uploaded by

AliH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Section A

Answer ALL questions in this section.

1. Describe the von Neumann architecture and the fetch-execute cycle. (5 marks)

2. The concrete syntax of a statement is the text of the statement in a particular programming
language. Show the concrete syntax of an assignment statement in two different programming
languages, e.g. in BASIC and in Java. Which parts are similar and which are different? (6 marks)

3. Name some features typical of imperative programming. Name one language which supports
these features. (5 marks)

4. An abstract data type allows a programmer to encapsulate fields and methods together. Write an
example in either Java or Ruby which illustrates encapsulation of a field and a method. (6 marks)

5. What is the difference between concurrency and parallelism? (4 marks)

6. Java version 7 introduced the diamond operator for type inference. Give an example of how the
diamond operator is used, and explain what advantages it provides the programmer. (6 marks)

7. The following Scala program adds up all the numbers from 1 to n:

def sumN (n:Int):Int = {


if (n == 0) {
return 0
} else {
return n + sumN (n-1)
}
}

Rewrite the program using match. (4 marks)

8. Using an example, explain the idea of precedence. (4 marks)


Section B

Every question in this section is worth 20 marks. Answer any THREE.

9. The following BASIC program adds up all the numbers from 0 to a number given in N:

1010 LET N = 10
1020 LET I = 0
1030 LET SUM = 0
1040 LOOP:
1050 LET SUM = SUM + I
1060 LET I = I + 1
1070 IF I < N THEN GOTO LOOP
1080 PRINT SUM

a. Rewrite the program in Java or Ruby. (6 marks)


b. Describe at least four syntactic differences in language evident between your solution and
that in BASIC. (8 marks)
c. What advantages or disadvantages are there in more advanced language constructs over
more primitive ones? For example, a while loop compared with the use of IF and GOTO in
BASIC. (6 marks)

Total: 20 marks

10. The Java language has evolved considerably since its first release in 1996.

a. Pick one of the following language features: interfaces, iteration, or switch statements and
describe how that construct has evolved through Java’s development. You should provide code
examples from an early version, and later versions to show how the changes. Explain what
advantages the changes provide for the programmer. (12 marks)

b. Early versions of Java did not support meta-programming (through annotations and reflection),
lambda expressions, or generics. Pick one of these aspects of the Java language, explain how it
works and how it improves the experience of programming in Java. (8 marks)

Total: 20 marks
11. Explain what is meant by the syntax and semantics of a programming language expression such
as 3+4*5. (6 marks)

You are given the following syntax for expressions:

expr = constant numeral


| neg expr
| sum expr expr
| diff expr expr
| prod expr expr
| quot expr expr

Where "constant" refers to numbers such as 1, 2, 3…; "neg" to negate; "sum" to plus (+); "diff" to
subtract (-); "prod" to multiply (*); and "quot" to division (/). (You can assume "numerals" have the
usual definition as sequences of digits.)

Assume a denotational semantics for expressions, as follows:

meaning (constant n) = numeral n 1


meaning (neg e) = - (meaning e) 2
meaning (sum(e1, e2)) = meaning e1 + meaning e2
meaning (diff(e1, e2)) = meaning e1 - meaning e2
meaning (prod(e1, e2)) = meaning e1 * meaning e2 // writing * for multiplication
meaning (quot(e1, e2)) = meaning e1 / meaning e2

You are asked to add a new operation to the expression definition, triple x (which returns the value
of 3*x).

a. Add a line to the syntax definition of “expr” for the triple operation (2 marks)
b. Give one or more rules to define the denotational semantics of the triple operation by
extending the definition of “meaning” (2 marks)
c. Show an example trace of the semantics using the expression “triple (2+1)”. You will first
have to write the expression using the syntax definitions, and then use the definition of
“meaning”. (10 marks)

Total: 20 marks
12. Functional programming allows the programmer to use functions as first-class values. Give
examples from any language of:

a. Creating an anonymous function, and assigning it as a value for a variable (2 marks)

b. Passing a function as an argument to another function (e.g. passing ‘double’ to a ‘map’


function to double every element in a list) (2 marks)

c. Calling one function and getting a function back as a result (2 marks)

The following function counts the number of items in a list of Int values:

def lstCount (lst:List[Int]):Int = {


lst match {
case Nil => 0
case _ => 1 + lengthInt (lst.tail)
}
}

d. Explain what is meant by a polymorphic function and rewrite the lstCount function so that it
can be used with lists of any type. (6 marks)

e. The function lstCount above is not tail recursive. Explain what is meant by tail recursion, and
rewrite the function to be tail recursive. Why might this be more efficient? (8 marks)

Total: 20 marks

13. In the following Java program, explain the problem that the synchronized keyword is solving
and how it solves it.

public int changePrice (int multipler) {


int result;
synchronized(this) {
int p = getPrice ();
int newPrice = p * multipler;
setPrice (newPrice);
result = getPrice() * 2;
}
return result;
}
(6 marks)

Clojure provides a “software transaction memory” approach to solving the same problem. With the
help of a Clojure example, explain in your own words how “software transaction memory” works,
and in particular include an explanation of “optimistic concurrency”. (14 marks)

Total: 20 marks

You might also like