You are on page 1of 8

CSE 1020 3.

0 Introduction to Computer Science I


Final Exam, Summer 2012
90 Minutes
Name:
Student Number:
CSE email:
Note: Complete all questions in the space provided. Do not separate pages. Ensure
your name and student number are on all pages. This is a closed-book test. No external
aids are permitted.
DO NOT OPEN THE BOOKLET UNTIL INSTRUCTED TO DO SO
NAME: NUMBER:
1/8
Consider the following Java program fragment
String s = radar;
boolean result = true;
for(int i=0;i<s.length()/2;i++) {
result = result && (s.charAt(i) == s.charAt(s.legnth()-i-1);
System.out.printf(i %d result %b%n, i, result);
}
1. [2 marks] What does the Java program fragment shown above output?
2. [2 marks] Suppose that the fragment was run with different strings s (replacing the
initial string radar above). Complete the following table showing the number of
character comparisons required when the program is run
String s Number of character comparisons
radar
raddar
blueeulb
madamiamadam

3. [2 marks] What is the big-Oh running time of the program fragment. For full marks
you must provide a rationale for your answer.
NAME: NUMBER:
2/8
4. [4 marks] Draw the UML diagram that describes the classes and relationships
between the classes Colour, Shape, Blue and Vertex which have the following
public properties
Shape is a class that has
public method getName() : String
public method addVertex(Vertex)
public method getColour() : Colour
Colour is a class that has
public method setColour(oat, oat, oat)
Blue is a subclass of Colour that adds a
public eld hue : int
Vertex is a class that has elds
public int x
public int y

When shapes are constructed they must have a Colour associated with them, but
multiple Vertex(es) can be added independently to a Shape. A Shape has a single
Colour but may have many Vertexs.
NAME: NUMBER:
3/8
5. [1 mark] What is meant by the statement that strings in Java are immutable?
6. [2 marks] What does the following Java program fragment output?
LIst<String> l = new ArrayList<String>();
for(int i=0;i<5;i++)
l.add( + i);
for(int i=4;i>=0;i--)
System.out.printf(%s,l.get(i));
System.out.printf(%n);
7. [2 marks] What does the following Java program fragment output?
Map map<String,String> = new HashMap<String,String>();
map.put(x,y);
map.put(y,z);
map.put(y,p);
System.out.printf(%s%n, map.get(map.get(x)));
NAME: NUMBER:
4/8
8. [4 marks] Suppose that the collections framework did not support a representation
like Set. We could emulate the operation of set with a list where when we are
asked to add an object to the set, if the object already exists we do not add it.
Sketch code to implement addToListWithoutReplacement given a list l and an
element e
List<String> l = .... some list with String elements in which no duplicate elements exist
String e = ...the element to add to the list l
Note: e should be added to the list l if, and only if e is not already in the list. If e can
already be found in the list l, your code should not modify the list l. If you cannot
remember the specics of the methods for a List, make your best guess and give a
short description of the denition you assumed.
9. [2 marks] Does every instance of an object have a toString() method? Why or why
not?
NAME: NUMBER:
5/8
10. [7 marks] Suppose that the denition of the boolean methods equals(Object other)
in Integer had been dened as public boolean equals(Integer other) and that
under this denition two Integers are equal if their corresponding primitive ints are
equal.
Then what does the following code fragment output?
Integer x = new Integer(2);
Integer y = new Integer(2);
Object ox = x;
Object oy = y;
System.out.printf(%b %b%n, x == y, x.equals(y));
System.out.printf(%b %b%n, ox == oy, ox.equals(oy));
System.out.printf(%b %n, ((Integer) ox).equals((Integer) oy);
System.out.printf(%b %n, ox.equals((Integer) oy);
System.out.printf(%b %n, ((Integer) ox).equals(oy);
11. [2 marks] Suppose an algorithm takes n^3 + 1000 * n + 100000000 steps. What is
its big-O running time?
NAME: NUMBER:
6/8
12. [ 2 marks] Place each of the following big-O running times in order from smallest to
largest
O(n^2), O(log n), O(1) O(2^n) and O(n^5)
13. [4 marks] Rewrite the following program fragment between the comments using
StringBuffer so that the fragment makes as few temporary copies of Strings as
possible.
Scanner sc = new Scanner(System.in);
String s = sc.nextToken();
/* from here */
String res = ;
for(int i=0;i<s.length();i++)
for(int j=0;j<=i;j++)
res = res + s.charAt(i);
/* to here */
System.out.println(res);
NAME: NUMBER:
7/8
14. [2 marks] The charAt(int index) method in String throws an
IndexOutOfBoundsException if the index is negative or beyond the end of the
string. What does the following program output?
String s = This the the end. ;
StringBuffer res = new StringBuffer();
for(int i= -2;i<=s.length();i++)
{
try {
res.append(new String(s.charAt(i)));
} catch(IndexOutofBoundsException e)
{
res.append(The End. );
}
}
System.out.println(res);


NAME: NUMBER:
8/8

You might also like