COMP202-2

COMP202-2 - School of Computer Science — Faculty of...

Info iconThis preview shows pages 1–13. 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

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

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

Unformatted text preview: School of Computer Science — Faculty of Science — McGill University COMP 202 — Introduction to computing 1 Final Exam - Winter 2006 April 13th 2006, 9:00-12:00 Examiners: Yannick Daoudi, Marc Lanctot, Nomair Naeem Associate Examiner: Joseph Vybihal Last Name: First Name: ID number: Section: Ell N .N aeem D2 Y.Da0udi D3 M.Lanct0t UDeferred Instructions: - No notebooks, textbooks, calculators, cell phones, pagers, laptops or handheld computers are allowed in this exam. - Language translation dictionaries are allowed. ~ FOR SECTION I gmultiple choice questions), answers should be marked on THIS FRONT PAGE of the exam. All other answers EXCEPT FOR 11.2 should be written on the booklet provided. - Answers can be given in French or English where applicable. - Read all questions carefully. _ Sectio_n_I: Multiplelchoic_e_ _ __ Your mark: / 30 __Qp¢s_ti_011__ Ql _Q_2__ __ Your _answer __ _ _ _Mark M /2 l2 /2 /2 _Question Q9 _Q10 Q11 (_Q_12_ 013 Your fl?!“ .. ___-__ __ _ Mark /2 ____[2_ I /2 _ Section II: Short Problems Your mark: / 30 _ Question _ _ S3 Mark _ /10 )10___' /10 Section III: Pr_ogramming_Pr0blems Your mark: / 40 FQW____ P1____ lMark _ _ __ /20 /20 Your Exam Score: / 100 COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 1 of 13 School of Computer Science — Faculty of Science — McGill University SECTION I: MULTIPLE CHOICE QUESTIONS (2 points each) 1.1 Which of the following is true about static member variabl A. They cannot be modified by private methods. B. They must be primitive types only. C. They must be initialized in the constructor. D. They have the same scope as final variables. E. Their values are shared among all instances of the class. [.2 What does the following program fragment return? if (y%x==O && y%2==0) return true; else return false; A. true if y is divisible by x or if y is even; false otherwise B. false if y is divisible by x or if y is odd; true otherwise C. true if y is even and if x is divisible by y; false otherwise D. true regardless the values of x and y E. false regardless the values of x and y Note: We say a is divisible by b if a divided by b yields an integer. 1.3 What does the following code fragment print? int 2 = 6; int y = 2; int x = 5; for (lnt I = O; I < 10, I++) { int x = 3, 1nt z = O; z = x + z; System.out.print(z); A. 3 B. 30 C. 6 D. 0 E. Nothing it produces a runtime error COMP 202 — Daoudi, Lanctot, Naeem es of a class? Version 1 - page 2 of 13 School of Computer Science — Faculty of Science — McGill University 1.4 We say a method is overloaded if: A. It has more than one declaration with differing signatures. B. It is contained in more than one class of the same package. C. It can change it's own code at run—time. D. It can return different results depending on the class that calls it. E. It is overridden. 1.5 Which one of the following statements is false? A. When an object reference variable ‘a’ of type A is passed to a method that expects a parameter of type B when B is a subclass of A, we must cast ‘a’ to the object type B. B. It is legal to have double A(int i, double j, String k) { . . . } and double A(double j, int i, String k) { . . . } in the same class. C. If 'x' is an alias of the object reference variable 'y', changing a private variable of 'x' will cause the same variable of 'y' to be changed in consequence. D. Two overloaded methods must have different parameter lists, but may have the same return type. E. We can pass an object reference variable of a superclass to a method that takes an object reference variable of a subclass, without casting. 1.6 Which combination of the following methods (inside the same class) would not cause a compile—time error (assume “...” represents valid statements)? 1.boolean equals(int x, int y) {...} 2. int equals(string x, String y) {...} 3. float equals(String y, String 2) {...} 4.boolean equals(String x, String y, String 2) {...} A. 2 and 3 B. 2, 3 and 4 C. l, 2, 3 and 4 D. 1, 3 and 4 E. Any combination of these methods would cause a compile-time error. 1.7 Which one of the following statements is correct? A. A class can extend more than one class. B. An interface can implement other interfaces. C. A class can implement more than one interface. D. A class can implement another class. E. Only abstract classes can implement interfaces. COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 3 of 13 School of Computer Science — Faculty of Science — McGill University 1.8 What is required for a class to implement an interface and have the possibility to be instantiated? A. Declare that it implements the interface B. Provide definitions for all the methods declared in the interface C. Extend the interface as an abstract class D. A, B and C E. A and B 1.9 Which one of the following statements is the correct way of creating an array? A. int count = new count[5]; B. String names={ "John","Bush","Jennifer","Yomosuki","Dung" }; C. boolean permit: new boolean[lO]; D. double measurement 2 new double(10); E. long summits; 1.10 What is the problem with the following code (assume “...” represents valid statements)? public class classA { public int function(int x) { } } public class classB extends classA { public int function(int x) { } public double function(double x) { } } public class Main { static int function(classA c) { c.function(0.0); } public static void main(String args) { classB B = new classB(m); function(B); } A. There is no problem. B. Main . function is passed an argument of type classB, but expects an argument of UpeclassA. C. O . 0 needs to be cast into an int to be passed to classA. function (int). D.Main.functionudHlookforclassA.function(double)andudflnotfindit E. Main. function calls a non—static method classA. function. COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 4 of 13 School of Computer Science — Faculty of Science — McGill University 1.11 An abstract class named Game has two subclasses, IndoorGame and OutDoorGame. The constructor for each class requires a String argument representing the name of the game. If you declared an array with the statement Game [ ] game = new Game [10] ; then which one(s) of the following assignments is/are valid? A. game [0] = new Game ("Chess"); B. game [1] = new OutDoorGame ( "Soccer"); C. game [2] = new IndoorGame ("Doom"); D. A, B and C E. B and C 1.12 What is the output of the following program? class A { public A() { System.out.print("The constructor of A is invoked"); } } class B extends A { public B() { System.out.print("The constructor of B is invoked"); } } public class C { public static void main(String args) { A a = new B(); } } A. None, there will be a compile-time error because the reference variable and object types are different. E.The constructor of B is invoked C.The constructor of A is invokedThe constructor of B is invoked D.The constructor of B is invokedThe constructor of A is invoked E.The constructor of A is invoked 1.13 What is/are the advantage(s) of using exceptions? A. Separating error handling code from regular code B. Increasing the efficiency and decreasing the running time of programs C. Grouping and classifying different error types D. A, B and C E. A and C COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 5 of 13 School of Computer Science — Faculty of Science — McGill University 1.14 What do we mean when we say that a method threw an exception? A. The method performed action that caused program to enter an infinite loop B. The method did not compile and caused a crash C. An error occurred in the method and lead to the creation of an exception object that was passed to the run-time system D. The method had a “throws” clause in its header, and it performed an action that caused the program to crash E. An exception object was created somewhere else in the program but caught in the method therefore saving the program from crashing 1.15 Consider the following method: public int foo(int a, int i, if(i == a.length) return w; else if(a[i] >= w) return foo(a, i+l, a[i]); else return foo(a, i+1, w); } What does this method do if we call it as: int a = {4,8,1,0,7,15,—4}; int answer = foo(a, O, a[0]); A. Sorts the array in increasing order B. Finds the smallest element in array C. Returns the first element in array D. Finds the largest element in array E. Returns the position of the smallest element COMP 202 — Daoudi, Lanctot, Naeem int w){ Version 1 — page 6 0f 13 School of Computer Science — Faculty of Science — McGill University SECTION II: SHORT PROBLEMS (30 points) 11.1 (10 points) The Hexadecimal number system (base 16) is a different number system than the decimal number system (base 10). It is analogous to the binary number system (base 2), except that instead of only 0 and 1 allowed as digits, there are sixteen allowed digits: 0—9, and A—F. The value of a hexadecimal number is obtained similarly to the way a binary number is: a sum of digits, each multiplied by a power of 16, where A is 10, B is 11, C is 12, D is 13, E is 14, and F is 15. So, for example, the value of the hexadecimal number 3AB2 is 2 - 160 + 11 - 161 + 10 ~ 162 + 3 - 163. 4P45 is an invalid hexadecimal number because P is an invalid digit. Write a class HexConverter that contains two methods: a method that converts a hexadecimal number (as a string) to an integer, and a main method which prints out “Please enter a hexadecimal number”, accepts input, uses the method to convert the input, and prints out the value of the hexadecimal number in decimal. Hints: recall that Double.parseDouble (String str) converts a string to a double; Character . isDigit (char c) returns true ifc is a digit (0, I, 2 ..., 9) and false otherwise. COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 7 of 13 School of Computer Science — Faculty of Science — McGill University 11.2 (10 points) The following program is supposed to take a filename passed as the first command-line argument, and print its contents in a file whose name is passed as the second command-line argument. You may assume that the first filename refers to a text file, if it is not valid the program should automatically use a default filename called data.txt. import java.util.Scanner; import java. _____J public class TextFileCopier { public static void main (String args) { File inFile = new File ( ); Scanner 50; FileWriter fw; BufferedWriter bw; PrintWriter outFile = null; try { so = } (FileNotFoundException fnf) { try { fw = new FileWriter (_________________*___*,true); bw = new BufferedWriter (____—); outFile = new PrintWriter (_____________________); while (sc.hasNextLine()) { String line = __ ____; outFile .println (__—_); } catch ( _____ e) { System.err.println(e.toString()); } { if (_________________l != _____________________} outFile.close(); COMP 202 — Daoudi, Lanctot, N aeem Version 1 - page 8 of 13 School of Computer Science — Faculty of Science — McGill University Note: Answer this question on the exam paper itself. a) (7 points) Complete the program so that it compiles and does what it is supposed to do. b) (3 points) Assume we were to remove the second t ry block altogether, so that this part of the method would look like (including the missing parts): fw = new FileWriter ( ,true); bw = new BufferedWriter ( ); outFile = new PrintWriter ( ); while (sc.hasNextLine()) { String line = __—:' outFile.println (—); } if ( != _) outFile.close(); Would the program still compile? If so, what is the advantage of having this try block? If not, rewrite the line(s) that would make it compile (you cannot use another try block)? COMP 202 — Daoudi, Lanctot, Naeem Version 1 — page 9 of 13 School of Computer Science — Faculty of Science — McGill University [1.3 (10 points) Write a method that accepts an array of strings and a "shift value" as parameters, and prints out the items in the array shifted by the “shift value” and separated “*5, by . Example: We have a string array a = {"Rachel", "Ross", "Chandeler","Monica","Phobie", "Joey"} If the "shift value" is 1, the result will be: Joey*Rachel*Ross*Chandeler*Monica*Phobie If the "shift value" is 2, the result will be: Phobie*Joey*Rache1*Ross*Chandeler*Monica COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 10 of 13 School of Computer Science — Faculty of Science — McGill University SECTION III: PROGRAMMING PROBLEMS (40 points) 111.1 (20 points) Tracking wildlife You are required to implement a tracking system for a field biologist who tracks animals on a delimited area. This area can be viewed as a rectangular grid where each cell would represent a sector, or a basic unit of location. This biologist tracks 2 species of animals: Springboks and Hyenas. Every animal always has a direction it’s heading in (East, West, North or South) and a unique numeric ID (a positive integer). For each Springbok, the biologist keeps track of its weight (a floating point number) and the size of its herd. For each hyena, the biologist keeps track of its biting pressure (a floating point number), its sex, and whether it is an alpha (dominant in the pack) individual or not. On top of implementing the classes to represent the animals, you are required to implement a class to represent the research field (the size of which is determined when it is first created, and does not change after that) along with the operations most often used by the biologist on the field: — Adding an individual of a particular species on a specific sector. - Removing an individual of a particular species from the field, by using its ID. - Updating an individual's information, by giving its ID: the user would then be prompted to enter the updateable information for this animal (note that the sex of the hyena and the ID of an animal may not be changed). - Updating the location of an individual, by giving its ID and the new sector where it was last sighted. - Returning information on a particular sector, which would return the ID of the tracked individual located on that sector (note that a sector cannot hold more than one tracked individual), or an invalid ID (like a negative integer) if there is no tracked individual on that sector. - Returning all the information on a tracked individual (as a string of text, which should also include its location), by giving its ID. - Returning a text-based representation of the field (as a grid of single characters, where ‘H' or '8' represents a tracked individual of that species and 'X' represents an empty sector). Your classes should de designed so that your program could be extended to add more species of animals to track with minimum effort (i.e. by having to modify your existing classes as little as possible). COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 11 of 13 School of Computer Science — Faculty of Science — McGill University 111.2 (20 points) Arithmetic Expression Trees An expression tree is a data structure that represents an expression. A binary tree is a structure whose nodes have references to two other nodes: the left child and right child. The node at the top of the tree is called the root node, and any nodes whose references are both null are called leaf nodes. An arithmetic expression tree is a binary tree whose nodes hold one datum: a string whose value is either a binary arithmetic operator (“+”, “—”, “*”, “/”) or a numerical value (“3 4”, “23 . 71”, etc.). By construction, all leaf nodes in an arithmetic expression tree will hold numerical values and all non-leaf nodes will hold operators. Expression trees can be visualized. For example, the following: is an expression tree. The root node contains the “+” operator as a datum; its left child is the node which contains “7” as a datum and its right child is the node that contains “—” as a datum. There are two major operations defined on arithmetic expression trees: evaluation, and string representation. The string representation of the above tree is (7+ ( (8/6) —3) ) . This expression gets evaluated to the value 7+ (1 . 3333—3) = 5 . 3333. COMP 202 — Daoudi, Lanctot, Naeem Version 1 - page 12 of 13 School of Computer Science — Faculty of Science — McGill University Here is an interface that describes the node of an (arithmetic) expression tree: public interface AbstractTreeNode { public AbstractTreeNode getLeftChild(); public AbstractTreeNode getRightChild(); public void setLeftChild(AbstractTreeNode _node); public void setRightChild(AbstractTreeNode _node); public void setDatum(String _datum); public String getDatum(); /~k* If this is a leaf node, returns the value of the datum. Otherwise, recursively evaluates the value of the left sub—expression and * * * right sub—expression, returns the value of the operator applied to * the two values. * * Hint: Double.parseDouble(String s) converts s to a double. */ public double evaluate(); * Returns the expression as a string by recursively obtaining the * string representation of the left sub—expression, appending the * operator, recursively obtaining the string representation of the * right sub—expression and appending it to the string representation, * then returning the string. public String toString(); } Write two classes: ExprTreeNode which implements AbstractTreeNode, and TreeTest which is the driver program that tests the implementation. The test class should create an expression tree with at least 4 nodes, print out the string representation of it and the value it gets evaluated to. Note: you don’t need to read any input from the user. COMP 202 — Daoudi, Lanctot, N aeem Version 1 — page 13 of 13 ...
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 / 13

COMP202-2 - School of Computer Science — Faculty of...

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

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