COMP202-3 - STUDENT NAME STUDENT NUMBER Faculty of Science...

Info icon This preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
Image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: STUDENT NAME: STUDENT NUMBER: Faculty of Science FINAL EXAMINATION (Version 1) COMPUTER SCIENCE COMP202 INTRODUCTION TO COMPUTING I Examiner: Prof. M. Newborn Friday, December 10, 2004 Associate Examiner: Prof. G. Ratzer 2 PM. i 5 PM. Instructions: (1) All answers are to be written on these pages. (2) NOTE: The Examination Security Monitor Program detects pairs of students with unusually similar answer patterns on multiple-choice exams. Data generated by this program can be used as admissible evidence. either to initiate or corroborate an investigation or a charge ol‘eheating under Section 16 of the Code of Student Conduct and Disciplinary Procedures. (3) Calculators are not allowed. Books are not allowed other than translation dictionaries. Notes are not allowed. (4) Return these pages when you complete the exam. (5) This exam comprises 20 pages, including this cover page. Pages I 1, 13, 18, and 20 are blank. GRADING Short answer I uestions "III. M I13 .5 II -IIIII IIII Anal sis, short II'O rams, lon_ urograms and final grade —- FINAL RESULT -—- Weight COMP 202 Final Exam —- Page 1 Question 1: (1 point) In the space provided, express the decimal number 45 as a binary number. Binary number: Question 2: (2 points) If a picture were made up of 128 different colors, how many bits would be needed to store each pixel of the picture? Question 31 (2 points) The four integer data types in Java are (answer one of (a) — (e))3 (a) bit, byte, int, long (b) short, int, long, float (0) short , int, long, double (d) byte, short, int, long (e) byte, short, int, double COMP 202 Final Exam — Page 2 Question 43 (2 points) Which of the following statements are true? 1) A class is a blue print of an object; it reserves no memory space for data. 2) A constructor is a regular method with all its properties, except that it must have the same name as the class name. 3) A class can have at most one constructor. 4) Methods are shared among all objects of a class. 5) A private method can only be referenced within the class that it is declared in. (a) all are true ([3) all are true except 2,8 (0) only 2,4,5 are true (d) only 1,2,5 are true (6) only 1,3,4 are true Question 51 (3 points; 1/2 point for each correct answer) Fill in the (3 missing words in the following paragraph that discusses the Math class. The Math class provides a large number of basic mathematical functions that are helpful in making calculations. The Math class is defined in the package of the Java class . All methods in the Math class are methods (also called class methods), which means they can be through the name of the in which they are defined, without having to an object of the class first. COMP 202 Final Exam —— Page 3 Question 61 (2 points; 1 point for each correct answer) Assuming that a Random object has been created called generator, what is the range of the result of each of the following expressions? (a) generator . nextInt( 10 0) (b) generator.nextInt(10) — 5 Question 71 (3 points; each answer is worth 1 points) A variable or method in some class A can be accessed by all classes. A variable or method in some class A can be accessed by all classes in the same package and all subclasses of A. A variable or method in some class A can be accessed only from within A. Question 83 (2 points for the correct answer) Which one of the five statements given below makes the following sentence true when substituted into the blank space? A variable that is “local” to a Java method named A (a) can be modified by another method in the same class. (b) is usually declared as “final” in A. (c) is essentially the same as an “instance” variable. ((1) must be declared as a “private” variable. (6) can be an argument of a method invoked from A. Answer: COMP 202 Final Exam — Page 4 Question 93 (2 points for the correct answer) Which two of the five statements given below make the following sentence true when substituted in the blank space? A constructor is a special method that (1) initializes an object when the object is instantiated (2) instantiates an object when the object is initialized (3) is a method in every class (4) cannot be passed parameters (5) has:1“vonf’type (a) Answers (1) and (3) (b) Answers (1) and (4) (0) Answers (1) and (5) (d) Answers (2) and (3) (e) Answers (3) and (5) Question 101 (2 points for the correct answer — no partial credit) Provide in the box below the output produced by the following program ‘? public class abc { public static void main(String args) { for(int k = 0; k < 4; k++) { if( k > 0) k = 3; else k = 5; switch(k) { case 0: System.out.print(0); break; case 1: System.out.print(l); case 2: System.out. print(2); break; case 3: System. out. print(3); default: System. out. print(5); } } System.0utprintln(); } } COMP 202 Final Exam 7 Page 5 Question 113 (2 points) Suppose that you have the following class definitions class A { int x; A(int x) { thiS.X = x; } } class B { void p(int x, A obj) { X++; Obj.x++; } What will be printed by the following code fragment? A a new A(4); B b new B(); int x = 7; b-p(X. a) System.out.println("" + x + ”," + a.x); (a) (b) (C) (d) (e) LNGJHJOOHJ ‘H“"‘ U'IU'IU'In-bih Question 12: (2 points) Class B is defined as a subclass of A, and sh0uld therefore be declared as i a) class B imports A {...} b) class B extends A {....} c) class B abstracts A {m} d) subclass B {...} P“ declared inside class A *l 8) class B inherits A {...} COMP 202 Final Exam — Page 6 Question 131 (2 points, 1/2 point for each correct answer) Write following each of the five statements whether the statement is true (T) or false (F) regarding the term inheritanccl (a) A derived class can have more than one parent. (h) The Java keyword inherits is used to indicate that a subclass of the parent class is being created. (0) Variables declared in a child class cannot be seen by the parent class. ((1) A method in a child class can override an inherited method. Question 14: (2 points) What class is the parent class of the String class? Question 151 (3 points; V2 point for each correct answer) Fill in the 6 missing words in the following paragraph that discusses “Exception Propagation.” If an exception is not caught and handled where it , control is immediately returned to the method that the method that produced the exception. We can design our software so that the exception is caught and handled at this outer level. If it isn’t caught there, control returns to the method that called it. This process is called the exception. This propagation continues until the exception is caught and handled or until it is passed out of the method, which terminates the program and produces an exception message. To catch an exception at an outer level, the method that produces the exception must be invoked inside a block that has clauses to handle it. COMP 202 Final Exam m Page 7 Question 161 (2 points) Given the following class definitions, class Boss { int eyes; } class Employee { Boss myboss; Employee(Boss b) { myboss = b; } } What will be printed by the following code segment? Boss bl = new Boss(2); Employee el = new Employee(bl); Employee e2 = new Employee(b1); Boss b2 = e2.myboss; b2.eyes = 3; bl.eyes = l; System.out.println("" + bl.eyes + ”," + b2.eyes); (a) 1,3 (In) 2,3 &)33 (d) 1,1 @)22 Question 171 (2 points for the correct answer) What happens when the following code is executed: int[ ] A new int[lO]; for (int i = O; i (=10; i++) A[i] = i; System.out.println(“Array A was initialized”); Gfl'Array A has been initialized‘naprnmed (b)An+fiuepfionisthr0wn,and'Array A was initialized‘isprflned @)Anexoqfifin1mthrown,and'Array A was initialized'isniprnmed (d) All of the above COMP 202 Final Exam v Page 8 Question 181 (2 points for the correct answer) What is printed, given the following recursive method definitioni public int mystery ( int n) { if ( n <= 1) return (1); else return ( n + mystery (n—l) );} and the call: System.out.println(mystery (5)); (a) 5 (b) 25 (c) — 5 (d) l 5 (e) 2 0 (f) none of the above Question 193 (2 points, 1/2 point for each correct answer) In addition to lists, which of the following are usually called linear data structures? (answers Yes or No on the line following each term.) (a) stack (b) tree (c) graph (d) queue COMP 202 Final Exam — Page 9 Question 201 Analysis Problem 1 (6 points) What will the following Java code print when executed? class Cat { void meow() { System.out.println(”Meooow”); } void pet() { System.out.println("(I'm being petted)"); meow(); } } Class LazyFatCat extends Cat { void meow() { System.out.println("Stop bothering me“); } void nap() { meow(); super.meow(); } } public class ComicStrip { public static void main(String args) { Cat nermal = new Cat(); LazyFatCat garfield = new LazyFatCat(); nermal.meow(); nermal.pet(); garfield.meow(); garfield.pet(); garfield.nap(); ((Cat)garfield).meow(); COMPflDFdemmv—PngO Question 211 Analysis problem 2 (6 points) What will the following Java code print when executed? class Worker { void work() { System.out.println(“Working"); } } class Carpenter extends Worker { void work() { System.out.println("Hammering"); } } class Electrician extends Worker { void work() { System.out.println("Bzzzt... poof...”); } } class Roofer extends Worker { void climb() { System.out.println("Climbing"); } } class Construction { void build(Worker w) { w.w0rk(); if (w instanceof Roofer) ((Roofer)w).climb(); } public class Test { public static void main(String[} args) { Carpenter c = new Carpenter(); Electrician e = new Electrician(); Roofer r = new Roofer(); Construction site = new Construction(); site.build(c); site.build(e); site.build(r); COMP 202 Final Exam we Page 12 Question 221 Short programming problem 1 (8 points) Write a recursive function that searches an array of integers of size N and returns the number of integers equal to a target value. Show how you would call this method from the main on an array called myNumbers with 3 as a target value. COMP 202 Final Exam —— Page 14 Question 231 Short programming problem 2 (8 points) duplicates Write the code for the method duplicates (on the next page). The method: (1) is passed an unsorted array of integers, (2) keeps only one of each repeated non-zero values in the array while replacing the others by Os, (3) groups all of these unique values together at the front of the array, followed by all the zeros, (4) does not use any extra space other than a fixed number of variables (you cannot create a temporary array inside the method) Please write a brief description (a few sentences) of your algorithm: COMP 202 Final Exam — Page 15 The output for the Java program shown below should be: 1 2 3 7 10 8 9 O 0 0 0 0 0 0 O 0 0 public class Duplicates { public static void main(String args) { int numbers={l,2,3,1,0,0,7,10,8,9,8,1,3,l,l,l0,0}; duplicates(numbers); for(int i=0; i<numbers.length; i++) System.out.print(numbers[i] + " "); //write your method here COMP 202 Final Exam — Page 16 Quesfi0n24iLonglnpgnunnnngprdflenl1(16p0nms) Write a LinkedStack Class that implements the following Stack interface, using a linked list to store its elements. Note-' You may not use anypredefi'ned Java methods or packages. interface Stack { public boolean isEmpty(); public Object pop(); public Object peek(); public void push(0bject obj); COMP 202 Final Exam m Page 17 Question 25! Long programming problem 2 (16 points) We want to simulate a simple machine that tags bean cans for a food- processing factory. There are two types or classes of bean cansi normal and extra—hot. Each bean can, regardless of whether it is normal or not, has a price and a mark saying whether the can has been tagged or not. An extra—hot bean can has, in addition, a warning message. Any bean can, regardless of its type, can be tagged with a given price. The action of tagging a can means that the given price is assigned to it, and the can gets marked. When an extra—hot bean can is tagged, in addition to marking it and setting its price, it‘s warning message is set to "Extra hot." The machine itself can process any given bean cani processing a bean can performs the action of tagging the given bean can with any given price. The machine can apply the tag regardless of whether the given bean can is normal or extra~hot. Furthermore, if in the future a new type of bean can is added, the machine should still be able to tag it. The machine "automatically" determines which tagging action to apply depending on the type of can given. Write the necessary classes to represent the elements and actions described above. The design must follow the principles of object—oriented programming. COMP 202 Final Exam — Page 19 ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern