You are on page 1of 12

04IN1011: Programming techniques and technologies (6 ECTS) INJE02: Programming (8 ECTS)

FinalDry run SS 2013


Universit at Koblenz-Landau, FB4 Prof. Dr. Ralf L ammel, Dr. Manfred Jackel 9 July 2013

Name, Vorname Matrikel-Nr. Email Studiengang ECTS Pr ufungsversuch

.......................@uni-koblenz.de 2 BSc Inf 2 BSc CV 2 ........................ 26 28 21 22 23

Hiermit best atige ich, dass ich zur Klausur angemeldet und zugelassen bin. Eine falsche Angabe wird als T auschungsversuch gewertet.

Unterschrift:
Aufgabe Punkte (0-2)

Korrekturabschnitt
In der 8 ECTS-Version werden 20 Punkte als 100 % angesetzt. Es werden die Antworten zu allen 10 Fragen gez ahlt. In der 6 ECTS-Version werden 16 Punkte als 100 % angesetzt. Es werden die Antworten zu 8 Fragen gez ahlt bei Maximierung der Gesamtpunktzahl.

1 2 3 4 5 6 7 8 9 10 Total
1

Exam Manual
1. If you have any questions regarding the following items, please ask them ahead of the nal. You can ask them during the nal or the re-sit as well, but this may be less helpful to you. 2. There are 10 exam assignments with 0-2 points each. 0 means missing or wrong; 1 means arguably appropriate, but signicantly incomplete or incorrect; 2 means appropriate and essentially complete and correct. You might get an extra point for each exam assignment, if you manage to come up with an exceptionally insightful solution. The exam lasts 1 hour. Thus, one can spend more than 5 min per exam assignment. 3. All exam assignments focus on program comprehension. That is, you need to understand and comment on some given code fragment. A 140 chars limit applies to each answer if text is required. Longer answers will receive a reduced score ( 0). In some cases, if any, you may need to write down some piece of code, grammar, XML, schema, SQL, or alike. 4. The overall topics for the exam are dened with the dry-run; see the section headers and the per-section footnotes for extra explanations. These topics are maintained for the actual nal and the re-sit of the given course edition. The topics may be dierent in the next edition of this course. 5. One should be preparedsystematicallythat the text of the exam assignments relates to the (software) concepts that are listed on the wiki pages for the individual lectures. Denitions of the concepts are never inquired, but basic understanding of the concepts is assumed and crucial for passing the exam. 6. The code fragments of the exam assignments may be drawn from the illustrations given for all concepts on the wiki pages, the contributions (i.e., implementations of the 101system ) practiced in the lectures, and the overall scenarios of the homework assignments. 7. Detailed knowledge of libraries, technologies, and language extensions (such as specic details of java.lang.reect, Hibernate mapping les, or AspectJ language constructs) is never assumed. As the exam assignments require understanding of code, one needs to remember a fair amount of details though. 2

The Expression problem1

