Practice Test Questions - Object-Oriented Programming and Design | C SC 335, Exams of Computer Science

Material Type: Exam; Class: Object-Oriented Programming and Design; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-qwi
koofers-user-qwi 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSc 335 Fall 2005 Practice Test Questions
Test 2: Tuesday, 25-Oct, 2005
This is not a test, but rather a collection of questions like the ones that will be on the test.
It is longer than the actual test will be.
Sincerely Rick Mercer in San Diego
1. Using the classes on the right, for each line of code to the left, write the output in the space provided.
Abba a = new Abba("Kim");
Bar b = new Bar("Devon");
Goo g = new Goo("Chris");
a.one(); // __________________________________
System.out.println("\n");
a.two(); // __________________________________
System.out.println("\n");
b.one(); // __________________________________
System.out.println("\n");
b.two(); // __________________________________
System.out.println("\n");
b.three();// _________________________________
System.out.println("\n");
g.one(); // __________________________________
System.out.println("\n");
g.two(); // __________________________________
System.out.println("\n");
g.three();// __________________________________
System.out.println("\n");
class Abba extends Object
{
private String id;
public Abba(String ID) {
id = ID + " ";
}
public String getID() {
return id;
}
public void one() {
System.out.print("A1 " + getID());
}
public void two() {
System.out.print("A2 ");
three();
}
public void three() {
System.out.print("A3 " + getID());
}
}
////////////////////////////////////
class Bar extends Abba {
public Bar(String id)
{
super(id);
}
public void one()
{
two();
}
public void three()
{
System.out.print("B3 " + getID());
}
}
////////////////////////////////////
class Goo extends Bar {
public Goo(String ID)
{
super("X" + ID + "X");
}
public void two() {
System.out.print("G2 ");
super.three();
}
public void three() {
System.out.print("G3 ");
}
}
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Practice Test Questions - Object-Oriented Programming and Design | C SC 335 and more Exams Computer Science in PDF only on Docsity!

CSc 335 Fall 2005 Practice Test Questions

Test 2: Tuesday, 25-Oct, 2005

This is not a test, but rather a collection of questions like the ones that will be on the test.

It is longer than the actual test will be.

Sincerely Rick Mercer in San Diego

1. Using the classes on the right, for each line of code to the left, write the output in the space provided.

Abba a = new Abba("Kim"); Bar b = new Bar("Devon"); Goo g = new Goo("Chris"); a.one(); // __________________________________ System.out.println("\n"); a.two(); // __________________________________ System.out.println("\n"); b.one(); // __________________________________ System.out.println("\n"); b.two(); // __________________________________ System.out.println("\n"); b.three();// _________________________________ System.out.println("\n"); g.one(); // __________________________________ System.out.println("\n"); g.two(); // __________________________________ System.out.println("\n"); g.three();// __________________________________ System.out.println("\n"); class Abba extends Object { private String id; public Abba(String ID) { id = ID + " "; } public String getID() { return id; } public void one() { System.out.print("A1 " + getID()); } public void two() { System.out.print("A2 "); three(); } public void three() { System.out.print("A3 " + getID()); } } //////////////////////////////////// class Bar extends Abba { public Bar(String id) { super(id); } public void one() { two(); } public void three() { System.out.print("B3 " + getID()); } } //////////////////////////////////// class Goo extends Bar { public Goo(String ID) { super("X" + ID + "X"); } public void two() { System.out.print("G2 "); super.three(); } public void three() { System.out.print("G3 "); } }

2. Use this page to answers questions on the next page

public class First { public void method2() { System.out.println("First2"); } public void method3() { method2(); } } public class Second extends First { public void method2() { System.out.println("Second2"); } } class Third extends Second { public void method1() { System.out.println("Third1"); super.method2(); } public void method2() { System.out.println("Third2"); } } public class Fourth extends First { public void method1() { System.out.println("Fourth1"); } public void method2() { System.out.println("Fourth2"); } }

In the table below, indicate in the right-hand column the output produced by the statement in the left-hand

column. If the statement produces more than one line of output, indicate the line breaks with slashes as in

"a/b/c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an

error, fill in the right-hand column with either "compiler error" or "runtime error" to indicate when the error

would be detected.

3. You are to rewrite a pair of classes using inheritance to avoid redundancy. The classes you will rewrite are

used to store account information for customers. An account includes a customer name and a list

f items that have been purchased. An item includes a name for the item and a price. Class item is declared as

follows:

