The exam will have 3 - 5 questions. There will be some programs similar to the projects. There will also be short answer questions. A few sample questions are given below, but the list is incomplete. You are responsible for the material covered in hte homework, lectures, and chapter 2 of the book.
Be able to clearly explain the differences between commands, expressions, and declarations.
What is a hybrid phrase? Give examples.
Here's an EBNF grammar for the Mod5 language:
<Exp> ::= <Literal> | <Sum>
<Literal> ::= 0 | 1 | 2 | 3 | 4
<Sum> ::= (+ <Exp> <Exp>)
Complete the following declaration:
class Exp {
Exp operand1, operand2; // = null if
this Exp is a literal
int literal; // ignore if this Exp is
not a literal
static int ERROR_TYPE = 0, INT_TYPE =
1;
static Exp parse(List tokens) throws
AppError { ??? }
int type() { ??? }
int exec() { ??? } // implements mod 5
addition
}
A rectangle has length and width, which can't change. Two rectangles are considered the same if they have the same area. Implement a Rectangle class in Java. Be sure to include all of the canonical methods.
A labeled rectangle extends a rectangle by adding a label field of type String. Two labeled rectangles are the same if they have the same area and label. Implement a LabeledRectangle class in Java. Be sure to include all of the canonical methods needed.
Overriding is also called Ad hoc polymorphism.
What is dynamic method invocation (also called dynamic dispatch)?
What is variant selection? Give an example.
Assume C1Type <: C2Type and C1 implements C1Type while C2 implements C2Type and overrides method m. Assume x, y: C1Type and z: C2Type.
x = new C1();
y = new C2();
z = new C2();
Be able to resolve x.m(), y.m(), z.m()
Assume the following declarations:
interface IC {
void foo(C x);
}
interface ISC extends IC {
void foo(SC x);
}
class C implements IC {
void foo(C x) { /* body 1 */ }
}
class SC extends C implements ISC {
void foo(C x) { /* body 2 */ }
void foo(SC x) { /* body 3 */ }
}
IC x = new C();
IC y = new SC();
ISC z = new SC();
Which method body is executed by each of these calls:
x.foo(x);
x.foo(z);
y.foo(x);
y.foo(z);
z.foo(x);
z.foo(z);
Assume Rectangle, Triangle, and Circle extend Shape. Assume each provides a draw() method. A graphical context maintains a list shapes and has a draw method that draws each shape in the list.
Implement Graphical Context in the data-driven style using polymorphism.
Redo the example in the control-driven style.
What are the advantages of data-driven over control-driven programming?
What is structural subtyping?
Assume the following definitions have been made:
[define Account AccountType (class
[define balance Number 0]
[define withdraw (map Void Number)
(procedure
((amt Number)) {assign balance (- balance amt)})]
[define deposit (map Void Number)
(procedure
((amt Number)) {assign balance (+ balance amt)})])]
[define savings (new Account)]
Draw an environment diagram that shows the environment during the call:
{savings.deposit 100}
Assume the following definitions have been made:
[define mkAccount accountType
(function ((bal Number))
(function
((Number msg))
{assign
bal (+ bal msg)}))]
[define checking (mkAccount 100)]
What is accountType?
Draw an environment diagram that shows the environment during the call:
(checking 25)
Write rules for the subtype relationship <: for non-mutable record types, map types, array types.
Give an example why the following rules are false:
T <: S
C<T> <: C<S>
T <: S
var<T> <: var<S>
Complete the implementation of:
class MapType {
Type domain, range;
boolean subType(Type t) {
// = true if this <: t
}
}
Check out the CS152 exam review for additional questions.