You are on page 1of 14

Name: _________________________________________ CS1081

Write Registration Number Here:

1. 2. 3. 4. 5. 6. 7. 8.

Two hours

QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM

UNIVERSITY OF MANCHESTER

Object Oriented Programming with Java (I)

January 2003

The Paper is in FOUR SECTIONS

SECTION A is compulsory and counts for half the marks.


SECTION A must be answered on the Question Paper

Only answers written in the boxes on the Question Paper will be marked

You should answer any TWO of SECTIONS B, C and D as well as SECTION A

Answer EACH of SECTIONS B, C and D in a new book

All question papers to be returned

The use of electronic calculators is not permitted.

[PTO]
2 CS1081

SECTION A

Answer SECTION A on this Question Paper.


The Question Paper must be returned before you leave the examination.

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.

Each question will be given equal weight.


Do not guess; marks will be deducted for incorrect answers.

A1) The computer term RAM stands for?

A) A male sheep in a computer game.


B) Really Active Mouse.
C) Reactive Allocated Module.
D) Random Access Memory.
E) Read Always Memory.

A2) The point at which a Java program starts executing is called?

A) initial
B) begin
C) entry
D) start
E) main

A3) Which one of the following is true?

A) All Java comments start and end with /*.


B) Java comments can end with */ or **.
C) The Javadoc tool recognizes comments it should create documentation from by the /**
token used to start the comment.
D) A computer program cannot read Java comments because they are natural language.
E) It is not possible to nest comments within a comment started with //.

3
3 CS1081

A4) Which of the following is not a keyword in Java?

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?

A) Class members are public, whereas instance members are private.


B) static members cannot be used in a non-static context.
C) public members cannot be used in a static context.
D) private members cannot be used in other classes.
E) Class members are non-static, whereas instance members are static.

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

A7) Which of the following is false?

A) The literal 5.0d is a double.


B) The literal 5.0D is a double.
C) The literal 5.00 is a double.
D) The literal 5.0F is a float.
E) The literal 5.0 is a float.

A8) Methods are either void or non-void. Which one of the following statements is true?

A) A non-void method is one which works properly.


B) A void method is one which returns a null reference.
C) A void method is one which does not return a result value, and does not take arguments.
D) A void method is one which always takes no arguments.
E) A non-void method is one which always returns a result, although that result might be a
null reference.

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?

A) long has lower precision than int.


B) long has greater precision than double.
C) long has lower precision than float.
D) int has lower precision than byte.
E) byte has greater precision than double.

10
5 CS1081

A11) Observe the following fragment of code.

int y, z;
// some code here which gives values to y and z
boolean x;

if (y <= z) x = true;
else x = false;

Which one of the following statements is true?

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

A12) Which of the following statements is true?

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

A15) Which one of the following is false?

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

A17) Which one of the following is false?

A) All Java objects are instances of a class.


B) All instances in a Java program have an associated type called a class.
C) The Java type int is a class.
D) The Java type String does inherit from java.lang.Object.
E) All objects are instances of a class.

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

A20) Which one of the following is false?

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?

A) String objects are immutable, so they can be changed efficiently.


B) StringBuffer objects are not immutable, so they are always efficient.
C) String objects are not immutable, so they are implemented efficiently.
D) StringBuffer objects are not immutable, so they are less efficient, except when many
changes are required.
E) StringBuffer objects are immutable, and so are more efficient than String objects.

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?

A) Recursion is always more efficient than iteration.


B) Iteration is always more easy to understand than recursion.
C) It is always possible to use iteration in a straight-forward way instead of recursion.
D) It is never appropriate to rewrite a recursion as an iteration.
E) None of the above.

23

A24) Which one of the following is true?

A) An applet can only be run using the applet viewer.


B) An applet can only be run using a web browser.
C) An applet can be run within a Java application.
D) An applet can only be run either using a web browser or using the applet viewer.
E) None of the above.

24

A25) Which one of the following is true for deciding whether to exit from a loop in Java?

A) All forms of loop require the use of a boolean expression.


B) Only for-loops require a bolean expression.
C) Only while-loops require a boolean expression.
D) Only do-while loops require a boolean expression.
E) Exactly two of the three forms require boolean expressions.

25

A26) Which of the following is available from the Math class in the package java.lang?

A) A public class method called pi().


B) A public class method called PI().
C) A public instance variable called PI.
D) A public class variable called pi.
E) A public class constant called PI.

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

A28) Which one of the following is true?

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

A30) Which one of the following is true?

A) A class can choose what it inherits from its super-class.


B) A class does not inherit anything but the public members of its super-class.
C) A class never inherits the private members of its super-class.
D) A class always inherits but cannot directly access the private members of its super-class.
E) A class does not inherit anything unless the Java keyword inherits is used.

30

[PTO]
12 CS1081

SECTION B

B31. a) i) Explain the relationship between objects and classes. (3 marks)

ii) How does a programmer create an object? Give an example. (2 marks)

iii) Explain the difference between instance and class variables. How are they
distinguished? (4 marks)

b) i) What is the difference between 'a' and "a"? (1 mark)

ii) Explain the difference between the == operator and the equals method from
the String class. When will they yield different results? (3 marks)

c) i) Explain the difference between String and StringBuffer. (3 marks)

ii) When should which be used, and why. (4 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)

b) Rewrite the following switch statement using only if () {} and/or if ()


{} else {} statements.

int x,y; boolean [ ] bit;


//
// code here sets x and y to arbitrary values, and
// creates a new array to hold the elements of bit
//
switch (x – y)
{
case 0 : bit[0] = bit[1];
bit[1] = false;
break;
case –1 : bit[0] = ! bit[0];
case 1 : bit[0] = ! bit[0];
bit[1] = bit[1] && bit[0];
case –2 : bit[0] = bit[1] || bit[0];
break;
default : System.out.println("Out of range.");
}
(6 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

D33. a) Explain carefully what is meant by recursion in Java. (3 marks)

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)

d) Given the existence of a method with the heading:

static boolean isWanted(int i)

Write a method with the heading

static int examAnswer (int n, int start)

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:

i) using a for-loop (4 marks)

ii) using recursion (5 marks)

You might also like