Computer Laboratory


Supervision questions: Programming in Java

This is the set of questions for my supervisions in Programming in Java. 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




Some of these questions are asking you to write code. In his case, please submit (1) the source code, (2) a runnable .jar file for it, and (3) a transcript of it compiling and running, as shown in the example below (or the equivalent output from the console in an IDE, e.g. Eclipse):

malte@cassiopeia:~$ javac 
malte@cassiopeia:~$ java Foo
Hello World!


Supervision 1: Basics

  1. Java is called an "object-oriented" programming language. Why is this? What is the notion of an "object", and how is it different from a "class"? [4 marks]
  2. Why does Java have packages? What are they and why are they useful? [3 marks]
  3. Describe, with the aid of a table, how integer numbers are represented in Java:
    • What integer types are there?
    • What are their sizes in bits, and what (approximate) number ranges can they represent?
    • How are negative numbers represented? What number does 0xDEADBEEF correspond to? How is the smallest (i.e. most negative) number that a Java int can hold represented in memory?
    [8 marks]
  4. What is the difference between a primitive datatype and a class type in Java? What are the primitive types? [3+4 marks]
  5. Write a class Stack that implements a binary last-in-first-out (LIFO) stack using a Java long type, the bitwise and the shift operators. This stack can hold up to 64 binary values (which are 0 or 1, respectively). Provide the following methods:
    • long pop(long stack) { ... }: This should take a number representing the stack as its argument, print the least significant bit as a boolean value (true or false), and return the remainder of the stack as a number. For example, for a stack represented by the number 10 in decimal (1010 in binary) should print "false" and return 3 (binary 101).
    • long push(long stack, boolean val) { ... }: This should take a number representing the stack as its first argument, and a value to be pushed onto it as the second. It should return the stack with the new value added. For example, push(3, false) should return 10 (binary 1010), and push(3, true) should return 11 (binary 1011).
    [15 marks]
  6. Bonus question: Modify the above stack implementation such that it maintains the stack in a global (member) variable (initialised to 0), rather than having to pass it into the methods every time. [This may require you to look ahead in the course material, use online documentation or have prior programming knowledge]. [4 marks]

Supervision 2: Advanced keywords, References and Arrays

Please also note the new Java Programming Competition page!
  1. What is an array? Why are arrays useful? Why do you think Java requires you to specify the size of an array when you declare it, rather than allowing it to be changed dynamically (as possible in other languages, e.g. PHP)? [4 marks]
  2. What are the meanings of the following Java keywords and where are they used? (Give an example for each.)
    • static,
    • final,
    • private,
    • for,
    • implements,
    • this,
    • Bonus: do { ... } while ( expr ).
    [6+1 marks]
  3. 1998 Paper 3 Question 3, first and third part only [8+4 marks]
  4. 1998 Paper 1 Question 10, (a) and (b) part only [8+6 marks]
  5. Write a Vector class that represents an n-dimensional vector. This class should provide the following facilities:
    • a constructor that takes the number of dimensions as an argument and sets up the necessary internal storage,
    • a method void set(int component, double value) that sets the value of a component,
    • a corresponding double get(int component) method,
    • a int getDimensionality() method that returns the number of dimensions in this vector,
    • methods for adding and subtracting vectors,
    • a method for computing the scalar product of vectors.
    Note that all methods for doing operations with vectors should always check the dimensionality of the vectors involved and ensure that they are of compatible sizes! [15 marks]
    Note: In a later supervision, you will be building a Matrix class and perform linear algebra operations involving your vector and matrix classes :-)

Supervision 3: Exceptions and Graphical User Interfaces

  1. Explain briefly why exceptions exist in Java. You should highlight in your answer why the concept of exceptions is preferable to alternatives, such as returning numeric error codes from functions. [3 marks]
  2. Consider the different ways of handling exceptions. For a line int n = a / b; where b = 0, explain three ways of dealing with the resulting DivisionByZeroException. [6 marks]
  3. Why do you think Java allows us to create additional exceptions to the built-in ones? How would one go about doing so? [5 marks]
  4. What facilities does Java provide for creating graphical user interfaces (GUIs)? What are layouts and controls? [5 marks]
  5. Draw a rough design (i.e. a "screenshot" of what it would look like) for the user interface of a simple desk calculator, and then sketch the corresponding UML diagram to implement it. [10 marks]
  6. 2003 Paper 1 Question 9, all parts except (a) and (h) [16 marks]
  7. 2003 Paper 1 Question 3 [10 marks]
  8. 2000 Paper 1 Question 4 [10 marks]
  9. Write a Matrix class that represents an n-by-m matrix. This class should provide the following facilities:
    • a constructor that takes the dimensions as arguments and sets up the necessary internal storage,
    • methods to get and set the value of any matrix component,
    • static and non-static methods to add, subtract and multiply matrices (Note: You will have to have a way of changing the dimensions of the matrix - and of its internal storage - for non-static multiplication to work).
    • a method for multiplying a matrix and a vector with each other.
    • a custom IncompatibleDimensionsException that is thrown when the user tries to perform operations on matrices of incompatible sizes.
    Note that all methods should always check the dimensionality of the matrices and prevent the user from performing illegal actions (e.g. multiplying matrices that are not of sizes n-by-m and m-by-k. [20 marks]

Supervision 4: Closing the Circle

  1. Explain how the instanceof keyword works as an operator in Java. What do you think it could be useful for? [3 marks]
  2. 2005 Paper 1 Question 9 [20 marks]
  3. 1998 Paper 1 Question 3 [10 marks]
  4. 1998 Paper 1 Question 9 (brief answers) [20 marks]
  5. Write a DeQue class that represents a double-ended queue (i.e. you can add and remove elements from the head and the tail of the queue). This class should provide the following facilities:
    • a constructor that sets up the queue including the necessary internal storage,
    • methods to add and remove elements both at the start and the tail of the queue,
    • a method to flush the queue (clearing it by removing all elements),
    • a method to return the current length of the queue,
    • a method to access the n-th element of the queue without removing it or any of the preceeding elements.
    In your implementation, pay particular attention to, and justify, your design decisions on:
    • the sort of internal storage used,
    • dealing with an empty queue in the methods that access elements,
    • whether the use of generics is appropriate or not.
    [Note: You are allowed to use existing Java library classes if you want.] [20 marks]
  6. 2001 Paper 1 Question 10 [20 marks]
  7. Bonus question (hard): 2003 Paper 1 Question 10 [20 marks]