






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to the midterm exam of the programming language technologies and paradigms course (cmsc 433) held in spring 2004. Topics such as white box and black box testing, abstract classes, subtype polymorphism, and parameteric polymorphism. It also includes java code examples and exercises.
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Programming Language Technologies and Paradigms Spring 2004
March 18, 2004
This exam contains 11 pages, including this one. Make sure you have all the pages. Write your name on the top of this page before starting the exam. The last page contains a cheat sheet of methods for LinkedLists and Iterators. Write your answers on the exam sheets. If you finish at least 15 minutes early, bring your exam to the front when you are finished; otherwise, wait until the end of the exam to turn it in. Please be as quiet as possible. If you have a question, raise your hand. If you feel an exam question assumes something that is not written, write it down on your exam sheet. Barring some unforeseen error on the exam, however, you shouldn’t need to do this at all, so be careful when making assumptions. You may avail yourself of the punt rule. If you write down punt for any numbered or lettered part of a question, your will earn 1/5 of the points for that question (rounded down).
Question Score Max
Question 1. Short Answer (25 points).
a. (5 points) What is the difference between white box and black box testing? Explain.
Answer: White box testing takes the source code into account, using metrics such as statement, branch, and condition coverage to judge the level of completeness. Black box testing does not use the source code, but instead checks the behavior of the code compared to its specification.
b. (5 points) What is an abstract class? Give one example usage of abstract classes (possibly from the class projects or from Java’s libraries) and explain the benefits of using an abstract class in that case rather than using an interface. If you can’t remember a concrete example, make up a hypothetical example and describe it.
Answer: An abstract class is one in which some of the methods are declared but do not contain code. An example is the ServletFilter class from project 1. The benefit compared to interfaces is that subclasses get to reuse some code. If we had made ServletFilter an interface, it could not contain code, hence we couldn’t get reuse.
c. (5 points) In project 3, our abstract syntax tree accepted a visitor that implemented NodeVisitor. We wrote visitors that were subclasses of NodePrePostVisitor, and then our makeVisitor methods wrapped those to turn them into NodeVisitors:
class SomeRefactoring extends NodePrePostVisitor { public static NodeVisitor makeVisitor(...) { return new PrePostOrderVisitor(new SomeRefactoring(...)); } ... }
What design pattern (aside from Visitor and Factory) describes this idea—this wrapping a NodePrePostVisitor in PrePostOrderVisitor to be compatible with NodeVisitor? Explain.
Answer: This is an example of the Adapter pattern. We adapt the NodePrePostVisitor subclasses to meet the AST’s visitor interface. This is not an example of the decorator or proxy patterns, because NodePrePostVisitor does not have the same interface as NodeVisitor. It is also not an example of the template pattern, because the invariant part of the algorithm is in PrePostOrderVisitor, not in NodePrePostVisitor.
Question 2. UML Diagrams (15 points). This question is about understanding UML diagrams. The memento design pattern is used to allow a third party to checkpoint internal state without having direct access to it. Below is the structure of this pattern (copied from Gamma et al). Here the Originator has some internal state that is private. A third party can save or restore that state with createMemento and setMemento.
Originator setMemento(Memento m) createMemento() private state
Memento Memento(state) getState() private state
return new Memento(state)
state = m.getState()
Write Java code equivalent to the above structure diagram. That is, write Java code for the classes Originator and Memento. Your code must compile. The state field of the Originator should be a String. (Hint: If you understand the above diagram, this is an easy problem...don’t think it’s complicated.)
public class Originator { private String state;
public void setMemento(Memento m) { state = m.getState(); }
public Memento createMemento() { return new Memento(state); }
public class Memento {
private String state;
public Memento(String state) { this.state = state; }
public String getState() { return state; }
Grading note: I was very picky in grading this question, since the code was so simple.
Question 3. Visitor Pattern (30 points). Write a visitor class that traverses a ClassDef and verifies that
Your visitor should throw an IllegalInputProgram(String) exception if it detects an error; otherwise it should do nothing. Make sure you include support for nested compound statements.
For this question, we will use a simplified version of the AST from project 3, described below. The simplifications are (a) the root node is ClassDef (not SourceFile); (b) it doesn’t have as many kinds of nodes; (c) there is no type information; (c) methods have no parameters; and (d) instead of separate classes Arith- meticExpression, AssignExpression, and BinaryOpExpression, we just use the single class BinaryExpression. You can assume there is a NodeVisitor interface that has one (overloaded) visit method for each of the classes (but not for the Statement interface). Hint: You will probably want to use a LinkedList to maintain the sets of fields and variables currently in-scope.
(This table is repeated on the cheat sheet) Class Fields Description ClassDef String name The name of the class List fields A FieldDef list List methods A MethodDef list FieldDef String name The name of the field MethodDef String name The name of the method CompoundStatement body The body of the method CompoundStatement List statements A Statement list Statement (interface) An VariableDef, IdentifierExpression, or BinaryExpression VariableDef String name The name of the local variable Expression init The initializer of the variable (may be null) IdentifierExpression String id The name of the identifier BinaryExpression Expression lhs The left-hand side String op The operation (+, -, &&, =, +=, or -=) Expression rhs The right-hand side
/* Continue IdentifierCheckVisitor */ public void visit(VariableDef n) { if (vars.contains(n.name)) throw new IllegalInputProgram(n.name + " shadows"); n.init.accept(this); vars.addLast(n.name);
public void visit(CompoundStatement n) { int mark = vars.size(); for (Iterator i = n.statements; i.hasNext(); ) ((Statement) i.next()).accept(this); while (vars.size() != mark) vars.removeLast();
public void visit(IdentifierExpression n) { if (!vars.contains(n.id) && !fields.contains(n.id)) throw new IllegalInputProgram(n.id + " not in scope");
public void visit(BinaryExpression n) { lhs.accept(this); rhs.accept(this);
Question 4. Testing (30 points). Below is part of a version of the LinkedList class from Java 1.4.2. (We’ve simplified the code a bit.) As you can see, our version of LinkedList implements singly-linked lists with a dummy head element. In other words, the empty list contains one Entry node, and a list with one element contains two Entry nodes. We’ve given you the code for the indexOf(Object) method, which either returns the index of the first occurrence of the parameter, or -1 if the element isn’t in the list. Notice that we start our search with node header.next to skip over the dummy head element. Also notice the support for lists containing null.
public class LinkedList {
private Entry header = new Entry(null, null);
private static class Entry { Object element; Entry next; }
public int indexOf(Object o) { int index = 0; if (o==null) { for (Entry e = header.next; e != null; e = e.next) { if (e.element==null) return index; index++; } } else { for (Entry e = header.next; e != null; e = e.next) { if (o.equals(e.element)) return index; index++; } } return -1; } }
Here is the control-flow graph for the indexOf(Object) method.
index = 0
o==null
e=header.next
e != null
e.element==null
index++ return index
e = e.next
return -
T
T F
F T
e=header.next
e != null
o.equals(e.element)
return index index++
e = e.next
T
T F
F
F
This page intentionally left blank.
Cheat sheet: Useful classes and methods. You may use methods from these classes that we haven’t listed, if you like.
public class LinkedList implements List {
/* Constructs an empty list. */ public LinkedList();
/* Returns the number of elements in this list. */ int size();
/* Appends the specified element to the end of this list */ void addLast(Object o);
/* Removes and returns the last element from this list; throws NoSuchElementException if this list is empty. */ Object removeLast();
/* Returns true if this list contains the specified element at least once */ boolean contains(Object o);
/* Returns an iterator over the elements in this list in proper sequence. */ Iterator iterator(); }
public class Iterator {
/* Returns true if the iteration has more elements */ boolean hasNext();
/* Returns the next element in the iteration; throws NoSuchElementException if the iterator has no more elements */ Object next(); }
Repeat of table from question 3
Class Fields Description ClassDef String name The name of the class List fields A FieldDef list List methods A MethodDef list FieldDef String name The name of the field MethodDef String name The name of the method CompoundStatement body The body of the method CompoundStatement List statements A Statement list Statement (interface) An VariableDef, IdentifierExpression, or BinaryExpression VariableDef String name The name of the local variable Expression init The initializer of the variable (may be null) IdentifierExpression String id The name of the identifier BinaryExpression Expression lhs The left-hand side String op The operation (+, -, &&, =, +=, or -=) Expression rhs The right-hand side