Sie sind auf Seite 1von 3

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

COMS2003: Application and Analysis of Algorithms II


A3 Class Test 1 30 April 2010
Instructions This is an open book test You have 60 minutes to answer the paper There are 50 marks available The paper consists of 3 questions

1 Question 1 - 10 Marks
What is the output of the following program? Pay particular attention to scope rules and parameter passing. If youre not sure of your answer and you have time, draw the state of memory as the program goes on (This is not necessary, but could be helpful) import java.util.*; public class Things { static int a=0, b=5, c=10; static Integer d=5; static int[] values = new int[20]; static void method1(int x, int[] y){ int test=14; x=32; y[10]=9; } static void method2(Integer in){ in = new Integer(4); int c=15; a=6; int[] values=new int[20]; values[10]=15; } public static void main(String args[]){ method1(b, values); System.out.println("b = "+b + " ,val10 = "+values[10]); method2(d); System.out.println("a = " + a + " ,c= "+c+ " ,d = "+d + " ,val10 = "+values[10]); 1

} }

2 Question 2 - 15 Marks
Assume the following code has been implemented public class Car{ public String numberPlate; public int price; } public class Calculator{ public static void main(String args[]){ Garage myGarage = new Garage(); myGarage.addNewCar("NDF253GP",20000); myGarage.addNewCar("KLW453GP",45000); myGarage.addNewCar("AAA111GP",115000); double average = myGarage.getAverageValue(); System.out.println(average); } } The Garage class uses a Vector to store Cars. The addNewCar() method creates a new Car object using the parameters received and adds it to the Vector. The getAverageValue() method calculates and returns the average value of the cars in the Vector. Implement (Write code for) the Garage class, providing all the necessary methods and attributes.

3 Question 3 - 25 Marks
1. What is the effect of declaring a method as private? [5] 2. Identify and x the problem with the following code: [6] public class Test{ public int i = 7; private int j=10; public static void main(String args[]){ System.out.println(i+j); } } 3. What data types would you use to efciently store the following: [3] (a) The distance travelled by a motor vehicle in a day 2

(b) The number of people in a lift (c) The number of people on earth 4. Explain why it is often a bad idea to use break statements to terminate loops. [4] 5. Is it possible to store objects of many different types in the same array? Explain your answer [7]

Das könnte Ihnen auch gefallen