Q5soln - cs 302‘ Fall 2006 Quiz 5(10 points Solution Answers must be hand-written Show all your work on the quiz Question 1[4 points Given the

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

Unformatted text preview: cs 302‘ Fall 2006 Quiz 5 (10 points) Solution Answers must be hand-written. Show all your work on the quiz. Question 1 [4 points] Given the following two classes: public class Item { private int code; public Item(int n) { code = n; } public int getCode() { return code; } public void modify() { Code += 3; } public String toString() { return "Item" + code; } } public class Subltem extends Item { private int id; public SubItem(int n) { super(n + 1); id = n * 2; } public void modify() { id *= super.getCode(); } public String toString() { return super.toString() + "[" + id + "J";} } What is the output for the following lines of code? Iteml] data = new Item[4]; for (int i = i < data.length; i++) if (i % 2 = O) data[i] new Item(i); else data[i] = new Subltem(i); for (int i = O; i < data.length; data[i].modify(); for (int i = O; i < data.length; System.out.println(data[i]); ll ~. Answer: Item3 Item2[4] ItemS Item4[24] Continues on back Question 2 [6 points] Consider the UML diagram below. Each type lists only the additional methods, not its inherited or implemented methods. Data members are not shown; you should assume that all data members are private. Private methods are also not shown. Each type may be a class or an interface. There is a UML symbol table on p. 619 of the textbook that you may find useful for answering these questions. TypeB Wis dtwg‘oxm tells wag — Type D extenivs Twila/C A <{.e.,'T\lpci3 is a (winches "' CK'JFQAAS Gabi lleflfNan'tf) + methodD() Consider the following Java class: + methodC () public static void main<StringH args) { 'TYPQ’D \f\\\9(\\"’i‘x$ Ntde A) ego} flpeC/ infinite) mailxodijb CAN) (mew/(n.9,th W’l'l'wii A public class CheckTypes { TypeA itemA; TypeB itemB; TypeC itemC; TypeD itemD; // HERE } mandala») Omit Whoa C } Consider inserting code at the place marked HERE (i.e., replacing the entire comment with the code). For each of the following pieces of code, if the code is inserted at the place marked HERE, would the resulting CheckTypes class compile without error? The answer choices are Yes or No Where Yes means the CheckTypes class would compile without error. N 0 means the CheckTypes class would not compile Without error. Yes A. itemA = new TypeC(); This would be ok if the second line was ((TypeC) itemB) .methodA(); No B. itemB = new TypeC(); itemB.methodA(); TypeC is a superclass of TypeD so we can't call a subclass method on a superclass object No C. itemC = new TypeC(); itemC.methodD () ; Yes D. itemC = new TypeD(); itemC.methodA(); No B. itemD = new TypeA() ; TypeA is an interface; interfaces can't be instantiated Yes F. itemD = new TypeD(); itemD.methodC () ; ...
View Full Document

This note was uploaded on 03/27/2008 for the course CS 302 taught by Professor Willbenton during the Fall '07 term at Wisconsin.

Page1 / 2

Q5soln - cs 302‘ Fall 2006 Quiz 5(10 points Solution Answers must be hand-written Show all your work on the quiz Question 1[4 points Given the

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

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