Practice Test 1 - University of Arizona



C Sc 335 Practice Test Fall 13 Section Leader ______ Name ________________________ 150 pts 1. Write the correct term, either "RESPONSIBILITIES" or "METHODS and DATA" in the blank space (2pts)At the conceptual level, an object is a set of __________________________At the implementation level, an object is a set of ___________________________2. Describe the essence of polymorphism. (3pts)3. List one Java structure to provide polymorphic messages ____________ (2pts)4. If you change ArrayList to LinkedList, will the following code still compile? true or false _____ (2pts) ?List<String> list = new ArrayList<String>(); ? list.add("b"); ? list.add("a"); ? list.add("c");? Collections.shuffle(list);5. Place an X by the name of any interface in the Java collection framework in the java.util package? (6pts) ___Collection ___ArrayList ___Queue ___Set ___Map __SortedArrayList6. Which goes into the top compartment (rectangle) of a class diagram of a single class? (2pts) ___Attributes(instance variables) ___Class Name ___Operations (methods)7. Can one Java class implement more than one interface? yes or no ________ (2pts)8. What is better, high cohesion or low cohesion? low or high ________ (2pts)9. Write the output that appears when this code runs. (4pts) ArrayList list = new ArrayList(); list.add(new Integer(3)); list.add(new Integer(5)); list.add(new Integer(1)); list.add(new Integer(4)); Iterator itr = list.iterator(); System.out.println(itr.hasNext()); System.out.println(itr.next()); System.out.println(itr.next()); System.out.println(itr.next()); 10. Write the output that appears when this code runs. (5pts) HashMap<Integer, List<String>> map = new HashMap<Integer, List<String>>(); List<String> one = new ArrayList<String>(); List<String> two = new LinkedList<String>(); one.add("A"); one.add("C"); two.add("B"); two.add("D"); map.put(1, one); map.put(2, two); System.out.println(map.get(0)); System.out.println(map.get(1)); System.out.println(map.get(2)); System.out.println(map.get(1).get(0)); System.out.println(map.get(2).get(1)); 11. Write the output generated by the same code when aNumberMaybe is first "123" and then "NOgOOD" (6pts)String aNumberMaybe = "123";try { System.out.println("--"); double num= Double.parseDouble(aNumberMaybe); System.out.println("oo");}catch (NumberFormatException nfe) { System.out.println("++");}String aNumberMaybe = "NOgOOD";try { System.out.println("--"); double num = Double.parseDouble(aNumberMaybe); System.out.println("oo");}catch (NumberFormatException nfe) { System.out.println("++");}12. List 1 difference between a Java interface and a Java class (2pts)13. Finish this program with a graphical user interface that will allows use to enter text into a field and be told whether or not that text represents a valid number. If the input is not a number, put !!ERROR in the middle. Hint use Double.parseDouble(String) that throws an exception if the argumnet isn't a valid number. (15pts) // Assume all imports existpublic class NumberFrame extends JFrame { public static void main(String[] args) { new NumberFrame().setVisible(true); } private JLabel heading = new JLabel("Enter a number below"); private JLabel myLabel; private JTextField inputField; public NumberFrame() { setTitle("Numbers"); setSize(170, 100); setDefaultCloseOperation(EXIT_ON_CLOSE); myLabel = new JLabel("Nothing yet ..."); inputField = new JTextField(); add(heading, BorderLayout.NORTH); add(myLabel, BorderLayout.CENTER); add(inputField, BorderLayout.SOUTH);14. The UofA Bookstore has decided that there is just too much traffic in the bookstore at the beginning of each semester. In an effort to reduce in-house traffic, the bookstore has decided to implement an online storefront where students can purchase all of their books online and just pick them up sometime after they’ve been purch-ased. You are to do some analysis and come up with a model for the bookstore’s new online front. User Stories:A student may remove 1 to many items from the shelf and place them into a shopping basketA student should be able to remove items from a shopping basket and place them back on the shelfA student should be able to purchase the items in their shopping basketTo check out, a student must give their shopping basket to the cashier (there is only one cashier)The cashier creates an order that consists of a item, quantity, and a price based on the item’s ISBNIf the CatCard has enough money then the total amount will be deducted and the items will be removed from inventory, a claim check confirmation for the order will be sent to the student’s email addressIf the CatCard funds are insufficient, place all back on the shelf14a) List the 4 most important objects to model this system along with the major responsibility: 12ptsCandidate Object Major Responsibility14b) Draw a UML class diagram showing all of your candidate objects and any relationships between them. Show inheritance relationships, interface implementation, or general association such as dependency by drawing a line. Write any multiplicity adornment you can think of. You will likely have 1, and or * in a few places at least. Each classes needs the class name and at least one appropriate method (no attributes needed for a perfect score). 12 pts.15. The Command design pattern encapsulates the concept of a command into an object. You can save it for later execution. In the following code, six commands of two different types are constructed and saved in a Vector. Later on, the execute message is sent to all six Command objects. The two classes that you will be asked to write are highlighted in boldface: Command and BorrowCommand. To save time, do not write ReturnCommand.// Have two borrowers and three books to store in the two Command objectsBorrower joe = new Borrower("Joe");Borrower kim = new Borrower("Kim");Book b0 = new Book("Pascal");Book b1 = new Book("Java");Book b2 = new Book("c++");Command c1 = new BorrowCommand(joe, b0); // Construct a Commmand so Joe borrows PascalCommand c2 = new BorrowCommand(joe, b1); // Construct a Commmand Joe borrows JavaCommand c3 = new BorrowCommand(kim, b2); // ...Command c4ThatFails = new BorrowCommand(kim, b0); // Nothing will happen later with execute()331470068580Output:Joe has [] attempts to BORROW Pascal[Joe has [Pascal], Kim has []]Joe has [Pascal] attempts to BORROW Java[Joe has [Pascal, Java], Kim has []]Kim has [] attempts to BORROW c++[Joe has [Pascal, Java], Kim has [c++]]Kim has [c++] attempts to BORROW Pascal[Joe has [Pascal, Java], Kim has [c++]]Joe has [Pascal, Java] attempts to RETURN Pascal[Joe has [Java], Kim has [c++]]Joe has [Java] attempts to RETURN c++[Joe has [Java], Kim has [c++]]00Output:Joe has [] attempts to BORROW Pascal[Joe has [Pascal], Kim has []]Joe has [Pascal] attempts to BORROW Java[Joe has [Pascal, Java], Kim has []]Kim has [] attempts to BORROW c++[Joe has [Pascal, Java], Kim has [c++]]Kim has [c++] attempts to BORROW Pascal[Joe has [Pascal, Java], Kim has [c++]]Joe has [Pascal, Java] attempts to RETURN Pascal[Joe has [Java], Kim has [c++]]Joe has [Java] attempts to RETURN c++[Joe has [Java], Kim has [c++]]Command c5 = new ReturnCommand(joe, b0); Command c6Fails = new ReturnCommand(joe, b2);List<Command commandList = new ArrayList<Command>();commandList.add(c1);commandList.add(c2);commandList.add(c3);commandList.add(c4ThatFails); // already outcommandList.add(c5);commandList.add(c6Fails); // Doesn't havefor(Command ref : commandList) { ref.execute(); System.out.println(borrowerList); System.out.println();}15a) Write interface Command as if it were in its own file (8pts)15b) Complete class BorrowCommand as if it were in its own file. The execute method adjusts the Borrower and the Book if everything is all right. However, if the Book is already borrowed, execute returns false. 18pts// BorrowCommand attaches a specific book copy to a specific subscriber, both, the book// copy’s and the subscriber’s objects are supplied by the programmer. public class BorrowCommand implements Command { private Borrower theBorrower; private Book theBook; public BorrowCommand(Borrower aBorrower, Book aBook) { theBorrower = aBorrower; theBook = aBook; } // When the Borrow button is clicked, execute this public boolean execute() { System.out.println(theBorrower + " attempts to BORROW " + theBook); } public boolean undo() { // a borrow by returning System.out.println(theBorrower + " undo a Borrow" + theBook);16. Here are the names of all 23 Object-Oriented Design Patterns cataloged in the GoF book. Factory Method, Builder, Prototype, Singleton, Adaptor, Bridge, Composite, Decorator, Fa?ade, Flyweight, Proxy, Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor. For each synopsis in the right column and write the matching design pattern name in the left column after each of the letters a. through k. (14pts, 2 pts each)Pattern NameSynopsisa.Provide a way to access the elements of a collection sequentially without revealing the underlying implementation.b.TBA……g.TBA17. Describe one benefit of "Clean code" (2pts)18. Given the following code, describe how you would refactor it to make for Clean Code (10pt) Example TBA19. According to the and reading of presentations, the refactoring catalog, Bob Martins Clean Code, or common sense, what is better code and/or design, a or b a) Which is the better code and/or design, a or b? _____ (2pts)-11430017780000a Stackempty()push()Object pop()Object getTop()b Stack-685803175000isEmpty()push()Object pop()Object getTop()b) Which is the better code and/or design, a or b? _____ (2pts)apublic String _namebprivate String _name;public String getName() {return _name;}public void setName(String arg) {_name = arg;}c) Which is the better code and/or design, a or b? _____ (2pts)aprivate int fibonacci(int n) { if(n <= 2) return 1; else return fibonacci(n-1) + fibonacci(n-2);}bprivate int fibonacci(int n) { int sum = 1; int older = 1; int old = 1; for (int next = 2; next < n; next++) { sum = old + older; old = older; older = sum; } return sum;}d) Which is the better code and/or design, a or b? _____ (2pts)aif (date.before (SUMMER_START) || date.after(SUMMER_END)) charge = quantity * _winterRate + _winterServiceCharge;else charge = quantity * _summerRate;bif (notSummer(date)) charge = winterCharge(quantity);else charge = summerCharge (quantity); e) Which is the better code and/or design, a or b? _____ (2pts)public class Part { // a. // The textual description private String m_dsc; void setName(String name) { m_dsc = name; }}public class Part { // b. private String description; void setDescription(String description) { this.description = description; }}f) Which is the better code and/or design, a or b? _____ (2pts) a. public void shutDown(); b. public void eatMyShorts();g) 2ptsh) 2pts20. Another question to total 150 points ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download