Consider the following object model for expression forms with support for the Visitor pattern: public abstract class Expr { } public class Literal extends Expr { public int info; // a constant public void accept(Visitor v) { v.visit(this); } } public class Add extends Expr { public Expr left, right; // operands of addition public void accept(Visitor v) { v.visit(this); } } public interface Visitor { void visit(Literal l); void visit(Add a); } A method declaration, needed for the Visitor pattern, is missing. Add the declaration and explain why it matters.

Reference solution
public abstract class Expr { public abstract void accept(Visitor v); } Without the abstract accept method in the Expr class, the visitor cannot dispatch, e.g., on operands of an addition.

1 This topic concerns the lecture with the same name. One should understand, for example, data and operation extensibility, the relationship to class inheritance, object composition, and the use of static versus instance methods. The problem also resurfaces in the contexts of AOP and the Visitor design pattern.

XML data binding2

Consider the following code fragment that should unmarshal an XML le into a Company object: JAXBContext jaxbContext = JAXBContext.newInstance("com.company"); Unmarshaller unMarshaller = jaxbContext.createUnmarshaller(); Company c = (Company) unMarshaller.unmarshal("sampleCompany.xml"); Why does the unmarshaller need to be prepared with a context that points out the package of classes to be considered for unmarshaling?

Reference solution
It is unreasonable for the unmarshaller to try all the many classes in the classpath. Also, multiple classes could match with a found XML tag.

2 This topic concerns the lecture with the same name. One should understand, for example, some basics of XML schemas, the dierences between object graphs and XML data, the overall utility of annotations in the mapping processing, the notions of marshaling and unmarshaling, and the implications on OO programming.

Object/relational mapping3

Consider the following SQL DDL statement for an EMPLOYEE table: create table EMPLOYEE ( ID bigint generated by default as identity (start with 1), name varchar(255), address varchar(255), salary double, DEPT_ID bigint, primary key (ID), foreign key (DEPT_ID) references DEPARTMENT ) This statement was implied by O/R mapping for a straightforward Employee class with elds for name, address, and salary. The ID column serves as a primary key and we can assume that the Employee class also has an ID eld. Does the Employee class also contain a eld corresponding to DEPT ID? Support your answer with a concise argument.

Reference solution
No (except for a non-standard model). An employee does not link to its department. Rather, each department aggregates its employees.

This topic concerns the lecture Database programming, specically the part on object/relational mapping. One should understand, for example, the motivation for the use of O/R mapping, basic mapping rules from object models to relational tables, the notion of transactions on the OO program side, the mechanics of persisting objects and retrieving them later again.

Reection4

Consider the following reection code which performs object construction and method invocation: 01 02 03 04 05 Class clss = Class.forName("mypackage.MyClass"); Constructor cons = clss.getConstructor(); Object obj = cons.newInstance(); Method meth = clss.getMethod("myMethod"); meth.invoke(obj);

What could go wrong in the sense that a precondition of a statement would not be met? (Perhaps, use line numbers for convenience.) Identify at least 2 dierent issues.

Reference solution
L1: Class mypackage.MyClass may be missing from the classpath. L2: The class may lack a default constructor. L4: The class may lack an instance method myMethod. L4: The signature of myMethod may be dierent. (These are a bit more than 140 chars because 4 reasons are given for convenience.)

4 This topic concerns the lecture Metaprograms and metadata, specically the part on Javas introspection. One should understand, for example, the basics of the reection API, its fundamental limitations, simple use cases such as object dumping, and the role of exceptions in the use of reection.

Aspect-oriented programming5

Consider the following two pointcuts for the interception of moves of gure elements which should be drawn on a display. pointcut move(FigureElement fe): target(fe) && (call(void FigureElement.moveBy(int, int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int))); pointcut topLevelMove(FigureElement fe): move(fe) && !cflowbelow(move(FigureElement)); Specically, the rst pointcut (named move ) intercepts some setters of X and Y coordinates and an actual moveBy method (which can be assumed to change both coordinates at once). The second pointcut (named topLevelMove ) derives from the rst one; see the two references to move. (The cowbelow predicate is used: the pointcut cowbelow (P ) denes all join points that occur in the control ow of the join points dened by P excluding the join points of P .) What is the eect of the cowbelow predicate?

Reference solution
topLevelMove captures the same join points as move except for moves that happen as part of another move.

5 This topic concerns the lecture with the same name. One should understand, for example, the key notions of crosscutting, join point, pointcut, before/after/around advice, and inter-type declarations as well as basic application scenarios such as logging, contract checking, and update handling.

Functors in OO programs6

Describe a functor interface for a binary operation on some parametric type X . Write down suitable Java code.

Reference solution
interface Binary<X> { X apply(X x, X y); }

This topic concerns the lecture Functional OO programming. The lecture dealt with parsing and traversal combinators, but these relatively advanced scenarios are not exercised in the exam. Instead, one should understand the notion of functors as such and manage simple scenarios such as list processing, as exercised by the homework assignment on FOOP.

Multithreading7

Consider the following synchronized implementation of a withdraw method of a straightforward Account class: 01 02 03 04 05 06 07 08 09 public synchronized boolean withdraw (int amount) { int preview = balance - amount; if (preview<0) return false; else { balance = preview; return true; } }

How would the method be vulnerable to an inconsistent state, if no synchronization was required? (You may refer to line numbers for clarity.)

Reference solution
balance is read in line 02 and modied in line 06. Any concurrent modication of balance in between would be lost.

7 This topic concerns the lecture with the same name. One should understand, for example, the use of callables, runnables, functors, and thread pools in scenarios of programming with threads as well as simple means of synchronization for use of shared resources.

Remote method invocation8

Consider the following simple class for companies. This should be thought of as a server-side class, i.e., a class of a remote objects to be accessible on the client side. class CompanyImpl extends UnicastRemoteObject implements Company { private static final long serialVersionUID = -200889592677165250L; private List<Department> depts = new LinkedList<Department>(); protected CompanyImpl() throws RemoteException { } public List<Department> getDepts() { return depts; } } Does the corresponding client-side stub also have a private eld for the list of departments? Support your answer with a concise argument.

Reference solution
This is not reasonable because a) the stub is generated from the interface alone and b) the proxy object only has remote state.

This topic concerns the lecture with the same name. One should understand, for example, the role of interfaces for services, the need for client-side stubs (proxies) and server-side skeletons, and the use of a name-binding service.

10

Recursive descent parsing9

Consider the following procedure (in fact, method) for a nonterminal Company of a recursive descent parser (in fact, just an acceptor) for companies (more or less like in the 101project). private final void company() { match("company"); quoted_string(); match("{"); ceo(); while (test("department")) department(); match("}"); } What grammar production corresponds to this code? (Some reasonable/understandable grammar notation is Ok.)

Reference solution
company : company STRING { ceo department* }

This topic concerns the lecture Language processing patterns, specically the Acceptor and Parser patterns. One should understand, for example, the correspondence between grammars and procedures (methods) that perform recursive descent parsing.

11

10

Parser generation10

Consider the following ANTLR grammar fragment which is concerned with expression evaluation. expr returns [int value] : e=multExpr {$value = $e.value;} ( + e=multExpr {$value += $e.value;} | - e=multExpr {$value -= $e.value;} )*;

Does this ANTLR grammar implement the Text-to-object design pattern? Support your answer with a concise argument.

Reference solution
No. This grammar does not intend to build objects as abstract representation of the input; instead the input is evaluated.

This topic concerns the lecture Generative programming, specically the Parser Generation and Text-to-object patterns. One should understand, for example, the representation of context-free grammars with ANTLR, the overall mechanics of generating, building, and running an ANTLR-based parser, the role and expressiveness of semantic actions in ANTLR, and the possibility to parse text into ANTLR-unaware objects, in fact, POJOs.

10

12

You might also like