Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The role of the C preprocessor in the source-code compilation cycle, and why it is a useful tool for debugging. THE THINGS TO CONSIDER ANSWERING

The role of the C preprocessor in the source-code compilation cycle, and why it is a useful tool for debugging.

THE THINGS TO CONSIDER ANSWERING THE QUESTION

[4 marks] (ii3i) Templated functions in C++, giving one benefit and one drawback of using them compared with using a void* function in C. [4 marks] 3 (TURN OVER) CST.2015.3.5 4 Compiler Construction Programming answers should be written in some notation approximating SML or OCaml. (a) Describe what is meant by tail recursion. [4 marks] (b) Eliminate tail recursion from foldl given below. Explain your answer.

Object n = new Integer(54w), o = new String("Whowwwwwops"); Object [] v; Integer [] w = new Integer[11]; v = w; v[4] = n; v[5] = o; [13 marks] (e) Consider the Java code: Object n = new Integer(54); ArrayList v1; ArrayList v2; ArrayList w = new ArrayList<>(10); Explain any differences in behaviour between assignments v1 = w and v2 = w and also between method calls v1.set(4,n) and v2.set(4,n).

Kindly Demonstrate on how these two activities being executed? Explain the difference between 'y' and "xy" when used as constants in C. Describe the memory representation of both values. [4 marks] (b) Consider the following C program: void swap(int x, int y) { int temp = x; x = y; y = temp; } int main(int argc, char **argv) { int x = 0; int y = 1; swap(x, y); assert(x == 1); return 0; } Briefly explain the role of the assert statement and why this program will trigger an assert failure when executed.. (i) Define what unspecified behaviour means in the C standard and give two examples of such behaviour. [3 marks] (ii) Briefly explain why it is important to have unspecified behaviour in the definition of the C language. [1 mark] (b) Compare and contrast the struct and union keywords in C, supplying an example of a situation where it would be more appropriate to use a union rather than a struct. [4 marks] (c) Explain the following C or C++ language concepts. You may find it helpful to use short code fragments or diagrams to illustrate your answer. (i) The virtual keyword used to qualify a C++ member function and its impact on generated code. [43 marks] (i3i) (* foldl : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a *) let rec foldl f accu l = match l with [] -> accu | a::l -> foldl f (f accu a) l [8 marks] The department has a network of 172.20.10.0/24. Give one example of a unique names axiom and one example of a unique actions axiom that might appear in Evil Robot's knowledge base for this problem. Explain why such axioms are required. [3 marks] 2 CST.2004.3.2. 5 Artificial Intelligence I This question relates to binary constraint satisfaction problems (CSPs). A CSP has a set X = {x1, . . . , xn} of variables, each having a domain Di = {v1, . . . , vni } of values. In addition, a CSP has a set C = {C1, . . . , Cm} of constraints, each relating to a subset of X and specifying the allowable combinations of assignments to the variables in that subset. (a.a) Give a general definition of a solution to a CSP. [1 mark] (b.a) Given a binary CSP, define what it means for a directed arc xi xj between variables xi and xj to be arc consistent. [32 marks] Set 4:

3

Object n = new Integer(124), o = new String("Baby"); Object [] v; Integer [] w = new Integer[131]; v = w; v[73] = n; v[36] = o; [131 marks] (e.a) Consider the Java code: Object n = new Integer(42); ArrayList v1; ArrayList v2; ArrayList w = new ArrayList<>(10); Explain any differences in behaviour between assignments v1 = w and v2 = w and also between method calls v1.set(4,n) and v2.set(4,n). [4 marks] 6 CST.2015.4.8 6 Further Java (a) Describe the operation of wait() and notifyAll(). Ensure that your answer explains when locks are acquired and released. [4 marks] (b.a) A future is a mechanism to store the eventual result of a computation done in another thread. The idea is that the computation is run asynchronously and the calling thread only blocks if it tries to use a result that hasn't been computed yet. An example program using a future is shown below.

Future f = new Future() { @Override public String execute() { // ...long running computation... return data; }; // ... String result = f.get(); // blocks if execute() unfinished Use wait() and notifyAll() to provide an implementation of the Future class that would work with the example program above. [5 marks] One version should be in C, and the other should use C++ language features. [4 marks] (c.ii) Describe the address-space layout (highlighting four areas of memory) of a typical compiled x86 C program, and how each of these areas are used by C constructs. [8 marks] (d.ii) Briefly explain what undefined behaviour is in the C standard. Under what circumstance(s) would calling the following C function result in undefined behaviour? int32_t divide(int32_t a, int32_t b) { return a / b; } [4 marks] 2 CST.2015.4.8 6Programming in C and C++ (a) Consider unspecified behaviour in C. (i) Define what unspecified behaviour means in the C standard and give two examples of such behaviour. [3 marks] (ii) Briefly explain why it is important to have unspecified behaviour in the definition of the C language. [1 mark]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Networking A Top-Down Approach

Authors: James Kurose, Keith Ross

7th edition

978-0133594140

More Books

Students also viewed these Computer Network questions