The test is open-everything with the sole limitation that you
neither solicit nor give help while the exam is in progress.
This is a take-home quiz. From the time you first look at it, you
have 12 hours to complete it. Print out the exam and submit all answers
on the printed sheets only. Sign the affadavit at the
bottom of this page, or else you will get a zero.
| Problem | You got | Out of |
| 1 | | 10 |
| 2 | | 20 |
| 3 | | 10 |
| 4 | | 10 |
| 5 | | 20 |
| 6 | | 10 |
| 7 | | 10 |
| 8 | | 10 |
| TOTAL | | 100 |
I have not worked on this exam past the twelve-hour period allowed.
Date
- Here is a description
of a language. Programs in this
language are made up of a non-empty sequence of function
declarations, followed by a
single expression. Each function declaration starts with the
keyword fun followed by the function's name
(an identifier), then a parenthesized list of zero
or more parameters (also identifiers) separated by commas,
then the body, which is a sequence of one or more expressions
terminated by semicolons with the sequence enclosed
in curly braces.
Expressions can be numeric literals, string literals,
identifiers, function calls, or can be made up of
other expressions with the usual binary arithmetic
operators (plus, minus, times, divide) and a unary
prefix negation and a unary postfix factorial ("!").
There's a conditional expression with the weird
syntax "x if y else z".
Factorial has the highest precedence, followed by negation,
the multiplicative operators, the additive operators,
and finally the conditional. Parentheses
are used, as in most other languages, to group subexpressions.
Numeric literals are non-empty sequences of decimal
digits with an optional fractional part and an optional
exponent part.
String literals are doublequote-enclosed sequences of non-control characters
with the backslash used to escape the double quote character and the
backslash itself.
Identifiers are those non-empty sequences of letters, decimal
digits, underscores, at-signs, and dollar signs, beginning
with a letter or dollar sign, that are not also reserved
words. Function calls are as in C, with the arguments
in a comma-separated list of expressions bracketed by
parentheses. There are no comments in this language, and
whitespace can be used liberally between tokens.
Write an example program in this language that
shows off everything described above.
- For the language
in the previous problem, write both the microsyntax
and the macrosyntax, using the notation I used in my
notes on syntax.
- Classify each of
the following Java fragments as (a) a lexical
error, (b) a syntax error, (c) a static semantic error,
(d) a dynamic semantic error, or (e) no error at all.
- class C {int x; void f(int x) {}}
- class C {void f(int x) {int x = 1;}}
- class C {String s = "Hawai`i";}
- class C {int x[] = new int[3]; System.out.println(x[10];)}
- class C {void f() {throw new IllegalStateException();}}
or else, answer the above question for the following
fragments of C90:
- {int x; int x;}
- {int x; {int x;}}
- {int x; struct x {};}
- void f() {return 5;}
- x +. 2;
If you answer both the Java and C halves of this question,
your score for this problem will be the worst of the two halves.
- Give the abstract
syntax tree for the following fragment of, well,
it doesn't matter whether you say this is C or Java.
while (true) x . last[y*3>>2] >>= y^w * f(q |- w);
- Write regular expressions for
- A number from the set {82,86,87,59,57,871}.
- Strings that start with an a, end with one or more b's,
which contain, in between, characters that are neither
letters nor spaces.
- The set of all strings over
{a,b,c} that do not contain two consecutive a's anywhere
in the string.
- The set of all strings of any length that either contain
exactly 10 digits (in '0'..'9') or contain exactly 11
digits (where the first one is a '1'). The following
strings are examples: "13jef784y/9903:*88}",
"2222222222", "000.009.2111", and 1(808)112-4444xx".
- Write a fragment
of Ruby code that, after it is executed, we
now have a new method called stutter
that works on arrays as follows:
[1,4,3].stutter returns [1,1,4,4,3,3]
- Write a Ruby class called
Account, representing a kind of bank
account with an account number that never changes once
constructed, and an account balance which can be read
and written. All writes cause, as a side effect, a message
to be written to standard output. Also all writes
that would make the balance negative are prohibited.
Example:
a = Account.new(14)
a.id returns 14
a.balance returns 0
a.balance = 100 prints "depsited 100 to account 14", returns 100
a.balance = 20 prints "withdrew 80 from account 14", returns 20
a.balance = -20 prints "overdrafts not allowed", returns -20
a.balance returns 20
- Consider this script:
var a = [];
for (var i = 0; i < 10; i++) {
a[i] = function() {return i;}
}
var b = [];
for (var j = 0; j < 10; j++) {
b[j] = a[j]();
}
At the end of this script, what is the value of b? Explain in detail
why this is. A skecth will be very helpful and improve your chances of
getting full credit.