Answer all questions
Time allowed: Three hours
In answers where you are writing Java programs and you wish to use facilities provided by the standard Java library, you may do so. Do not worry if you cannot remember the exact signatures of library constructors & methods; sensible guesses can score good marks; include comments to explain your intention if you think it might be unclear.
In answers where you are writing Java programs,
do not include javadoc
comments or test-drivers unless the
question explicitly asks for them.
Test
class definition:
public void Test() { <statements> }
public Test() { <statements> }
public static Test() { <statements> }
public static void Test() { <statements> }
Select the most appropriate answer.
Select all correct answers.
String str = "abc"; System.out.println(str.length()); int[] a = new int[3]; System.out.println(a.length);The programmer comes to two of the following conclusions. Which ones?
String
s, length
is a variable
String
s, length
is a method
length
is a variable
length
is a method
Select both correct answers.
public void insert(int a) { <statements> }
void
int
Select the most appropriate answer.
public void insert(int a) { <statements> }
void
int
Select the most appropriate answer.
public class One { public One(String theName) { name = theName; } public String getName() { System.out.println(name); } private String name; } public class Two { public static void main(String[] args) { One o = new One("Java"); o.getName(); } }
Java
Select the most appropriate answer.
public class Test { public Test() { count++; id = count; if (id <= 10) { System.out.println(id); } } private int id; private static int count = 9; public static void main(String[] args) { Test t1 = new Test(); Test t2 = new Test(); } }
id = count
count
Select all correct answers.
int x = 12; double y = 47.9; x = y;
x = y
gives rise to a compile-time error
x = y
gives rise to a run-time error
x
will come to contain 47
x
will come to contain 48
x
will come to contain 47.9
Select the most appropriate answer.
public class Test { public static void main(String[] args) { for (int i = 1; i < arrayOfIntegers.length; i++) { arrayOfIntegers[i] = i; } } private static int[] arrayOfIntegers; }
Select the most appropriate answer.
public class One { public int count; } public class Two { public static void main(String[] args) { System.out.println(count); // a System.out.println(One.count); // b One o = new One(); // c System.out.println(o.count); // d System.out.println(this.count); // e } }Select all lines of the program (a, b, c, d, e) that contain compile-time errors.
Object
s is one...
Object
or subclass of Object
Select all correct answers.
Object o = new JButton("Press me");
JButton b = new Object();
JButton b = new JLabel("Press me");
JComponent c = new JButton("Press me");
Select all correct answers.
Select the most appropriate answer.
One
is an interface, defined as follows:
public interface One { public void insert(int a); public void delete(int a); }A class definition,
Two
, begins as follows:
public class Two implements OneHence, which of the following is true:
Two
must include concrete
definitions of insert
and delete
Two
must include abstract
definitions of insert
and delete
Two
must include concrete
definitions of insert
or delete
Two
must include abstract
definitions of insert
or delete
Select the most appropriate answer.
One
interface from the previous question,
a class definition, Three
, begins as follows:
public abstract class Three implements OneHence, which of the following is true:
Three
must include
definitions of insert
and delete
and they must both be concrete
Three
must include
definitions of insert
and delete
and they must both be abstract
Three
must include
definitions of insert
and delete
and they may be either concrete or abstract
Three
must include
definitions of insert
or delete
and they must both be concrete or both abstract.
Select all correct answers.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame f = new JFrame("A window"); Container contentPane = f.getContentPane(); JButton b = new JButton("Press me"); contentPane.add(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { count++; System.out.println(count); } }); f.setBounds(100, 100, 100, 100); f.setVisible(true); } private static int count; }Which of these statements about the execution of this program are true:
Select all correct answers.
myfile
on the host file system:
import java.io.*; public class Test { public static void main(String[] args) throws IOException { DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream(new File("myfile")))); } }
try/catch
construction
try/catch
construction
Select the most appropriate answer.
PrintWriter pw = new PrintWriter(new BufferedWriter( new FileWriter(new File("myfile")))); pw.println(356); pw.close();Subsequently, the file is successfully opened for reading as follows:
BufferedReader br = new BufferedReader( new FileReader(new File("myfile")));What is required to read in the contents of the file and store them in a variable of type
int
?
read
operations, reading 8 bits at a time
(followed by 3 shift operations to assemble the bits into a 32-bit word)
readInt
operation
readObject
operation, followed by casting to an
int
readLine
operation, followed by conversion from
a String
to an int
using, e.g.,
Integer.parseInt
Select the most appropriate answer.
Which of the following accurately paraphrase parts of the diagram?
Foo
need not be associated with
corresponding objects of class Bar
Foo
must be associated with
corresponding objects of class Bar
Foo
must not be associated with
more than one object of class Bar
Foo
may be associated with
more than one object of class Bar
Select all correct answers.
Select four correct answers.
Note that text in italics signifies an abstract class or method.
Give Java definitions for the classes Publication and ConferencePaper.
In both class definitions, include a sensible constructor to initialise all instance variables.
JournalPaper
and Book
, have also been
written; both are concrete subclasses of Publication
.
Write a class method, called getTotalLength
,
that takes in a
single parameter and returns an int
. The parameter is an array
that may contain objects constructed from the ConferencePaper
,
JournalPaper
and Book
classes (in any mixture),
with some possibly empty cells. (You can assume the actual parameter
as a whole will not be null
.)
The method will exploit dynamic method binding to compute the total
length of the publications in the array.
CD-Direkt is a CD tele-sales company. Customers ring up to place orders for CDs. The order is taken by a sales-person, who records customer details, details of the recipient (if different from the customer) and, the quantity of each CD that the customer wants to order.
Draw a UML Class Diagram based on this description. There is no need to include attributes or operations of classes. Concentrate instead on identifying the classes, their relationships and, in the case of associations, their multiplicities.
Important: The examiners will not answer questions about this description during the examination. If you feel that there are problems with the description (e.g. vagueness, ambiguity or incompleteness), resolve them yourself by stating assumptions in your answer.
Use Swing classes to implement a simple scientific calculator, as follows.
Define a subclass
of JPanel
that contains an editable
JTextField
for entry of a number, three JButton
s and, below these,
an uneditable
JTextField
. When an object is created from your class
and added to a visible JFrame
, something like the following
will appear on the screen:
When users press one of the buttons, whatever they have typed into
the editable JTextField
is obtained, and converted to
a double
(if possible).
The mathematical function
that corresponds to the button is then applied to
the user's input. The result is displayed in
the uneditable JTextField
.
Include a main
method for running your program.
Hints & Help.
You can ignore fine details such as screen size, colours, fonts, etc.
The screenshot is intended to show roughly where you must place the components on the screen, but their precise layout (sizes, co-ordinates, alignment) need not be exactly as depicted.
String
stored in
variable str
to a double
is:
double number = Double.valueOf(str).doubleValue();A
NumberFormatException
is thrown if the String
cannot be converted to a double
.
Math
class to carry out
the computations you need, Math.sin, Math.cos
and
Math.tan
. (Ignore problems that might arise with these
methods, such as
taking the tangent of 90.)
JOptionPane.showMessageDialog( null, "Your error message", "A title for the little window", JOptionPane.ERROR_MESSAGE);