Comp Sci Final Review
What is a Java object?
A: All of these objects
What is wrong with the following array definition? int[ ] data = {1,"two",3.0};
A: all elements in the array must be the same data type
What term best describes a programming "black box"?
A: data encapsulation
What is an array?
Arrays contain a group of individual elements, with each element having the same data type.
With which classes does the Bank class have a "uses-a" relationship?
B: Customer, SafetyDepositBox
Which of the following is NOT a Java "collection" class?
C: Arrays
How do you ensure that a set of JRadioButton objects work together on the screen, automatically de-selecting one when another is selected?
C: Create a ButtonGroup and add() each JRadioButton object to the group
How many different classes can implement the same interface?
C: any number
Which line of code would add an empty 5-pixel border around the edge of a JPanel named myPanel?
C: myPanel.setBorder (BorderFactory.createEmptyBorder(5,5,5,5));
What kind of object can you add to your JFrame or JPanel client area?
D: all of these are correct
Which situation describes an ideal use for a LinkedList?
D: all of these things are correct
Which of the following code snippets will produce a compiler error, assuming this statement comes first: Bank b = new Bank();
D: b.callPolice()
A linked list is just as effective as an array at frequently accessing the nodes in the middle of a list.
False
By default, the JList will allow a user to select only one item from the list.
False
Creating a JFrame object in the main() method is the best way to enable your program to easily receive events from graphical controls.
False
If a class property is declared as static, and you create two instances of the class, how many copies of the property exist?
A: 1
What happens if you do not declare your own constructor?
A: A default constructor will be created for you
What is true, assuming careful data encapsulation procedures are defined?
A: All of these are true
What is a limitation of the enhanced "for" loop shown in the code below:element String[] myArray = {"circle", "square", "triangle"}; for (String shape : myArray) { /* implementation not shown */ }
A: Changes to the loop's "shape" variable do not actually update the original array
When modeling a complex system, how many individual objects should you define?
A: Enough that each object is reasonably small, well understood
What are two objects most likely in an inheritance relationship?
A: Flower, rose
Given the class definition below, which example shows how to correctly call the "mystery" method on the HauntedHouse object from some code outside the class? spooky.mystery(); class HauntedHouse { public void mystery() { } }
A: HauntedHouse spooky = new HauntedHouse();
Which of the following Listener interfaces would you implement to receive events about checkboxes or radio buttons getting selected and de-selected?
A: ItemListener
What methods can be used to traverse or search data in a Collection?
A: Iterator<String> myIterator = myStrings.iterator();
What line of code will send a simple message box pop-up to the user?
A: JOptionPane.showMessageDialog(null, "This is my message");
What object represents the client area in the window?
A: JPanel
What is the difference between a JTextField and a JTextArea?
A: JTextField typically takes a single line of text. A JTextArea typically takes multiple lines of text.
What three things should usually be considered when defining an object?
A: Properties, methods, relationships
Which statement correctly reads the static String property Color from the class Water?
A: String c = Water.Color;
Who invented the Java Swing classes?
A: Sun Microsystems
Why would you want to make this function call on your newly created JFrame? setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
A: The default JFrame behavior will just hide the window
Why do we start all of our example interface names with "I" as in "ILogin"?
A: This is a convention that tells you at a glance
What does it mean to create an event-driven program?
A: This means that certain functions within your code
With which classes does the Bank class have a "has-a" relationship?
A: Vault, ATM
If the Mystery class contains a private function called secret(), which of the following lines of code will execute that function from some other class?
A: You cannot call private classes on other classes
Assuming we had a LinkedList of Strings called "myStrings", how would we create and initialize an Iterator from the list?
A: an enhanced for loop
How many different constructors can you define for a class?
A: as many as you like, as long as they all have different combinations of parameters
When is a constructor function run?
A: automatically when an object is created with the new keyword
If you are storing a JList selected item in a string variable, what do you have to do with the data returned by JList.getSelectedValue()?
A: cast it to a string
What OOP principle does the following code violate? class Mystery { public int myData; }
A: data encapsulation
Where are interface definitions stored?
A: in a .java* file named after the interface
Where should you create the main window for your application?
A: in the main() function
What tasks do you usually complete within an object's constructor?
A: initialize the class member variables to default values
Where do you declare member variables for a class?
A: inside the class curly braces but outside a function
In Swing, the placement of controls in the window client area is handled by an object called _______.
A: layout manager
The objects Bicycle, Wheel, Chain, Pedal, Seat, Frame, and HandleBar are examples of what OOP?
A: modeling complex systems
Given the following array: int[ ][ ] my2DArray = {{1,2,3,4}, {5,6,7,8}}; Which line of code will replace the number "4" with the number "9"?
A: my2dArray[0][3] = 9;
How would you initialize a JSpinner variable "mySpinner" with a JSpinner that allowed numbers between 0 and 100, used an increment of 5 when the arrow was pressed, and had a starting value of 50?
A: mySpinner = new JSpinner(new SpinnerModelNumber(50,0,100,5));
If you declare a class member variable as "static", how many copies of that variable will ever exist at one time?
A: one
What type of data is returned by an Input Dialog box?
A: only text data
What is one advantage of writing objects?
A: other programmers can reuse your work
Which properties and methods can only be accessed by the object that defines them?
A: private
Which of the following answers defines a property with a private access level?
A: private String mySecret;
Which properties and methods can only be accessed by the object that defines them, or by objects that inherit from or subclass from the first object?
A: protected
Which properties and methods can be accessed by any other object?
A: public
Which of the following CellPhone class declarations correctly implements the IDialer interface?
A: public class CellPhone implements IDialer
Which of the following statements successfully declares the Mystery class as implementing the IHaunt interface?
A: public class Mystery implements IHaunt
Consider the following code: char[ ][ ] my2DArray = {{'a','b','c','d'}, {'e','f','g','h'}}; for (int i=0; i<my2DArray.length; i++) { for (int j=0; j<my2DArray[i].length; j++) { if(my2DArray[i][j] == 'e') { System.out.println("row: " + i); System.out.println("col: " + j); } } } What is the output?
A: row: 1 col: 0
If you change the value of a static property within one instance of a class, what happens to that property when accessed by other instances of the class?
A: the property immediately changes
How would you implement a method body within an interface definition?
A: you cannot
What is wrong with the following code? class HauntedHouse { static public void main(String[] args) { mystery(); } public void mystery() { } }
A: you cannot call a non-static function
How would you insert or delete elements from an array with one or two dimensions?
A: you cannot insert or delete elements
Which situation describes an ideal use for an ArrayList?
A: you need to frequently access list items
What will happen when you run the following code? int[ ] myIntArray = {100,200,300,400}; for (int i=1; i<3; i++) { System.out.print(myIntArray[i] + " "); }
A: you will get an ArrayIndexOutOfBounds exception
What was one limitation of the original Java AWT graphical library?
B: AWT was limited to the common elements
If you wanted to arrange controls on your JPanel in an evenly spaced array with a certain number of columns and rows, which Layout Manager would work best?
B: GridLayout
What is an easy way to prompt the user for a single string value?
B: JOptionPane.showInputDialog()
What is wrong with the following code? class HauntedHouse { static String scare = "Boo"; public void mystery() { System.out.println(scare) } }
B: Nothing, this will print "Boo"
Which statement shows an array of 3 elements correctly initialized with string values?
B: String[] myArray = {"cats",""dogs","frogs"}
What expression will return the number of columns (elements in the 2nd dimension) in the following array: int[ ][ ] my2DArray = new int[5][10];
B: my2DArray[0].length
If the ILogin interface is defined as follows: {return true} public interface ILogin { public boolean validatePIN(int pin); } What methods would satisfy the interface requirements for a class that implements the ILogin interface
B: public Boolean validatePIN(int pin)
What does the List interface add to the Collection concept?
B: your List objects are kept in sequential order
What is the difference between the following two statements? int[ ] myIntArray = new int[4]; int[ ] myIntArray = {100,200,300,400};
C: The initial values in the 4 elements of each array
Which list traversal method hides the iterator completely?
C: an enhanced for loop OR an iterator interface
How many interfaces can a class implement at one time?
C: any number
What set of classes can be used to add GUI elements to a Java program?
C: both AWT and Java Swing Classes
How do you know when the user is finished adding text to a JTextField control?
C: both of these will work
A pixel located at coordinates (0, 800) will be closest to which corner of the screen?
C: bottom left corner
How does your program receive events from a user button click?
C: implement the ActionListener interface
Given an array with 7 elements, which of the following values is not a valid index to one of the elements?
D: 7
If you add a JComponent to your JFrame or JPanel, and then lose track of that object reference, what are you prevented from doing later?
D: Identifying which of your multiple controls was
What is a difference between a JList and a JComboBox control?
D: all of these are correct
Which statement below is true when considering plain and "generic" Collections?
D: all of these are true
Which of the following are valid JList selection modes?
D: all of these are valid
Which of the following functions declarations would define a valid constructor for the CellPhone class?
D: all of these are valid
How would you let the user view data in a JTextArea that is either to the side or below the currently visible area?
D: create a JScrollPane object to hold your JTextArea
When considering an AutomaticTellerMachine object which allows people to get money from a bank using their personal debit card, which of the following interfaces might be useful for the AutomaticTellerMachine to implement?
D: implement all of them since an object can
After you create a JFrame window and exit your main() function, what happens?
D: your application will continue
You must include a visual border around groups of radiobuttons and checkboxes.
False
If you are creating a titled border to go around a group of components, on what object do you call
IDK the answer but it's not D
int[ ] myIntArray = {100,200,300,400}; System.out.println("value = " + myIntArray[4]); What will the following code print to the console?
It throws an exception
If the user is only allowed to choose one item in the list, how would you retrieve their selection?
String userChoice = (String)myList.getSelectedValue();
Which line of code will create an array of Strings named "pets" with the values "dog", "cat", and "fish"?
String[ ] pets = {"dog", "cat", "fish"};
Regardless of where you place radio buttons on your screen, if they are part of the same ButtonGroup, they will all work together as a group.
True
The AWT is limited to using only the common controls that are shared among the major graphical operating systems.
True
How do you check the current status of a JRadioButton control?
bool currentSetting = myRadioButton.isSelected();
If the user clicks the "Cancel" button on an Input Dialog, the returned result will be null.
True
A Collection represents a group of individual elements but does not give any clues how the elements are stored within the class.
True
All Swing UI controls inherit either directly or indirectly from the JComponent base class.
True