COMP202-1

COMP202-1 - School of Computer Science e Faculty of Science...

Info iconThis preview shows pages 1–10. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: School of Computer Science e Faculty of Science — McGill University COMP~202 * Introduction to Computing 1 Final Exam — Version 1 April 20, 2005 Examiners: Marc Lanctot, Ernesto Posse, Alex Batko Associate examiner: Joseph Vybihal Last name First name Id number “__ Section :I 1 (Marc Lanctot) D 2 (Ernesto Posse) D 3 (Alex Batko) Instructions 0 No notes, notebooks, textbooks, calculators, cell phones7 pagers, laptops or hand-held computers are allowed in this exam. For section 1 (multiple choice) mark your answer in the front page. The rest of the questions are to be answered in the booklet. Read the questions carefully. Language translation dictionaries are permitted. Answers may be given in either English or French. Grading Section 1: Multiple choice Section 1 mark: /20 Question Your answer Question Your answer Mark Section 2 mark: / 4U COMP-202, Lanctot, Posse, Batko Q1 Section 3 mark: / 40 Total: _/190 Page 1 out of 10 School of Computer Science — Faculty of Science — McGill University Terminology reference table Term l Synonyms/Meaning Object Object creation, creating an instance of a class Attribute, non—static variable, instance data Static variable Non—static method Static method Method header Method call, sending a message to an object (normal methods), asking an object to perform a task (normal methods). Instance Object instantiation Instance variable Class variables Normal method Class method Method signature Method invocation ) Argument Parameter ) Base class Parent class, super class ) Derived class | Child class, subclass m l Inherits from ) Extends 1 Multiple choice Question 1 Which of the following creates an object? a) class A { String s; } b) int n = 1729; c) n = gen.nextInt(); (1) String getNameO e) Scanner 3 = new Scanner(System.in); Question 2 Which of the following is true? a) A class is the same thing as an object b) A class defines a data—type c) All objects in a class have the same state d) The values of the attributes of a class are shared by all its instances e) A method is a class with parameters Question 3 Suppose that there is a class BankAccount which has an attribute balance, and a class CheckingAccount that extends BankAccount and has an attribute monthly_fee. Which of the following statements is false? a) CheckingAccount cannot extend any other class. b) If balance is shadowed (redefined in CheckingAccount,) then it is still possible to access balance from BankAccount. c) It is not possible to access monthlynfee directly in class BankAccount. d) A BankAccount object is the same as a CheckingAccount object where the monthlyjee is 0.0. e) If there is a class SavingsAccount which extends BankAccount and you modify the balance of a CheckingAccount object, then the balance of SavingsAccount is not updated. COMP—202, Lanctot, Posse, Batko Page 2 out of 10 School of Computer Science 7 Faculty of Science 7 McGill Universityw Question 4 Which of the following is true? a) Both an interface and an abstract class can be instantiated b) Neither an interface nor an abstract class can be instantiated c) An interface can be instantiated, while an abstract class cannot d) An interface cannot be instantiated, while an abstract class can e) Abstract classes cannot have subclasses Question 5 Which of the following is false? a) If a method expects a parameter of type T, then it can be passed as argument any instance of the parent class of T. b) If a method expects a parameter of type T, then it can be passed as argument any instance of T or any of its subclasses. c) If a method has a return type T, then it can return an instance of a subclass of T. d) If v is of type S, and S is a subclass of T, then the casting (T)v is possible. e) If v is of type T, and S is a subclass of T, then the casting (S)v is possible, provided that v has been assigned a reference of type S, Question 6 Consider the following program fragment: Scanner scanner — new Scanner(System.in); int num = 0, sum = 0; try { while (true) { num = scanner.nextInt(); if (num == —1) { break; } else if (num < O) { throw new ExceptionO; sum = sum + num; } } catch (Exception e) { System.our.println(“00ps! ” + e); } System.out.println("sum = ” + sum); // // // // // // // // // // // // // stmt stmt stmt stmt stmt Stmt stmt Stmt stmt stmt stmt stmt stmt \lO‘SU'Ia'bCAJMI—L LOCI) 10 11 12 13 Suppose that this fragment is executed, and the user types 34, and then types -82. In which order will the statements be executed? a) 1, 2, 3, 4, 11, 12, 13 b) ) ) ) ('3 ,1U,4,5,6,7,11,12,13 10,4,5,6,8,9,11,12,13 ,8,9,10,11,12,13 CDC-L- COMP—202, Lanctot, Posse, Batko Page 3 out of 10 School of Computer Science — Faculty of Science — McGill University Question 7 It is a good idea to make attributes of a class private because: a) Because making attributes public forces them to be shared by all instances of its class. b) Because they can be inherited. 0) Because private attributes are accessible from other classes, allowing other classes to modify the state of an object any time. d) Because public attributes are accessible from other classes, which could modify the state of an object in some unwanted way. e) Because otherwise we could define only public and protected methods in the class. Question 8 In large applications it is often desirable to have general helper functions that do not implement actions performed on, 01' by objects. That is, in addition to having normal methods it is useful to have object-independent methods that perform general tasks. For example, in a statistical application, a helper function could be a method that returns the average of an array of integers. Since helper functions do not act on objects, it is unnecessary to create an object to perform the task. What is the best way to organize a program that uses helper functions and avoids creating objects of the class(es) containing helper functions? 21) Use an array of helper methods. b) Define an abstract class (or classes) containing the helper functions implemented as abstract methods. (3) Define one or more helper classes containing the helper functions implemented as static methods. d) Define a base class containing the helper methods, and making sure that all other classes extend this base class. e) Define a helper interface, making sure that any class that needs a helper function implements that interface. COMP—202, Lanctot, Posse, Batko Page 4 out of 10 School of Computer Science w Faculty of Science ~ McGill University 2 Short problems Question 1 Consider the following class definition: class Person { String name; Person(String n) { name = n; } void wakeup(Brain x) { System.out.print( name ); x.activate( “starts” ); } } public class Test { public static void main(String args) { Person a = new Person(“Newton”); Brain b = new Brain(); a.wakeup(b); } } rThis program is missing a class Brain. Write a definition of class Brain so that the program prints “Newton starts the day? COMP—202, Lanctot, Posse, Batko Page 5 out of 10 School of Computer Science — Faculty of Science 7 McGill University Question 2 What will the following print if method testi in class Test is invoked? class Banner { String message; void set(String w) { message = w; } String getC) { return message; } } class Test { Static String w; static void test1() { w 2 “1 ”; String x = “2 ”; Banner y = new Banner(); y.set(“3 ”); String 2 = new String[2]; z[0] = “4 ’U z[1] = “5 ’3 process( x, y, z ); System.out.print1n( w + x + y.get() + zEO] + z[1] ); } static void process(String a, Banner b, StringE] c) { System.out.print1n( w + a + b.get() + CEO] + c[l] ); w = “6 “; a = “7 ”; b.set(“8 ”); CEO] = “9 ’U COMP—202, Lanctot, Posse7 Batko Page 6 out of 10 School of Computer Science — Faculty of Science — McGill University Question 3 Suppose that you are developing a program to keep track of all the items in stock at some store. There are many different kinds of items, but all of them have an ID number, a name and a cost. Therefore, each item belongs to a class which implements the following interface: public interface IdentifiableItem { public int getIdO; public String getNameO; public double getCostO; } Given an array of Identifiableltems, an item a; is said to be unique if there is no other item in the array with the same name and ID as 37. Write a method that recoives an array of Identifiableltems, and returns the number of unique items. That is, duplicate items should not be counted. COMP—202, Lanctot, Posse, Batko Page 7 out 0f10 School of Computer Science — Faculty of Science — McGill University Question 4 Write a method called merge that takes as argument two sorted arrays of integers a and b, and returns a sorted array of integers that combines all of the values stored in a and b. For example, if a is [3, 4, 7, 7, 9, 10, 13] and b is [2, 2, 2, 5, 8, 9, 11, 11] then the method returns an array [2, 2, 3, 4, 5, 7, 7, 8, 9, 9, 10, 11, 11, 13]. Hint: take advantage of the fact that the two given arrays are already sorted. School of Computer Science 7 Faculty of Science 7 McGill University 3 Programming problems Question 1 A Matryoshkn doll is a Russian nesting doll. A set of Matryoshka dolls consists of a wooden figure which can be pulled apart to reveal another similar but of course slightly smaller figure inside. The smaller figure will in turn have another figure inside it, and so on, such that there are usually six or more nested dolls in a set. In other words, a Matryoshka doll has a Matryoshka doll inside it. Implement this recursive concept of a Matryoshka doll by writing a class MatryoshkaDoll. A lVlatryoshka doll of size n has at (stores a) Matryoshka doll of size n w 1 inside it. The constructor of this class must take n the number of dolls in the set as an argument, and it must create the nested set of dolls by creating a Matryoshka doll with n — 1 dolls inside, or no dolls if n = D. The class should also have two additional methods; one named count that recursively counts and returns the number of dolls in the set; and one named toString that recursively traverses the set of dolls and returns the word "pretty" for each doll. Using this class, the following statements... MatryoshkaDoll prettyDolls = new MatryoshkaDollCS); System.out.println( "Number of dolls: ” + prettyDolls.count() ); System.out.println( "My pretty dolls: " + prettyDolls ); ...should produce the following output: Number of dolls: 5 My pretty dolls: pretty pretty pretty pretty pretty You do not need to write a driver class with a main method for this problem. School of Computer Science 7 Faculty of Science 7 McGill University Question 2 Many computer applications deal with representations of geometrical shapes, such as circles, triangles, rectangles, etc. Suppose that you are writing an application that deals With such geometrical Shapes. Initially the application deals only with circles, rectangles and composite shapes. But, new types may be added later. 0 A rectangle is a shape that has a width and a height, and the location (2:,y) of its top—left corner (for simplicity, assume that shapes are aligned with the X and Y axis, and not rotated.) o A circle is a shape that has a radius, and the location (:13, y) of its center. a A composite shape is a shape that has up to 100 shapes. It can contain any shape: circles, rectangles and other composite shapes. It should be possible to add a shape (any kind of shape) to a composite shape. 0 Any shape has a, bounding box. The bounding box of a shape is the smallest rectangle that contains the shape. Write a set of classes to represent these shapes, such that for any shape it is possible to ask the shape what is its bounding box. It should be possible to define new types of shapes so that your classes work without modi- fications. Hint: it might be easier to compute the bounding box of a composite shape whenever you add a new shape to the composite. The composite shape asks the shape being added which is its bounding box, and using this, it updates its overall bounding box. You can use arrays or any other appropriate collection you want. You may use the static methods min and max from the Math class, that compute the smallest (resp. largest) of two given numbers. Their signatures are public static double min(double a, double b) public static double max(doub1e a, double b) You do not need to write a driver class with a main method for this problem. COMP-202, Lanctot, Posse, Batko Page 10 out of 10 ...
View Full Document

Page1 / 10

COMP202-1 - School of Computer Science e Faculty of Science...

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

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