Chapter 9&10
Assume an array defined and created: int[ ] mylist = new int[10]; If the next statement is: System.out.print(mylist[2]); What will print out?
0
Assume an array defined and created: int[ ] mylist = new int[10]; If the next statement is: System.out.print(mylist[2]);What will print out?
0
If an array has n elements, what are the values of the indices of the first and last elements?
0 and n-1
Given the following code which line that will be displayed? double[] squares = new double[10]; for(int i = 0; i < 5; i++) squares[i] = i * i; for (double s : squares) System.out.print(s + " ");
0.0 1.0 4.0 9.0 16.0 0.0 0.0 0.0 0.0 0.0
Given: int[ ] [ ] vals = new int[5][4]; vals.length will evaluate to:
5
The BorderLayout Manger has _____ regions
5
When you use a try block what do you need to match with it?
A catch block
The default layout for a JFrame window is
BorderLayout
Which is the most appropriate layout manager to use to: Distribute components among five regions - north south east, west, and center
BorderLayout
What does a user do to cause an event to be fired by a JButton component?
Clicks on the button component
What does LIFO mean?
Last In, First Out
Which one of these is NOT a basic step required to read input from a file?
Open the block the keyboard buffer method.
If you wish to write text to a file use:
PrintWriter
Which single statement declares, creates, and assigns a 100-element array that stores book titles.
String[] books = new String[100];
What is the method call you can insert before and after a block of code to measure the number of nanoseconds needed to execute that block of code?
System.nanoTime()
Assume code that has imported Scanner and instantiated it for keyboard input as stdIn. Also assume there is a declaration existing for roots to be an array of double variables. Which code prompts the user to enter the number of elements and then initializes roots with an array object whose length equals the input?
System.out.print("Enter the number of elements: "); roots = new double[stdIn.nextInt()];
What is unboxing?
Taking a value from a wrapper object and storing it into the defined primitive variable.
Which of these is NOT true of text files created by Java programs?
Text data is in a unicode format.
If you wish to read objects from a file use:
ObjectInputStream and FileInputStream classes
In a GUI program an event is
a message that indicates something has happened.
One thing the JFrame class doesn't provide when a program uses it as the superclass (extends) is
a write field
Which Java statement declares an array of char elements called vowels and initializes it with 'a', 'e', 'i', 'o', and 'u'.
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
Given the following declaration and creation String[ ] starTrek = { "Spock", "Uhura", "Chekov", "Sulu"}; What will System.out.print(starTrek[2]); display?
chekov
What Java statement that makes it impossible for a user to enter anything into a JTextField component called entry.
entry.setEditable(false);
Assume that roots is an initialized array of double variables. Which code fragment uses a for loop to fill each array element with the square root of that element's index value?
for (int i=0; i<roots.length; i++) roots[i] = Math.sqrt(i);
Given: an ArrayList of strings called cards. String myCard; The following is the code to use a for-each loop.
for(String x : cards) { ..... }
To make an object able to be stored in a file, what must you append to the object's class heading.
implements Serializable
To use JFrame you need to import
javax.swing.*
The two components of a Java map are called ___ and _____.
key and value
Write a Java code fragment that changes the text in a JLabel called label to the word, "Done".
label.setText("Done");
Given: an ArrayList of Strings called cards. String myCard; To retrieve the third item from an ArrayList cards:
myCard = cards.get(2);
In the book, which sort method is discussed?
selection sort
If you have a component someField and it needs to take action when an event is fired you must use a code similar to
someField.addActionListener(new Listener());
For a binary search to work on an array, the array must already be __________.
sorted
Which of the following is a syntactically correct Java example of accessing an element in a two-dimensional array with 5 rows and 5 columns named table?
table[2][2]
Given array element initialization example: String[] names = {"Mary", "Tom", "Ann"};The size of the students array is Correct!
the number of elements specified
Assume a class called Voter. It has a constructor that takes two elements, a String, and a char. Write a Java statement that appends to an ArrayList of Voter objects call votingPublic a Voter whose values are "Leibowitz" and 'D', respectively. Use an anonymous object,
votingPublic.add(new Voter("Leibowitz", 'D'));
Write a Java statement that removes the Voter element currently at index position i in an ArrayList called votingPublic.
votingPublic.remove(i);
The book uses the acronym ANSY as a mnemonic device. What does ANSY stand for?
arrays no, strings yes
A chananel's position ansd size values are number of _______ .
bytes
The JTextField component
can let a user enter data.
In the FlowLayout manager to change the vertical alignment to start at bottom use
cannot be done.
Given: an ArrayList of Strings called cards. String myCard; To replace the third item from in ArrayList cards:
cards.set(2,myCard);
Write a Java statement that creates an ArrayList called monthlyBalances. Each element in the array list contains one double value.
ArrayList<Double> monthlyBalances = new ArrayList<>();
Assume a class called Voter. Write a Java statement that instantiates an ArrayList of Voter objects call votingPublic;
ArrayList<Voter> votingPublic = new ArrayList<>();
Which of these is NOT a benefit of binary files?
Binary files can be read by any editor.
Which is the most appropriate layout manager to use to: Arrange different-sized components in a rectangular grid.
GridBagLayout
Which is the most appropriate layout manager to use to: Arrange same-sized components in a rectangular grid.
GridLayout
What can be used to store multiple componets with in a GridLayout cell?
Instantiate a JPanel
The best choice if you have a list of multiple items to select from and you want to allow more than one to be chosen is
JCheckBox
The best choice if you have a long list of multiple items (such as state) to select from and you want to allow only one to be chosen is
JComboBox
Which class generates a dialog box (pop-up window)?
JOptionPane
What code would add this: hello world box! ok button
JOptionPane.showMessageDialog(null, "Hello, world!");
The best choice if you have a list of a few items to select from and you want to allow only one to be chosen is
JRadioButton
Assume a class Person exists and it has both a default and parameterized constructor. It has Update and Display methods. To create an array of Person objects that has been properly initialized to default values the following code should be used.
Person[] people = new Person[6]; for(int i = 0; i < 6; i++) people[i] = new Person( );
What is autoboxing?
Placing a primitive value into a wrapper object.
What method do you use to insert another element into an ArrayList?
add
What code is would add a read-only text string to a window in your program.
add(new JLabel("I am here"));
Assume an array books is declared and initialized. The proper way to get the size of the array books is
books.length
Which code fragment declares but does not initialize roots to be an array of double variables?
double[ ] roots;
To distinguish which component was fired a listener can use
e.getSource( )
GUI programs use
event handlers
If you wish to create a window for your program what should you add to your class header?
extends JFrame
Which is the most appropriate layout manager to use to: Add componets left to right until current row is filled. Then continues on next row.
flowlayout
To set the background color for a JFrame window to red
getContentPane( ).setBackground(new Color(255, 0, 0);
Identify the import statement that will make the Arrays class available
import java.util.*
As a program reads from or writes to a DoubleBuffer, the buffer's position variable:
increase by 1 with each successive double
As a program reads from or writes to a DoubleBuffer, the buffer's position variable:
increases by 1 with each successive double.
The JLabel Component
is a read only message
JFrame's setVisible method
is called so that the window is displayed.
If you open a file in append mode,
it allows new text to be written at the end of the file.
If you open a file with the RandomAccessFile class,
it allows reading and writing from a random location within the file