Supervision questions: Object Oriented Programming

This is the set of questions for my supervisions in Object Oriented Programming. I will typically email you a list of question numbers before each supervision, but if not, attempt the next two or three. (More questions will appear here as I set them, so come back if you want to make an early start on some of the future work.)

Administrativa & handing in work

I expect you to make a good attempt at producing solutions to the relevant questions before each supervision. I prefer submissions by email (PDF or text format). Please submit your work 24 hours before the supervision. If you want to submit a hard copy of the work to student administration, please hand it in before 17:00 two days before the supervision (i.e. before Wednesday, 17:00 for a Friday afternoon supervision) as I will have to scan it. Remember that Student Administration is closed on weekends.

When emailing me regarding supervisions, please only use my lab address, or
your email will be misfiled and may slip by unnoticed:

The mark allocation (whilst very approximate) should give you a rough idea of how you should divide your time between the questions, as well as how much credit I expect a similar question to be worth in the exam. It will also serve as a guide for me when marking the questions.

If there is a particular part of the course you would like explaining, or questions you have about the lectures (independent of whether they are covered by the questions or not), please let me know in an email before the supervision so that I can prepare appropriately.

Supervision 1: Fundamentals

In the context of functional and imperative programming, what is the difference between a function and a procedure? Which is used with which programming methodology? [4 marks]

Explain in a short (max. a couple of paragraphs) high-level description of how a computer can execute a program written in some imperative programming language. Your answer should mention the following terms:

memory address,

compilation (or compile, compiler etc.),

main (system) memory,

registers,

transistors / logic gates.

fetch-execute cycle,

[8 marks]

Q1 from the example sheet [3 marks]

Q2 from the example sheet [8 marks]

Explain briefly the concept of Java bytecode and how the process described in 2. changes if we are considering a Java program. [Hint: There is one additional step that has to be performed]. [3 marks]

What might a pointer type of int **p in the C language mean? Explain how you would get at the int value stored. [2 marks]

Q5 from the example sheet [4 marks]

Q6 from the example sheet [5 marks]

Explain the difference between passing something "by reference" and "by value". Assuming a class Int with a member field value, write a small piece of code that contrasts the behaviour of this with the primitive int type. [Hint: Consider passing them to a procedure that will change the value, and what value they will hold after the procedure has returned]. [5 marks]

Supervision 2: OOP Principles, Java

Explain the terms coupling and cohesion, and indicate what we are generally optimising for with each of them ("low"/"high"). [4 marks]

Q7 from the exercise sheet [6 marks]

Explain the differences between a class, an abstract class and an interface [6 marks]

Q27 from the exercise sheet [7 marks]

Explain what polymorphism is in the context of object-oriented programming languages and how its realisation differs between Java and C++. [Hint: Use a worked example and consider virtual functions in the case of C++]. [5 marks]

Give an example of variable shadowing as a result of inheritance. [3 marks]

Is the Vector2D class developed in lectures mutable or immutable? [2 marks]

What is the difference between shallow and deep copying when cloning an object (using the clone() keyword or otherwise). [6 marks]

Q12 from the exercise sheet [20 marks]

Q16 from the exercise sheet [5 marks]

Bonus question: Explain how to write a Java program that is ‘functional’ in nature i.e. no modifiable variables, methods are mathematical functions without side effects. Remember the final keyword. [8 marks]

Supervision 3: Class Library, Design Patterns

What is the Java class library? In the context of the class library, explain what a collection and a comparator are and why they are useful. What are examples of each? [4 marks]

What are generics in Java? Why were they introduced? [Hint: Think about early versions of the collections framework] [4 marks]

Write an implementation of a generic linked list, i.e. one that can hold any
type of object, using Java generics (cf. Q35 on the exercise sheet). [5 marks]

Is there a difference between the == operator and calling the .equals() method on an object, and if so, when? [2 marks]

Q42 from the exercise sheet [8 marks]

What is a design pattern? [1 mark]

Q45 from example sheet [5 marks]

What is the difference between the state and strategy patterns? [3 marks]

Q46 from the example sheet [12 marks]

One technique to break a Singleton object is to extend it and
implement Cloneable. This allows Singleton
objects to be cloned, breaking the fundamental goal of a Singleton!
Write a Java Singleton class that is not final but
still prevents subclasses from being cloned. [4
marks]

Without referring to the solution notes, do the second question of the sample
exam questions. You may skip the differences between abstract classes and
interfaces in part (a)[18 marks]