CSC 123/252 Study Guide to the final exam.
The final exam will be targeted to be about 1 to 1.5 hours in length. It will
NOT be comprehensive but will include some material that were covered
on previous exams and quizzes.
There will be an emphasis on aspect-oriented programming.
There will be an emphasis on writing code fragments.
The material on ML/F# will be covered lightly - just enough to ask you to
demonstrate that you've absorbed the material.
There could also be a question that ties everything we've learned this semester
together, something that concerns everything we've learned about languages, types,
abstraction, oop, polymorphism, etc...
----------------------------------------------------------------------------
Topics to Study:
Inheritance in C#.
Static versus runtime types of objects and their impact.
Type casting rules.
How they're different in C#/Java and C/C++
Understand the role of casting in a low-level language like C
and with respect too OOP.
Defining data structures using interfaces and inheritance (study the
expression tree programs).
The difference between interface, class and abstract classes.
The Observer, Adapter, and Visitor Design Patterns.
Polymorphism.
The difference between natural and artificially (ad-hoc)
polymorphic procedures.
Generics (parametric types) in C#. How to use it, how it differs
from C++ templates.
What are its advantages and disadvantages compared to purely
inheritance-based polymorphism
(static type checking and avoidance of type casts).
Be sure to read the MSDN article on .Net generics.
Polymorphism and Type Inference in ML/F#: What additional capabilities
with respect to polymorphic typing, does F# has compared to C#?
Aspect-Oriented Programming and AspectJ
Know how to define pointcuts and advice. You don't have to
know every pointcut, but at least call, execution, set, get,
within/withincode/cflow, args, target, this, and composition
using &&, || and !. Know how to call proceed inside around advice.
Know how to use "inter-type" declarations". What is the meaning
of "private" within an aspect?
The purpose of aspect oriented programming - what it's trying
to give us that we don't already have.
Understand AOP keywords such as cross-cutting, weaving, join-points.
Don't just memorize the definitions. Understand their purpose.
What to Study:
Class notes
Sample programs
Past assignments
The class notes are the most important. There are a lot of subtle
points about inheritance that are not shown by the sample programs
(because they work, and don't show what doesn't work).
How to Study:
Do the sample problems here without looking at the answer.
Do NOT memorize definitions or simply learn the mechanics without
understanding their purpose.
---- Sample questions to prepare for the final exam ----
Answers to be posted later - try them first.
1. Given the following:
interface I
{
I f();
}
class A : I
{
public virtual I f() { return this; }
}
Explain what's wrong with the following lines, what kind of errors will
be caused and how to correct it.
A x = new A();
A y = x.f();
2. In the visitor pattern we had a critical function:
public object accept(visitor v) { return v.visit(this); }
Where was this function defined (in what class(es) does it exist:
visitor or visitee?)
What does "this" refer to?
3. Describe a situation in which an object adapter (aka adapter pattern)
can be useful. Can you think of a situation outside of the food
program?
4. Explain the difference between natural and artificial polymorphism.
Describe an example of each type of procedure.
When answering questions that require you to compose a paragraph, you
cannot just memorize what I say verbatim. You must be able to put
the answer in your own words. Not all questions I ask will be so
direct. For example, problem 6 below is really asking you about
the same thing as problem 4. Unless you truely appreciate the problem
here you will not see that, and you will not know how to answer the
question.
5. Explain in your own words one advantage of parametric polymorphism
over inheritance polymorphism. That is, what's the difference between
using a type parameter , and calling your variables (for example)
"A x" instead of "object x".
5b. Assume that classes sub1 and sub2 both extend class super1. Consider the
following alternatives for defining a polymorphic linked list class
that can contain elements of either sub1 or sub2:
class one
{
super1 item; // head, car
one next; // tail, cdr
}
class two where A : super1
{
A item;
two next;
}
Assume you wish to have a linked list M of sub1 objects and another list
N of sub2 objects. Which class would you use and why?
Assume you wish to have a single list that consists of some sub1 objects
and some sub2 objects. Which class would you use?
Write code to create sample lists using both one and two.
6. Given :
interface I
{ void f(int x); }
interface J
{ void g(int x); }
class A : J
{ int x = 0;
void g(int x) { x++; Console.WriteLine(x); }
}
Class A objects do not implement I interface. However, sometimes we may
want to use an A object as an I object (instead of a J object). Write an
object adapter class that enables this. That is, calling f on an adopted
A object should invoke A's g function.
6b. Assume that F# inferred that the type of a function is ('a -> int).
What does this mean? What is an example of a function that will have
such a type?
Sample AspectJ problems:
7. Assume that classes C and D both have a function void f(int).
Write a pointcut expression that picks out calls to either function. Also
capture the argument passed.
8. Given class:
class B
{
private int x;
pubilc B(int x0) { x=x0; } // constructor
public int f(int n)
{ if (n<2) return 1; else return n*f(n-1); }
public void g(int y)
{ System.out.println(x+f(y)); }
}
a. Write a pointcut that picks out the "execution" of the constructor of B.
(when the constructor is called, the object doesn't exist yet).
b. Write a pointcut that picks out the initial call to f (as opposed to
recursive calls).
c. The following pointcut and advice tries to change the parameter passed to
g. Explain what's wrong with it the way it's written. DON'T JUST CORRECT
IT; *EXPLAIN* WHY IT'S WRONG THE WAY IT IS!
before(int y) : call(void B.g(int)) && args(y)
{
g(y+1);
}
(hint: there are three problems that need to be addressed).
d. Write an advice that throws an error if the g function is called from
anywhere except main (public static void *.main(..))
f. Suppose an aspect contains the following intertype declarations:
Aspect importantaspect
{
public int B.y;
private int B.z;
...
Explain the difference between public and private above (hint: they're
not the same as public/private within an ordinary class). That is,
what are the consequences with respect to other parts of the program.
9. Explain the difference between "withincode" and "cflow" pointcuts.
10. Explain the difference between the "this" and "target" pointcuts.
The best way to answer this question is by using a specific example.
11. Describe a programming language with the following characteristics:
1. Allows type casting between arbitrary types.
2. No run-time type checking. I.e., no dynamic type information
3. No garbage collection (no automatic memory management)
4. No built-in support for OOP
5. No built-in supoort for writing polymorphic programs
11b. How would you modify these characteristics to describe Perl?