COMP202-4

COMP202-4 - McGill University COMP-202A School of Computer...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: McGill University COMP-202A School of Computer Science McGill University School of Computer Science COMP-202A Introduction to Computing I Final Exam — Version 1 Tuesday, December 13, 2005. At 9 AM in the GYM Instructors: Joseph Vybihal, Yannick Daoudi, Sherif Shaker Student Name: Section # Student ID: Instructions N o notebooks, calculators or textbooks permitted in this exam. Language translation dictionaries are permitted. You are permitted to write your answers in either English or French. Attempt all questions. Parts marks are given for all questions. Show your work and do not leave a question empty. 0 Write all answers in the exam booklet. Grading Section Grade Your Mark Section 1: Multile Choice 20 Section 2: Problems 40 Section 3: Proramming 40 Pro. 1 Grade Pro. 2 Grade Pro. 3 Grade Total 100 % Instructors: Vybihal, Daoudi & Shaker. Page 1 of 11 McGill University COMP-202A School of Computer Science Section 1: Multiple Choice Questions 120 Points) Please circle your answer on this page AND write it in the box provided on the cover page. 1. String strBase="Canada is the second largest country in world!" Which statement from below will extract the substring "largest" from strBase? A) String subStr = strBase.substring("largest"); B) String subStrzstrBase.substring(strBase.indexOf("largest")); C) String subStr=strBase.substring(strBase.indexOf("largest"),strBase.indexOf("country")-1); D) String subStr=strBase.substring(strBase.indexOf("largest"), ((String)("1argest")).1ength); 2. Which feature of object-oriented programming languages (such as Java) enables the programmer to define a new class by adding attributes to an already-existing class and possibly modifying its behavior? A) Encapsulation B) Inheritance C) Polymorphism D) Aggregation E) Garbage collection 3. The following statements are examples of : public void setVar(String s){...} public void setVar(int x){...} public void setVar(int x, float f){ ...} A) Multiple inheritance B) polymorphism C) Overriding D) Overloading 4. What will the following program fragment output? Integer i = new Integer(5); Integer j = new Integer(5); if (i==j) System.out.println(“Equal”); else System.out.println(“Not equal”); A) Equal B) Not equal C) This code fragment has a syntax error in it D) The program doesn’t execute because i and j are not correctly initialized. 5. Like arrays, files A) Can contain information of only one type. B) Can be instantiated. C) Are stored as a one-dimensional vector of information. D) None of the above. 6. In recursion, the point of the base-case is to: A) Prevent ArrayIndexOutOfBounds exceptions. B) Ensure termination. C) Compute 1! D) Deal with low input values. 7. An exception can cause: A) Execution to jump to the corresponding catch block. B) An error message to be printed. C) The program to halt. D) All of the above. Instructors: Vybihal, Daoudi & Shaker. Page 2 of 11 McGill University COMP-202A School of Computer Science 8. A child class Doll extends a parent class Toy. Assume that each class has a public void play() method. Which of the following statements calls the play() method that belongs to the Toy class from within the D011 class? A) play(); B) Toy-play(); C) super.p1ay(); D) this.p1ay(); D) None of the above 9. If two Strings are declared as String strl = new String("Hello world"); and String str2 = new String("Hello world"); Then the value of strl == str2 is: A) true B) false C) illegal D) unknown E) Depends on the version of Java 10. Which of the following statements are FALSE? i) All classes extend another class (either explicitly or not), except Object ii) A class may extend more than one class iii) A class may implement more than one interface iv) An interface may extend another interface v) An interface may extend a class A) i and ii B) i and iii C)iandv D) ii and iv E) ii andv 11. Which of the following statements are FALSE? i) An interface can have private methods ii) An interface can have static final variables iii) An interface can have instance variables iV) An interface can have concrete methods v) An interface can have abstract methods A) i and ii B) ii and v C) i, ii, and v D) i, iii. and iv E) ii. iii. and v 12. If you pass an array from a main method to a method named changeItO, and the method changes a value in the array, then which of the following hold? A) You will receive an error message B) The original array element in main() changes C) The original element in main() remains unchanged D) The original element in main() changes only if you return a value from the method E) The address of the original array will change 13. When you pass an array to a method, the method receives? A) A copy of the values in the array B) The address of each of the array elements C) A copy of the value of the first element of the array D) The address of the first element of the array E) All the above Instructors: Vybihal, Daoudi & Shaker. Page 3 of 11 McGill University COMP-202A School of Computer Science 14. University is a child class of Building, BrickHouse is an object of type Building, and McGill is an object of type University. The University class contains a method named getRoomNumO; which of the following statements is valid? A) BrickHouse.getRoomNum(); B) McGill.getRoomNum(); C) University. getRoomNumO; D) a and b E) All the above 15. Which of the following is an effect of "protected"? A) A friend class can access methods that is protected B) a parent class can access protected field of child class C) the "protected" field cannot be rewritten after initialized D) a protected method cannot have a method body E) a child class can access protected method of parent class 16. What is the output of the following code fragment? public class A{ public void print(){ System.out.print(4); } public void print(int a){ System.out.print(3*a); } } public class B extends A{ public void print(int b){ System.out.print(b *b); } public void print(int 0, int d){ System.out.print(c+d); } .}. B bl = new B0; b1.print(4); A) 4 B) 8 C) 12 D) 16 e) compilation error Instructors: Vybihal, Daoudi & Shaker. Page 4 of 11 McGill University COMP-202A School of Computer Science 17. Static vs Instance. Static members (variables) of a class are initialized: A) When the program starts. B) When the class is instantiated for the first time using the new keyword. C) Just before the constructor of the class is called. D) At compile time. 18. Which of the following keywords in Java is NOT reserved for inheritance? A) super B) implements C) extends D) protected E) None of the above 19. Object References and Values. Determine the output of this program: class cExl { public String name; public cEx1(String in) { name = in; } public void print() { System.out.println(name); } } class cExZ { private cExl oldClass; public cEx2(cExl arg) { oldClass = arg; oldClass.name = new String("Aragorn"); } public void transformO { oldClass = new cExl("Saruman"); oldClass.name = new String("Gandalf"); } } static void main() { cExl a = new cExl("Frodo"); cEx2 b = new cEx2(a); b . transformo ; a.print(); } A) Frodo B) Aragorn C) Saruman D) Gandalf Instructors: Vybihal, Daoudi & Shaker. Page 5 of 11 McGill University COMP-202A School of Computer Science 20. Which of the following are valid array declarations/definitions? int iArray1[10]; int iArray2; int iArray3 = new int[10]; int iArray4[10] = new int[10]; int iArray5 = new int[10]; int iArray6 = new int; int iArray7 = null; A)2,3,5,7 B) l,3,5,7 C)2,3,7 D) 1,6,7 Instructors: Vybihal, Daoudi & Shaker. Page 6 of 11 McGill University COMP-202A School of Computer Science Section 2: Problems 140 Points} Answer the following questions on the exam booklet. 1. Provide answers for each part of the question below. Each part advances the problem on step further. (10 points) Part A: Write a public class called Circle with one private data field radius (of type double). Include a default constructor, which sets radius to 1.0, an accessor for radius and a modifier for radius. Parth Write a statement that declares a variable called myCircles which is an array of objects of type Circle. Part C: Write a second statement that initializes myCircles to be an array of 30 references to Circle objects. Part D: Write a for-loop which fills the array with the result returned by the default constructor of the class Circle. 2. We want to write a method that copies the lst, 3rd, 5th, etc..., elements of an array to a new array. Fill in the blanks: (10 points) public oddVals (double a) { int newLen = Math. __(a.length / 2); b = new [newLen]; for (int i = O; i < a.length; i +2 ) b[ ] = a[i]; return b; } 3. Fibonacci numbers are the numbers in the Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, etc. each of which, after the second is the sum of the two previous ones. Fibonacci numbers can also be considered as a function of non-negative integers where: (10 points) F (n) = {1, 1, 2, 3, 5, 8,13, 21, 34, 55, 89,144...} Forn={0,1, 2, 3, 4, 5, 6, 7, 8, 9,10,11...} Part A: Write an iterative method public static int F(int n) which takes an integer n as input, and that returns the Fibonacci value corresponding to 11 (ex: F(7) = 21). Part B: Write a recursive method that does the same thing as A) but using recursion. Part C: Which method is more efficient and why? Instructors: Vybihal, Daoudi & Shaker. Page 7 of 11 McGill University COMP-202A School of Computer Science 4. What is the output of the following program? (10 points) class Boxer implements Cloneable // the clone() method below { protected int stamina; protected int power; public Boxer (int stamina, int power) { this.stamina = stamina; this.power = power; } public void punch (Boxer opponent) { opponent.stamina —= power; } public Object clone () throws CloneNotSupportedException { return super.clone(); } public String toString () { String info = "This Boxer“; info += " has an average power of “ + power; info += " and a stamina of " + stamina; return info; class Kickboxer extends Boxer public Kickboxer (int stamina, int power) { super (stamina, power); } public void punch (Kickboxer opponent) { opponent.stamina —= power/2; } public void kick (Boxer opponent) { opponent.stamina —= power*2; } public String toString () { String info = "This Kickboxer"; info += " has an average power of “ + power; info += " and a stamina of " + stamina; return info; Instructors: Vybihal, Daoudi & Shaker. Page 8 of 11 McGill University COMP-202A School of Computer Science } public class Fight { public static void main (String args) throws CloneNotSupportedException { Boxer ali = new Boxer(50,6); System.out.println("ali: " + ali); Kickboxer panda = new Kickboxer(100,3); System.out.println("panda: " + panda); ali.punch(panda); panda.kick(ali); System.out.println(“ali: “ + ali); System.out.println(“panda: " + panda); Boxer me = (Kickboxer) panda.clone(); panda.punch(me); me.punch(panda); System.out.println("me: " + me); if (me.equals(panda)) System.out.println("l might be Panda“); else System.out.println("am I Panda?"); Instructors: Vybihal, Daoudi & Shaker. Page 9 of 11 McGill University COMP-202A School of Computer Science Section 3: Programming (40 Points! Attempt all questions; remember that part marks are given for every programming question. Write your answer in the exam booklet. 1. Short Programming Problem: Recursion (10 points) Write a recursive method that reverses a sentence, by completing the following piece of code. The output for the following should be ‘!y1feriF eht evil gnoL’. Public class Sentence { public static void main(String args) { System.out.println(reverse("Long live the Firefly!”)); } } 2. Medium Programming Problem: A range list is a text file that has a list of values of unknown length. These values are integers formatted as one per line. Every two consecutive values represent a range (1st & 211d line, 3rd & 4"] line, etc.). A range is only valid if it has two values and if the first value is strictly less than the second value. (15 points) 0 Write a new type of Exception called InvalidRangeExpection, which holds a message indicating that a range is not valid. 0 Write a driver program that reads in a range list (a file name given by the user), and only prints out its valid ranges formatted in this way: [vall,va12] [val3,val4] Invalid range [val7,va18] Note: - Invalid ranges should be dealt with elegantly using the InvalidRangeException class. 0 An invalid file name (FileNotFoundException) should also be dealt with elegantly. o If a valid file name is entered, you may assume it holds one integer per line. If you have trouble doing this question then this is a good question where you can solve only the file part or only the exception part to get half marks. If you can do a little of the part you have trouble with then you can get up to 2/3 of the points. Instructors: Vybihal, Daoudi & Shaker. Page 10 of 11 McGill University COMP-202A School of Computer Science 3. Long Programming Problem: Polymorphism (15 points) A biologist would like to track the movement of wildlife in a nature—preserve. In order to do this effectively she wants to build a true object oriented program that uses Encapsulation, Inheritance and Polymorphism. The wildlife she is studying is deer, antelope and wolf migration patterns. All the animals have some common information: String Name, int ID, an integer counter for how many times they have been seen, and ArrayList used to record the coordinates of where the animal was seen (int X and int Y values). The coordinates are used to track where the animal travels. Each type of animal has specific information about itself: Deer: String color Antelope: double weight Wolf: int packSize She needs a program with the following menu options: (1) Add an animal, (2) Display Information for an animal, (3) List all the animals, (4) Saw the animal today, (5) Quit program. After selecting any of these options the program loops back and displays the menu once again until Quit is selected. ADD ANIMAL: Will ask the user for information about an animal and insert that animal into the data structure that will store all the animals. The animals are stored using a unique ID number. DISPLAY INFO: Will ask the user for the ID number of an animal and then display on the screen ALL the information for only that animal. LIST ALL: Will simply list all the animals on the screen in a nice way. The nice way means that the animals will be listed 10 at a time. Each animal is displayed as a row. After 10 animals are displayed the program asks the user to press enter to continue. This does not display the animal’s ArrayList of coordinates but it does display the coordinates of the last location is was seen at. SAW ANIMAL: Asks the user for the animals ID number and the X and Y coordinates for that animals. The animals counter is incremented and the X and Y coordinate values are added to the ArrayList. Make sure to do the following: 0 She needs you to suggest a practical set of classes that use Inheritance and Polymorphism to capture the complexity of this problem. 0 Implement a data structure that can be used to store all these animals (do not use ArrayList). 0 Make sure to implement Encapsulation on all members This is also a good question for parts marks. Everything here can be divided into methods and classes. Each class and method has points attached to it. Start off with the easier ones and move on to the harder ones. Instructors: Vybihal, Daoudi & Shaker. Page 11 of 11 ...
View Full Document

This test prep was uploaded on 04/09/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 11

COMP202-4 - McGill University COMP-202A School of Computer...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online