OOP Final Exam Practice Questions Disclaimer: The following are questions that try to provide you with some practice material for the final exam. By no means do they represent the only material you should know for the final exam. We will not be providing answers to this set of questions however, feel free to see the instructors or TAs for assistance during office hours. Some of the questions can be considered challenging. General Questions ----------------- 1. What is the difference between overriding and overloading? 2. Can every class in Java have a main method? 3. Can you have a private class? 4. What is the difference between the following two declarations? final static int SIZE = 10; final int SIZE = 10; 5. What is the difference between using composition vs using inheritance? 6. What is the difference between a class and an instance of a class? 7. What are the advantages of the StringBuffer class? Implementation -------------- 1. Write a static method that duplicates a two-dimensional array of doubles. The signature of the method is: public static double[][] duplicate(double[][] array); 2. Write a static method that returns the first row of a two-dimensional array of integers that contains only zeros. The signature of the method is: public static double[] onlyZeros(double[][] array); 3. Implement a method call theSame that determines whether two two-dimensional arrays of String objects have the same objects. Your solution should be efficient; that is once you know the two arrays are not the same no further computation should take place. HINT: Use break/label 4. Write a method that receives a two-dimensional array of String objects and returns a two-dimensional array of String objects where all the null entries have been removed. For example, if the original array has the data (NULL represents a null reference): John NULL Mary NULL Pete Rick the the result generated by your method will be a two-dimensional array with two rows. John Mary Pete Rick 5. Using the TableSeatingChart class presented in class (and available online in the Code Snippets section) draw a memory map that represents the contents of the heap and stack up to the point (but before we return) from the addSection method in the following piece of code: int maxPersonsPerTable = 3; TableSeatingChart chart = new TableSeatingChart(maxPersonsPerTable); // Create some sections int numberOfTables = 4; int firstSection = chart.addSection(numberOfTables); 6. Implement the classes and interface associated with the inheritance hierarchy below. We are providing the basic idea but it is up to you to develop the full set of classes/interfaces. Try as much as possible to have the following constructs in your code: CONSTRUCTS: super, this, protected, overriding, overloading Inheritance Hierarchy: Vehicle ++++++++++ TaxableItem / \ / \ Car Truck Vehicle Class: data members: weight, maximumSpeed, numberOfDoors, numberOfTires, color methods: get methods for data members, abstract methods: start(), move(), shiftGear() Car Class: data members: add any members you understand are necessary methods: get/set methods for data members, isSedan(), isSportCar(), Truck Class: data members: maxLoadCapacity, maxSpeedLoaded methods: get/set methods for data members, unloadCargo(), loadCargo() TaxableItem Interface: methods: getTaxAmount(), getItemName() Feel free to add any data members/methods you understand are necessary. 7. Expand the Scramble.java program discussed in class as follows: a. Version 1: Add a difficulty level. Currently we tried to scramble all the letters in a word. You can change that and limit the number of letters scrambled controlling in this way the level of difficulty. b. Version 2: Instead of scrambling a word the game scrambles a phrase. Each word of the phrase will be scrambled and you will scramble the order of the phrases. 8. You will develop a program that implements a cookbook. We are providing a general description of the classes associated with this design. Feel free to add any methods/data members you understand are necessary. Try to practice the following concepts as you develop the implementation: One-dimensional array of objects get/set methods for classes private vs public access specifiers Passing primitives and objects as parameters I. Recipe class - represents a cooking recipe. --------------- data members: name - name associated with the recipe (e.g. "PlainCheeseCake") instructions - an array of String objects where each entry represents one instruction associated with the recipe. numberOfInstructions - integer representing the number of instructions ingredients - an array of String objects where each entry represents an ingredient numberOfIngredients - integer representing the number of ingredients methods: appropriate constructor get/set methods you understand are necessary toString() method equals method II. Cookbook - represents the collection of recipes ------------- data members: listOfRecipes - an array of Recipe objects numberOfRecipes - an integer representing the number of recipes methods: appropriate constructor get/set methods you understand are necessary addRecipe - adds the recipe by creating a new array that includes the current number of recipes plus the new one to add. You must keep the recipes ordered by recipe name. findRecipe - locates a recipe based on a recipe name. removeRecipe - deletes a recipe from the cookbook. It requires creating a new array with all the original recipes except the one to delete. toString - implement the appropriate toString method main - write a main method that tests your class and show how your methods work. 9. Reimplement the Cookbook class using classes from the Java Library that we studied in class. In particular, there are classes that could help you manipulate the array of recipes.