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) { } 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; void addCountry(String theName, int thePopulation) { // add data for a new country to the end of the // data array } 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. } public int getTotalPopulation() { // return the total population of all the // countries in the array } } 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); } } 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? (b) Write the statement which will throw the ArrayIndexOutOfBoundsException? CS110- Sample Final exam Page 6 of 7 5. What is polymorphism and why is it important in object-oriented software development? 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 (); b. Vehicle v = new Motorcycle (); c. Motorcycle m = new Truck (); 7. Draw UML diagram for the hierarchy in question 6. CS110- Sample Final exam 8. (a) What is wrong with the following interface? public interface TwoDPoints { public int getX(){ return 0; } } (b) Fix the interface in part a. Page 7 of 7