CPSC 233 Midterm, Winter 2003 DEPARTMENT OF COMPUTER SCIENCE THE UNIVERSITY OF CALGARY Time: 50 minutes 50 marks total L04 This is a closed book exam. No calculators or other computational devices are allowed. Put all your answers in this exam. Unless otherwise indicated assume that all programs and code fragments will compile. Section I, Short answer: 20 marks 1. True/False questions: circle the correct choice. (1 mark for each sub-question x 2 sub-questions = 2 marks total) a. Arrays in Java are always dynamically allocated (True / False): b. Array indices in Java must always be integers (True / False): Answer: True, True 2. When is the constructor for a class called? (2 marks) Answer: A constructor is called when an object gets (dynamically) instantiated. 3. Circle the correct choice (1 mark) The ( private / public ) data fields of an object can only be accessed or modified within the methods of that object. Answer: Private 4. Fill in the blanks (6 marks) The __________ operation allows you look at the element at the top of a stack without removing the element, whereas the __________ operation actually removes the element from the stack. You would add an element to the top of the stack with the __________operation. Answer: peek, pop, push 5. What are the components of the signature of a method? (3 marks) Answer: It includes the name of a method, the type and number of its parameters. (Some parts of the book incorrectly include the return value as part of the signature). 6. Describe what is meant by message passing?. (2 marks) Answer: Message passing refers to the invocation of the methods of an object. (Extra note: This is not same as invoking the static methods of a class). For the following two questions, please refer to the statements below: String s1; // First statement s1 = new String ("233 Rocks!"); // Second statement 7. Which of the following is true about the first statement? (2 marks) a. It is a declaration of a reference to an object of type String. b. It is a declaration of class String c. It allocated memory for an instance of type String and this memory is called/labeled ?s1?. d. It dynamically allocated memory for an instance of type String with the address of the dynamic memory being stored in the reference called/labeled ?s1?. e. None of the above are true. Answer: a 8. Which of the following is true about the second statement? (2 marks) a. It is a declaration of a reference to an object of type String. b. It is a declaration of class String c. It allocated memory for an instance of type String and this memory is called/labeled ?s1?. d. It dynamically allocated memory for an instance of type String with the address of the dynamic memory being stored in the reference called/labeled ?s1?. e. None of the above are true. Answer: d Section II, Reading code (What does this do?): 15 marks 1. In the space provided below list the output of the following program. (10 marks) class Person { private String name; private int age; public Person () { name = "Nameless"; age = 0; } public Person (String s, int i) { name = s; age = i; } public String getName () { return name; } public void setName (String newName) { name = newName; } public int getAge () { return age; } public void setAge (int newAge) { age = newAge; } } class Driver { public static void methodOne (Person p) { Person temp = new Person ("Jim Tam (Ace Consultant)", 24); p = temp; System.out.println("m1: " + p.getName() + " " + p.getAge()); } public static void methodTwo (Person p) { p.setName("Jim Tam (Ace Gameshow host)"); p.setAge(27); System.out.println("m2: " + p.getName() + " " + p.getAge()); } public static void main (String [] argv) { Person p = new Person (); System.out.println("Main: " + p.getName() + " " + p.getAge()); methodOne (p); System.out.println("Main: " + p.getName() + " " + p.getAge()); methodTwo (p); System.out.println("Main: " + p.getName() + " " + p.getAge()); } } << Put your answer here >> Main: Nameless 0 m1: Jim Tam (Ace Consultant) 24 Main: Nameless 0 m2: Jim Tam (Ace Gameshow host) 27 Main: Jim Tam (Ace Gameshow host) 27 2. In the space provided below list the output of the following program. (5 marks) public class Uhoh { static void not(int o) { System.out.print("Rats "); } static int again(int ack) { System.out.print("Big "); if (ack > 0) Uhoh.not(again(ack-1)); return ack; } public static void main(String[] args) { int ack = 4; again(ack); System.out.print("Rats"); } } << Put your answer here >> Big Big Big Big Big Rats Rats Rats Rats Rats Section III, Writing code: 15 marks 1. For this question, please refer to the following definitions for class Walker and class LOTR. (10 marks) In the space provided below, in the main method of class LOTR, write the code to do the following: * Set the name field of w1 to ?Legolas? * Set the race field of w1 to ?Wood Elf? * Display the NEMESIS field on a line by itself * Display the name field on a line by itself * Display the race field on a line by itself class Walker { private String name; private String race; public static final String NEMESIS = "Sauron"; public Walker () { name = ""; race = ""; } public String getName () { return name; } public void setName (String n) {name = n; } public String getRace () {return race; } public void setRace (String r) {race = r;} } class LOTR { public static void main (String [] argv) { Walker w1 = new Walker (); << Put your answer here >> w1.setName("Legolas"); w1.setRace("Wood Elf"); System.out.println(Walker.NEMESIS); System.out.println(w1.getName()); System.out.println(w1.getRace()); } } 2. In the space provided below write a toString () method for class ManyFields. (5 marks) class ManyFields { private String str; private int num; private boolean bool; public ManyFields () { str = "Foo good times and foo bad"; num = 34; bool = true; } public ManyFields (String s) { str = s; } public ManyFields (int n) { num = n; } public ManyFields (boolean b) { bool = b; } << Put your answer here >> public String toString () { String s = new String (); s = s + str + num + bool; // Could check if str is null return (s); // but it isn't manditory } }