NUMBER:
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
1/8
NAME:
NUMBER:
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!
!
!
“radar”
“raddar”
“blueeulb”
“madamiamadam”
Number of character comparisons
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.
2/8
NAME:
NUMBER:
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(float, float, float)
Blue is a subclass of Colour that adds a
• public field hue : int
!
!
Vertex is a class that has fields
• 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 Vertex’s.
3/8
NAME:
NUMBER:
5. [1 mark] What is meant by the statement that ‘strings in Java are