Docsity

Midterm Exam 2004 | Programming Language | CSC 4101, Exams of Programming Languages

Partial preview of the text

Download Midterm Exam 2004 | Programming Language | CSC 4101 and more Exams Programming Languages in PDF only on Docsity!

Midterm

CSC 4101, Fall 2004

11 October 2004

Read the whole exam first (there are a total of 6 pages) and plan your time. You have 50 minutes to complete all the questions. There are a total of 100 points. The exam is open book, open notes, and closed neighbors. Good luck!

Name:

Show the result of evaluating each of the following Scheme expressions:

(a) (car (car โ€™((a) (b) (c)))) (b) (cdr (cdr โ€™((a) (b) (c)))) (c) (cons (cons โ€™(a) โ€™(b)) โ€™(c)) (d) (car โ€™(car โ€™((a) (b) (c)))) (e) (car (cons (car โ€™((a) (b) (c))) โ€™(d))) (f) (cdr (cons โ€™((a) (b)) (cons โ€™(c) โ€™(d)))) (g) (car (cdr (car (cdr โ€™(((a) (b)) ((c) (d))))))) (h) (car (car (cons โ€™(quote ((a) (b))) โ€™((c) (d)))))

Hint: remember that (car (cons a d)) = a and (cdr (cons a d)) = d.

Consider the following grammar for variable and class declarations in Java:

-> |

-> SEM

-> CLASS ID LBRACE RBRACE

-> |

-> ID | ID ASSIGN | COMMA ID | COMMA ID ASSIGN

Indicate any problems in this grammar that prevent it from being parsed by a recursive-descent parser with one token lookahead. You can simply circle the offending parts of the grammar above.

Transform the rules for and so they can be parsed by a recursive-descent parser with one token lookahead (you donโ€™t need to transform the rules for ). I.e., remove any left-recursion and left-factor the grammar. Make as few changes to the grammar as possible, but do not use Extended BNF. The nonterminals and of the modified grammar should describe the same language as the original nonterminals.

Given the following C++ class declarations:

class B { private: int x; public: virtual int foo() { return this->bar(); } virtual int bar() { return 1; } };

class C { private: int x; public: virtual int foo() { return 2; } };

class D : public B, public C { private: int y; public: virtual int foo() { return B::foo(); } };

class E : public D { private: int z; public: virtual int bar() { return 3; } };

Show the layout of objects of class E and the contents of the virtual function tables (you can ignore the this-offsets). What are the values assigned to variables i, j, k, and l when the following code is executed?

B * p = new D(); C * q = new D(); B * r = new E(); C * s = new E(); int i = p->bar(); int j = q->foo(); int k = r->bar(); int l = s->foo();