CS110- Sample Final exam Page 1 of 7 CS110-Introduction to Computing with Java (Sections 1-2) Spring 2005 Sample Final Exam Namita Singla May 08, 2005 1. Implement a method that fills an array with consecutive integer numbers starting from the length of the array down to 1. For example, if the array is of length 5, then it should be filled with the values 5, 4, 3, 2, 1. /** * Fill the array with integer numbers from N down to 1. * N is determined by the length of the array. */ public void fillArray(int[] intArray) { for(int i = 0; i < intArray.length; i++) intArray[i] = intArray.length – i; } CS110- Sample Final exam Page 2 of 7 2. This problem concerns storing data about the population of various countries. The data for a single country is stored in an object belonging to a class called CountryData. The data for all the countries is stored in a class called Atlas. This class uses an array to hold the data. The problem is to complete the definition of the methods addCountry, getPopulation, and getTotalPopulation in the class Atlas. public class CountryData { private String name; // the name of the country private int population; // the population of country public CountryData(String name, int population) { this.name = name; this.population = population; } public String getName() { return name; } public int getPopulation() { return population; } } public class Atlas { CountryData[] data = new CountryData[200]; int countryCount = 0; void addCountry(String theName, int thePopulation) { // add data for a new country to the end of the // data array data[countryCount]= new CountryData(theName, thePopulation); countryCount++: } CS110- Sample Final exam Page 3 of 7 public int getPopulation(String countryName) { // // // // // Find the country named "countryName" in the array, and return the population of that country. If the country does not exist in the array, then a value of -1 should be returned. for (int i = 0; i < countryCount; i++) { if (data[i].getName().equals(countryName)) return data[i].getPopulation(); } return -1; } public int getTotalPopulation() { // return the total population of all the // countries in the array int totalPopulation = 0; for (int i = 0; i < countryCount; i++) totalPopulation += data[i].getPopulation(); return totalPopulation; } } CS110- Sample Final exam 3. What will the main method of the following Java class C print? public class A { protected int x = 1; public A(int i ) { x += i; } } public class B extends A { public B(int i) { super(i); x += i; } } public class C { public static void main(String argv[]) { A ar[] = new A[2]; ar[0] = new A(1); ar[1] = new B(2); System.out.println(ar[0].x); System.out.println(ar[1].x); System.out.println(ar[0].x + ar[1].x); } } 2 5 7 Page 4 of 7 CS110- Sample Final exam Page 5 of 7 4. Consider the following code fragment: public class TestException { public static void main(String[] args) { String[] p = new String[4]; try { p[0] = "John Lennon"; p[2] = "Paul McCartney"; p[4] = "George Martin"; p[1] = "Ringo Starr"; p[3] = "George Harrison"; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Only four developers"); } for (int i=0; i< p.length; i++) System.out.println(p[i]); } } (a) What will the main method of the above Java class print? Only four developers John Lennon null Paul McCartney null (b) Write the statement which will throw the ArrayIndexOutOfBoundsException? p[4] = "George Martin"; CS110- Sample Final exam Page 6 of 7 5. What is polymorphism and why is it important in object-oriented software development? Ability of superclass reference to denote objects of its own class and sub classes at runtime is called polymorphism or in other words ability to interchange modules dynamically at run time without affecting clients is called polymorphism. Polymorphism helps create programs that are: more concise modular easy to change and adapt 6. Suppose the classes Truck and Motorcycle are derived from the class Vehicle. Identify each declaration as valid or invalid. a. Truck t = new Vehicle (); Invalid b. Vehicle v = new Motorcycle (); Valid c. Motorcycle m = new Truck (); Invalid 7. Draw UML diagram for the hierarchy in question 6. Vehicle Truck Motorcycle CS110- Sample Final exam 8. (a) What is wrong with the following interface? public interface TwoDPoints { public int getX(){ return 0; } } All the methods in interface should be abstract i.e. without any implementation whereas in TwoDPoints interface getX () is implemented (b) Fix the interface in part a. public interface TwoDPoints { public int getX(); } Page 7 of 7