public class Item {

private String name;

private double myPrice;

public Item(String name, double price) {

name = name;

myPrice = price;

public double getPrice() {

return myPrice;

public String toString() {

return name + " at $" + myPrice;

The account classes include methods for adding items, getting specific items (using the standard Java

convention of indexing starting at 0), getting the number of items, getting the overall bill and printing the

overall bill. There are two different account types. A standard account is charged a fixed service fee of $5 plus

the total of the items. A preferred account is not charged a service fee and receives a 10% discount off the total

of the items. Below are the definitions of the two account classes:

public class StandardAccount { private String name; private ArrayList items; public StandardAccount(String name) { name = name; items = new ArrayList(); } public void add(Item i) { items.addElement(i); } public String getName() { return name; } public Item get(int i) { return (Item)items.elementAt(i); } public int length() { return items.size(); } public double getBill() { double bill = 5.0; for (int i = 0; i < items.size(); i++) { Item next = (Item)items.elementAt(i); bill += next.getPrice(); } return bill; } public void print() { System.out.println("Standard Customer "

  • name); for (int i = 0; i < items.size(); i++) System.out.println(items.elementAt(i)); System.out.println("Total bill = $"
  • getBill()); } } public class PreferredAccount { private String name; private ArrayList items; public PreferredAccount(String name) { name = name; items = new ArrayList(); } public void add(Item i) { items.addElement(i); } public String getName() { return name; } public Item get(int i) { return (Item)items.elementAt(i); } public int length() { return items.size(); } public double getBill() { double bill = 0.0; for (int i = 0; i < items.size(); i++) { Item next = (Item)items.elementAt(i); bill += next.getPrice(); } bill = bill * 0. return bill; } public void print() { System.out.println("Preferred Customer "
  • name); for (int i = 0; i < items.size(); i++) System.out.println(items.elementAt(i)); System.out.println("Total bill = $"
  • getBill()); } }

Obviously there is a lot of redundancy in these two class definitions. You are to eliminate the redundant code

by creating a superclass for these two classes called Account. You should appropriately declare any method as

abstract if the definition of the method varies from one subclass to the next. All data fields of your classes must

be declared to be private, although you can introduce protected methods. You are to write the common

superclass and the two subclasses (answer in different location)

5. Describe a general situation in which inheritance is useful and the reason(s) why you would use this feature

of object-oriented technology

6. In this problem, you will use the object-oriented design pattern Strategy to begin a Background manager

strategy consisting of just two different (and rather simple, and silly) BackGround managers (these were made

to be intentionally simple). One background manager draws many ovals in a specified width and height (that

code is actually given). The other background manager draws a rectangle with a 10 pixel border in a

specified color. Here is a main method that uses the two Background strategies you will implement along with

the graphical output. You will be implementing several classes described after this page. The call to

3a. Write Background as if it were in its own file. 3c. Complete OvalBackground around the method given as if it were in its own file. The single drawBackground method is provided below to remind you of methods that will help you complete the other Background class. public void drawBackground(JFrame f) { Graphics g = f.getContentPane().getGraphics(); Graphics2D g2 = (Graphics2D)g; // Clear old background g2.setPaint(f.getBackground()); g2.fill(new Rectangle(f.getHeight(), f.getHeight())); // Clear old background g2.setColor(my_color); for(double r = 5.0; r < f.getHeight() - 40.0; r += 5.0) g2.draw(new Ellipse2D.Double(r, r, my_width, my_height)); } } // end class OvalBackground

BackGroundFrame

setBackgroundShape

( bg: Background )

<>

Background

drawBackground()

RectangleBackground OvalBackground

JFrame

3d. Completely write SquareBackground as if it were in its own .java file. 3e. Describe what you would have to do to add a third background strategy to your system. Do NOT write the new strategy.

  1. The composite design pattern composes objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Here is the general form of the Composite Design Pattern:

4b. Using your UML diagram write the abstract class

4c) Using your UML diagram write the Primitive and Composite classes

  1. Which code is the better design A or B ___ Why?

// A

if (isSpecialDeal()) {

total = price * 0.95;

send();

else {

total = price * 0.98;

send();

// B

if (isSpecialDeal())

total = price * 0.95;

else

total = price * 0.98;

send();

6. A Networking Question will be posted sometime Friday, 21-Oct

This question will be on the test with a different set of final projects from which to choose.

7. Write 1 above the final project you would like to do. Write 2 above your second choice. This will allow us to

establish the three final project possibilities by Thursday's class (MOO could be a social network).

___ ___ ___ ___ ___ ___

Mud or Texas Hold-Em Monopoly Pacman Scorched Earth Clue (Networked and AI)

MOO