Professional Documents
Culture Documents
Object Oriented Programming With Java (I) - Jan 2003 (PAST PAPER)
Object Oriented Programming With Java (I) - Jan 2003 (PAST PAPER)
1. 2. 3. 4. 5. 6. 7. 8.
Two hours
UNIVERSITY OF MANCHESTER
January 2003
Only answers written in the boxes on the Question Paper will be marked
[PTO]
2 CS1081
SECTION A
Each question has exactly one correct answer, and should be answered
by clearly writing the appropriate letter (A, B, C, D or E) in the box provided.
A) initial
B) begin
C) entry
D) start
E) main
3
3 CS1081
A) then
B) while
C) static
D) break
E) class
A5) Class and instance members may be explicitly stated to be public or private and
sometimes also to be static. Which one of the following statements is true?
A6) Javadoc comments can be used to automatically generate HTML documentation. Which one
of the following statements is true?
A) Javadoc comments are usually designed to help users of a class know how it has been
implemented.
B) Javadoc comments are usually designed to help users of a class know what it does, but not
how to use it.
C) Every private method must have Javadoc comments to enable other users to see what the
method does; because they are not allowed to look at the code.
D) Javadoc comments are needed most for public members, as those are the items most
frequently used by other programmers, who usually do not want to look at the code.
E) Javadoc comments are usually designed to help anyone who needs to modify the class, as
they concentrate on explaining how the code works.
[PTO]
4 CS1081
A8) Methods are either void or non-void. Which one of the following statements is true?
A9) When we write classes, we can declare instance variables and local variables. Which one of
the following statements is true?
A) Instance variables and local variables are the same thing: they are simply variables which
cannot be used in other classes, only locally.
B) Local variables can only be used inside the method which declares them.
C) Instance variables can only be used inside the method which declares them.
D) Two different methods in the same class cannot each declare a local variable with the same
name.
E) A method can declare two local variables with the same name, as long they have different
types.
A10) Which of the following is true of primitive numerical data types in Java?
10
5 CS1081
int y, z;
// some code here which gives values to y and z
boolean x;
if (y <= z) x = true;
else x = false;
A) The if statement above is the simplest way to make x get the value true when y has a
larger value than z, and false otherwise.
B) The if statement above is the simplest way to make x get the value false when y has a
larger value than z, and true otherwise.
C) The if statement above is unnecessary, and should be replaced by a single assignment
statement to get the same effect.
D) The if statement above is one way to make x get the value true when y has a larger value
than z, and false otherwise.
E) The if statement above is one way to make x get the value true when z has a smaller value
than y, and false otherwise.
11
A) Unary arithmetic operators have lower precedence than binary arithmetic operators.
B) The boolean AND operator && has higher precedence than the boolean OR operator ||.
C) The assignment operator has higher precedence than the relational operators.
D) The arithmetic operators have lower precedence than the boolean operators.
E) Subexpressions at the same level of nesting are evaluated from right to left.
12
A13) Which one of the following is true about the three forms of loop statement in Java?
A) while loops are the only form of loop which may execute the loop body zero times.
B) do-while loops may execute the loop body zero times.
C) while loops always execute the loop body at least once.
D) do-while loops always execute the loop body at least once.
E) for loops always execute the loop body at least once.
13
[PTO]
6 CS1081
A14) In String and StringBuffer objects, each character is referenced by an index. Which
one of the following statements is true?
A) The indices run from zero, to one less than the length of the String or StringBuffer.
B) If we use the index 1.5, we can insert a character between those at indices 1 and 2.
C) A negative index can be used to signify that many places from the end of the String object;
e.g. an index of -1 enables access to the last character in the String object, -2 to the one
before that, and so on.
D) An empty String object has length one, because every String object must be null
terminated. So the indices of any String object actually run from 1 upwards.
E) If a StringBuffer object is empty, then any attempted use of the insert() method on it
should use an offset of 1, as the offset should lie in the range zero to the length of the
StringBuffer object.
14
A) A Java class that has no data members does not have a state.
B) In Java, behaviour is represented by the values of the data members of a class.
C) In Java, state is represented by the values of the data members of a class.
D) A Java class that has member methods has behaviour.
E) A Java class that has one or more data members has state.
15
A16) How many symbols can be represented uniquely using a 12-bit binary encoding?
A) 2047
B) 2048
C) 4095
D) 4096
E) 8191
16
7 CS1081
17
A18) Instances of classes are initialised via constructors. Which one of the following
statements is true?
A) A class can have more than one constructor, as long as they all have different names.
B) The programmer can choose not to define a constructor, in which case the class gets a
default one.
C) A class can have more than one constructor, but only one of them can be a void method.
D) A class cannot be instantiated unless the programmer defines a constructor.
E) A class can have more than one constructor, as long as they have the same number and
types of arguments.
18
A19) Which one of the following is true of the three forms of loop in Java?
A) They are logically equivalent – i.e. given any one you could achieve the effects of the other
two.
B) while loops and for loops are equivalent – but do-while loops are distinct and could not
be replaced by either of them.
C) All three forms are logically necessary – i.e. none could be used to replace another.
D) while loops and do-while loops are equivalent – but for loops are distinct and could not
be replaced by either of them.
E) for loops and do-while loops are equivalent – but while loops are distinct and could not
be replaced by either of them.
19
[PTO]
8 CS1081
A) An object instance does not require memory during the runtime of a Java Program.
B) An object instance requires memory for each of its data members during the runtime of a
Java program.
C) Separate memory is not required for each instance’s class variables during the runtime of a
Java Program.
D) Separate memory is required for each object instance during the runtime of a Java Program.
E) Separate memory is required for each instance’s instance variables during the runtime of a
Java Program.
20
A21) String objects and StringBuffer objects are both capable of storing pieces of text.
Which one of the following statements is true?
21
A22) The M.V.C. argument was introduced in the case studies of CS1081. Which one of the
following statements is true?
A) M.V.C. stands for model, view and control, and while model aspects of a program are often
separated into specific classes, the view and control aspects of a program can be kept together.
B) M.V.C. stands for method, visualization and connectivity, and is an approach to design that
allows several user interfaces for a program to be developed at the same time.
C) M.V.C. stands for model, view and control, and the argument suggests these 3 aspects of a
program can be beneficially separated into different classes, but in practise the model and view
aspects are always put in the same class.
D) M.V.C. stands for method, visualization and connectivity, and the argument suggests these
3 aspects of a program can be beneficially separated into different classes.
E) M.V.C. stands for model, view and control, and the argument suggests these 3 aspects of a
program should be designed at the same time so the program works smoothly.
22
9 CS1081
A23) Which one of the following is true when writing a Java program involving repeated
behaviour?
23
24
A25) Which one of the following is true for deciding whether to exit from a loop in Java?
25
A26) Which of the following is available from the Math class in the package java.lang?
26
[PTO]
10 CS1081
A27) Which of the following is not true of the self-referencing pointer this?
A) It can be used in an instance method to refer to an instance variable var using the notation
this.var.
B) It can be used in a class method to refer to an instance variable var using the notation
this.var.
C) It can be used in an instance method to refer to an instance method meth() using the
notation this.meth().
D) It can be used in a constructor method to refer to another constructor in the same class
using the notation this().
E) Its use is not permitted in a class method.
27
A) If a Button does not have an ActionListener, pressing it causes the program to crash.
B) A Button only generates an event the first time it is pressed.
C) It is not possible for an ActionListener to listen to more than one Button.
D) If an ActionListener listens to more than one Button, it cannot determine which Button
was pressed.
E) Any user-defined class can be defined to act as an ActionListener.
28
A29) String and StringBuffer objects have a charAt() method to access the character at a
given index position. The setCharAt() method can be used to change the character at a
given index position. Which one of the following statements is true?
A) String objects do not have a setCharAt() method because they are mutable.
B) String objects have a setCharAt() method, which takes the index position and the desired
new character as inputs and returns a new String object, being a copy of the old one with the
requested change made to it; it must return a new String object because it cannot change the
existing String object: String objects are immutable.
C) String objects have a setCharAt() method because they are immutable.
D) StringBuffer objects have a setCharAt() method, which takes the index position and the
desired new character, as its two arguments; and returns a new StringBuffer object, being a
copy of the old one with the requested change made to it.
E) StringBuffer objects have a setCharAt() method, which takes the index position and the
desired new character as its two arguments and alters the existing object (assuming the index
position is in range).
29
11 CS1081
30
[PTO]
12 CS1081
SECTION B
iii) Explain the difference between instance and class variables. How are they
distinguished? (4 marks)
ii) Explain the difference between the == operator and the equals method from
the String class. When will they yield different results? (3 marks)
[PTO]
13 CS1081
SECTION C
C32. a) What does the single task object (STO) principle recommend? When should
this principle be applied? (3 marks)
c) Explain what is meant by an array data type in Java. Describe how an array is
created and how the programmer can access the individual elements in the
array, as well as other information about the array. (6 marks)
d) In what ways is a vector (i.e., an object of the class Vector) different from an
array in Java. (5 marks)
[PTO]
14 CS1081
SECTION D
b) Taking care to distinguish them, what do the terms direct and indirect mean in the
above context? (2 marks)
c) Describe the syntax of the for loop in Java, explaining the purpose of each of its
parts. (6 marks)
which will return as its result the sum of the first n integers greater than or equal
to start for which isWanted() returns true. Do this: