You are on page 1of 7

Copyright c 2013 All rights reserved.

Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

CS 1331 Exam 3 Practice

Name (print clearly): Signature: GT account username (gtg, gth, msmith3, etc):

Signing signies you are aware of and in accordance with the Academic Honor Code of Georgia Tech. Calculators and cell phones are NOT allowed.

Note
This is an object-oriented programming test. Java is the required language. Java is case-sensitive. DO NOT WRITE IN ALL CAPS. A Java program in all caps will not compile. Good variable names and style are required. Comments are not required.

Question Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 TOTAL

Points per Page 10 10 20 20 20 20 100

Points Lost -

Points Earned = = = = = = =

Graded By

Copyright c 2013 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

1. Multiple Choice Circle the letter of the best answer. [2] (a) Given the following code: ArrayList tasks = new ArrayList(10); tasks.add("Eat"); tasks.add("Sleep"); tasks.add("Code"); How many more items can be added to tasks? A. 0 B. 7 C. as many as memory will allow, essentially unlimited D. None of the above. [2] (b) What is true about the following code: ArrayList<Integer> myInts = new ArrayList<Integer>(); myInts.add(2); myInts.add(3); A. B. C. D. [2] (c) Which of A. B. C. D. It will not compile becuase no capacity was given in the ArrayList constructor; It will not compile because you can only add reference variables to collections. The int arguments to add will be auto-boxed to Integers. None of the above. the following is/are (a) valid way(s) to declare a listener for a JButton? Having the enclosing class implement ActionListener. Writing an anonymous ActionListener inner class. Writing an ActionListener as a standalone class. All of the above.

[2]

(d) Which of the following layout managers arranges components uidly in a manner similar to English language text on a page? A. FlowLayout B. BorderLayout C. GridLayout D. BoxLayout (e) Which of A. B. C. D. the following layout managers arranges components in one of ve named regions? FlowLayout BorderLayout GridLayout BoxLayout

[2]

Page 1 of 6 Points available: 10 - points lost:

= points earned:

. Graded by:

Copyright c 2013 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

2. Multiple Choice Circle the letter of the best answer. Assume Trooper is dened as follows: public class Trooper { private String name; private boolean mustached; public Trooper(String name, boolean hasMustache) { this.name = name; this.mustached = hasMustache; } public String getName() { return name; } public boolean hasMustache() { return mustached; } public boolean equals(Object other) { if (this == other) return true; if (null == other || !(other instanceof Trooper)) return false; Trooper that = (Trooper) other; return this.name.equals(that.name) && this.mustached == that.mustached; } public int hashCode() { return super.hashCode(); } } And the following has been executed in the same scope as the code in the questions below: ArrayList<Trooper> troopers = new ArrayList<>(); troopers.add(new Trooper("Farva", true)); troopers.add(new Trooper("Farva", true)); troopers.add(new Trooper("Rabbit", false)); troopers.add(new Trooper("Mac", true)); [2] (a) What would be the result of the statement Collections.sort(troopers)? A. The code will not compile. B. troopers will be sorted in order by name. C. troopers will be sorted in order by mustache, then name. D. troopers will not have any duplicate elements. (b) After executing the statement Set<Trooper> trooperSet = new HashSet<>(troopers), what would be the value of trooperSet.contains(new Trooper("Mac", true))? A. The code will not compile. B. true C. false D. void (c) Given the denitions of troopers and trooperSet above, what would troopers.size() return? A. true B. false C. 3 D. 4 (d) After the statement Set<String> stringSet = new HashSet<>(Arrays.asList("meow", "meow")) executes, what would be the value of stringSet.size()? A. true B. false C. 1 D. 2 (e) Meow! A. true

[2]

[2]

[2]

[2]

Page 2 of 6 Points available: 10 - points lost:

= points earned:

. Graded by:

Copyright c 2013 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

3. Short Answer [5] (a) Write a line of code that instantiates an ArrayList object that can hold JLabel elements (and only JLabels) with an initial capacity of 20 and does not produce any compiler errors or warnings.

[5]

(b) Continuing from the previous question, write a for-each loop that prints out the text label of each JLabel in the ArrayList to the console.

[5]

(c) Given the denition of Trooper and the ArrayList<Trooper> troopers in the previous question, write a single statement that sorts troopers by mustache, then name using Collectionss public static <T> void sort(List<T> list, Comparator<? super T> c) method.

[5]

(d) Complete the code below that adds to a JButton an ActionListener that pops up a simple message dialog that simply displays the message Thanks!. Do this in the simplest possible way. JButton button = new JButton("Click me!"); button.addActionListener(

);

Page 3 of 6 Points available: 20 - points lost:

= points earned:

. Graded by:

Copyright c 2013 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

[20] 4. Write a class named Person with a name eld of type String and a birthYear eld of type int. Instances of Person should be immutable, behave properly as elements of any collections class, and you should be able to pass collections of Person to Collectionss public static <T extends Comparable<? super T>> void sort(List<T> list) method, which would sort by birth year, then name (where earlier years are less than later years). Do not use any raw types. Assume you always get valid input.

Page 4 of 6 Points available: 20 - points lost:

= points earned:

. Graded by:

Copyright c 2013 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

5. Write the class/method. Dont worry about imports or invalid input. [15] (a) Write a class called NewItemListener which implements the ActionListener interface. NewItemListener should have two elds (instance variables), Component parent and DefaultListModel<String> listModel which are initialized in the single constructor. NewItemListeners actionPerformed method should display a simple input dialog using JOptionPane and add the String returned from the dialog to the listModel.

[5]

(b) Write a helper method createButton that returns a JButton and takes two arguments, a String that will be the buttons text, and an instance of an ActionListener to register as an action listener of the button.

Page 5 of 6 Points available: 20 - points lost:

= points earned:

. Graded by:

Copyright c 2013 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.

[20] 6. Complete the constructor for BucketListFrame below, which uses the NewItemListener class and createButton helper method you wrote in the previous question. BucketListFrame should display a JList in the center of the frame that has automatically appearing scroll bars and stretches vertically and horizonatally when the frame is resized, and a button labeled New item ... in the bottom of the frame that stretches horizontally but not vertically. The JList should use a DefaultListModel for its list data, which is poplulated by the NewItemListener thats registered on the button in the createButton helper method. Dont worry about imports or invalid input. Note that the user cannot remove items from the list. public class BucketListFrame extends JFrame { public BucketListFrame() { // Complete the constructor.

Your code goes here:

} private JButton createButton // from previous question public static void main(String[] args) { BucketListFrame blg = new BucketListFrame(); blg.setVisible(true); } }

Page 6 of 6 Points available: 20 - points lost:

= points earned:

. Graded by:

You might also like