CSc 335 Practice Test 1 Answers

1. At the conceptual level, an object is a set of __RESPONSIBILITIES__

At the implementation level, an object is __METHODS and DATA__

2. The same method or interface may be realized in different ways. The actual method that executes is determined at runtime and is dependent on the type. Different classes can be treated as the same type: Comparables or ActionListeners for example.

3. __inheritance__ ___interfaces__ (2pts)

4. _true_ (2pts)

5. Place an X by the name of any interfaces in the Java collection framework in the java.util package? (6pts)

_X_Collection ___ArrayList _X_Queue _X_Set _X_Map __SortedArrayList

6. Which goes into the top compartment (rectangle) of a class diagram of a single class? (2pts)

___Attributes(instance variables) __X_Class Name ___Operations (methods)

7. Can one Java class implement more than one interface? yes or no ___YES_____ (2pts)

8. What is better, high cohesion or low cohesion? low or high ___HIGH_____ (2pts)

9.

true

3

5

1

10.

null

[A, C]

[B, D]

A

D

11. Write the output generated by the same code when aNumberMaybe is first "123" and then "NOgOOD" (6pts)

String aNumberMaybe = "123";
--
oo / String aNumberMaybe = "NOgOOD";
--
++

12

Interfaces have no method bodies {}
Interfaces have no instance variables

Interfaces have no constructor: can not be instantiated

13.

NumberListener listener = new NumberListener();

inputField.addActionListener(listener);

}

private class NumberListener implements ActionListener {

public void actionPerformed(ActionEventae) {

try {

doublenum = Double.parseDouble(inputField.getText());

myLabel.setText("You entered: " + num);

}

catch (NumberFormatExceptionnfe) {

myLabel.setText("!!ERROR");

}

}

}

}

14a) Candidate Objects and Major Responsibility

BookStore: Coordinates Activities (Check out student, collect payment, update inventory)

Student: Keeps track of CatCard number, address, sales confirmation order

ShoppingBasket: Keeps track of desired purchases

Item: one the books that can be purchased

Shelf: Shows books for purchase, with browse/search

Inventory: Keeps inventory levels of all items

14b)12pts

Possible grading criteria for a 12 point question

+3 Has the same classes as candidate objects

+1 Rectangles around each class

+2Each class has at least one operation

+2Almost every class has at least one or more attributes (one is enough)

+2 Reasonable associations (lines where a solid line is a dependency, dotting not required) exist

+2Hastwo or more reasonable multiplicityadornments (1 or * usually are possible)

One Answer others are possible(a few word processor additions were made to get 100% on this question)

15a) public interface Command {

public boolean execute();

public boolean undo();

}

15b)

public class BorrowCommand implements Command {

protected Borrower theBorrower;

protected Book theBook;

publicBorrowCommand(Borrower aBorrower, Book aBook) {

theBorrower = aBorrower;

theBook = aBook;

}

// When the Return button is clicked, execute this code

public boolean execute( ) {

System.out.println( theBorrower + " attempts to BORROW " + theBook );

if(!theBook.isAvailable( ) ) //The book is not available. Cannot borrow it.

return false;

theBook.borrowBook( );

return true;

}

public boolean undo( ) { // a return

System.out.println(theBorrower + " undo a Borrow" + theBook);

if (theBook.isAvailable()) //The book is not borrowed. Cannot return it.

return false;

// The borrower should no longer hold the returned book

if (!theBorrower.removeBook(theBook)) //The book is not borrowed by this subscriber.

return false;

theBook.returnBook();

return true;

}

}

16a. Iterator

Know the others we covered or are on the test like Command.

Also Strategy, Observer, Factory, Singleton, Decorator

17. It is more maintainable

18. TBA

19a. b

19b. b

19c. a

19d. b

20. TBA, a surprise question from lecture or reading

1