Final Exam

Your grade is determined by your only attempt!!!!

• 1   What control structure does Java use to repeat statements?

1. A   A loop.

TQA-1 --- Java also gives us some convenient formats for repeating actions in loops. We talked about two of them in the first Java course, the while and do-while loops. Their format also includes a logical expression, which Java uses each time through the loop to decide whether to continue. So the equivalent of an if statement is hidden in each loop.

2. B   An iterator.

3. C   A repetitor.

4. D   A branch.
_______________________________________________________________________________________________________________________

• 2   What is a constructor?

1. A   A class designed especially to construct a Java object.

2. B   A data field used to build new data types.

3. C   A method that we use to initialize new objects of a class.

TQA-2 --- Constructors are special methods that we can use to construct, or initialize, new objects of the class. You can identify constructors easily; they always have the same name as the class and never have a return type. Java calls a constructor whenever a program creates an object from a class. For example, a constructor for the above Student class could look like this:

4. D   One of Java's primitive data types.
_______________________________________________________________________________________________________________________

• 3   What is a Java constant?

1. A   A literal whose value is also its name.

2. B   A reference to a value that cannot change.

Not all constants are references. The correct answer is: A value that cannot change. (Lesson 1, Chapter 3)

TQA-3 --- Constants represent values that don't change. They can be either named constants, which are often just called constants, or literals. A named constant refers to a value that doesn't change. For example, if I were to write a program that dealt with geometry and used the mathematical value π repeatedly, I could either type 3.1415927 every time I needed it, or I could set up a name for it. The name has several advantages. It's easier to remember, easier for another programmer to read and understand, and easier to use correctly. If I use a name, the compiler will insert the same value for that name every time. But if I enter the number manually every time and I accidentally type 3.1445927 once, for example, the compiler won't recognize that it's the wrong value.

3. C   A Java program that constantly repeats the same action.

4. D   A value that cannot change.
_______________________________________________________________________________________________________________________

• 4   What control structure does Java use to process all elements in a collection?

1. A   A do-while loop.

2. B   A for loop.

3. C   A for-each loop.

TQA-4 --- The last loop format is often called the for-each loop since it runs once for each item in the collection, no matter how many there are. Java makes sure that each item in the collection is processed and that none of them are processed twice. Here's the format of the for-each loop:

for (<element> : <collection>)
{
    // code for loop body goes here
}

To describe this loop in English, we could say, "For each element in collection, execute the loop body." Let's set up our toString method one more time with this new loop type. Ideally we could set it up like as below. But if you run it that way, you'll get an error, so don't run it yet:

4. D   A while loop.
_______________________________________________________________________________________________________________________

• 5   Which of the following is a valid Java array declaration?

1. A   String array s[10];

2. B   String[] s;

TQA-5 --- Arrays are Java's way of storing a group of similar things in one name. You could, for example, build an array of integers, an array of strings, or an array of Booleans to use in your process. It's the same concept, regardless of the type of data you store in the array. The two rules that govern arrays are (1) all elements of an array must be the same type, and (2) once you declare an array, its size is fixed and you can't change it. Java uses square brackets ([]) to designate and use arrays. Here are some array declarations and instantiations to look at:

        	int[] x;
			int[] y = new int[5];
			String[] s;
			float[] f = new float[1000];
			x = new int[10000];
			s = new String[99];
		

3. C   String[10] s;

4. D   array String[] s;
_______________________________________________________________________________________________________________________

• 6   Which of the following references will get the last element in an array of n elements named a?

1. A   a[n – 1]

TQA-6 --- This drawing represents the memory where an array with n elements is stored. The array's elements are stored contiguously, and we can use them individually via the index in square brackets. For example, if the array stores character values, and its name is c, then using c in a program will refer to the array, while using c [0] will refer to the first character in the array; c [4] will refer to the fifth character, and c [n-1] will refer to the last character in the array.

2. B   a[n]

3. C   a[n + 1]

4. D   a
_______________________________________________________________________________________________________________________

• 7   Why did we use the PrintStream and Scanner classes for file output and input, respectively?

1. A   They're the only classes available for file input and output.

2. B   They allow us to use the same input and output method call formats we used for keyboard input and display output.

TQA-7 --- We'll start by going over how to declare an output text file. Java provides a convenient class for us to use when declaring an output text file. It's called the PrintStream class. This class does text output in a way that's very similar to displaying text on your screen. There are other ways to write output files, but for learning purposes, this class is the easiest way to start.

3. C   Those are not classes we used for file input and output.

4. D   Only the PrintStream class can create a file that the Scanner class can read.
_______________________________________________________________________________________________________________________

• 8   Which of the following is a valid Java output file declaration?

1. A   PrintStream oFile = new PrintStream("Output File.txt");

TQA-8 --- Before we can use the output stream, we have to declare it like this:

PrintStream outputFile = new PrintStream("FileName.txt");
        

2. B   OutputFileStream oFile = new OutputFileStream("Output File.txt");

3. C   OutputFile oFile = new OutputFile("Output File.txt");

4. D   OutputStream oFile = new OutputStream("Output File.txt");
_______________________________________________________________________________________________________________________

• 9   Which of the following statements will write to an output file, assuming that a variable oFile has been declared correctly as in the previous question and that a String variable named s has been created with the appropriate data to write?

1. A   oFile.println(s);

2. B   oFile.write(s);

?

The write() method does not write strings. The correct answer is: oFile.println(s); (Lesson 3, Chapter 3)

3. C   oFile.output(s);

4. D   oFile.fileWrite(s);
_______________________________________________________________________________________________________________________

• 10   What is an abstract method?

1. A   A method with no parameters.

2. B   A method that does not return a value.

3. C   A method that is not implemented, one that has no body.

TQA-10, 12 --- The abstract keyword in a method declaration tells Java that we're going to declare this method, but we're not going to implement it yet. In other words, we're going to tell Java what the method's name is, what its parameter list looks like, and what type of value it will return, but nothing else. That means the method won't work!

4. D   A method that is not public.
_______________________________________________________________________________________________________________________

• 11   Which Java class does every other class inherit from?

1. A   The First class.

2. B   The TopLevel class.

3. C   The Original class.

4. D   The Object class.

TQA-11 --- Each of those classes, in turn, can depend on other classes above them in the hierarchy. Ultimately, every class traces its way back to Java's Object class. Every data type in Java except the primitive types (int, float, bool, and so on) inherits from the Object class.


_______________________________________________________________________________________________________________________

• 12   Which of the following statements will correctly declare an abstract method?

1. A   public abstract int getX(){return x;}

An abstract method does not have a body. The correct answer is public abstract int getX();. (Lesson 4, Chapter 4)

TQA-10, 12 --- The abstract keyword in a method declaration tells Java that we're going to declare this method, but we're not going to implement it yet. In other words, we're going to tell Java what the method's name is, what its parameter list looks like, and what type of value it will return, but nothing else. That means the method won't work!

2. B   public abstract int getX(){}

3. C   public abstract int getX();

4. D   public abstract int getX;
_______________________________________________________________________________________________________________________

• 13   What is a frame in Java?

1. A   It's the outer edge of an image.

2. B   It's what we use in Java's GUI to represent a window.

TQA-13 --- Think of the frame as the wood frame around the glass of a window in a building. A Java frame holds the title bar with its icon and buttons, but we can't add items to it. We add them to the pane of the window, which will display all our GUI components. We call the frame's getContentPane() method to get the pane we will add our components to. The contentPane container is like the pane of glass in a window.

3. C   It's a program's internal structure.

4. D   It's a boundary inside a window separating one group of GUI components from another group.
_______________________________________________________________________________________________________________________

• 14   What is a Java event?

1. A   It's a user action in the GUI that initiates communication with the program.

TQA-14 --- Luckily, Java has a mechanism for dealing with GUI user actions. It's called event-handling. A Java event is a user action in the GUI that needs to communicate with the program. There are lots of different events in Java, and one of them is a button click. How do we get the button to tell us if it's been clicked? We have to listen for it.

2. B   It's a transfer of control from one method to another.

3. C   It's Java's way of handling errors that occur at runtime.

4. D   It's something unexpected that interrupts a program.
_______________________________________________________________________________________________________________________

• 15   What is a dialog box?

1. A   A rectangle with text in it.

2. B   A GUI window that pops up to interact with a user.

TQA-15 --- The first thing we added to that method is a String object declaration named textToShow. We'll capture the user's input in that string so we can display it on the button.

The next statement does all the dialog work for us. It includes a call to the showInputDialog() method of the JOptionPane class. That method displays a dialog that has a text box for user input, then returns that text to us when the user clicks OK.

3. C   A rectangle with a place for user entry.

4. D   A Java class that permits two-way communication between Java programs.
_______________________________________________________________________________________________________________________

• 16   How does Java's BorderLayout work?

1. A   It manages the layout of the borders around components in a window.

2. B   It places GUI components into five window regions, one in the center of the window and the other four surrounding it.

TQA-16 --- BorderLayout divides its window into five parts that have fixed positions relative to each other. Each part can hold one component, and the size of the part controls the component's size. This figure shows the five parts, each with a button added to it:

3. C   It controls the look-and-feel of your window's border.

4. D   It defines the look of borders between panes in a window.
_______________________________________________________________________________________________________________________

• 17   In Java's GUI organization, what is a panel?

1. A   It's a GUI container that hides other components.

2. B   It's a GUI container that can hold other components.

TQA-17 --- I said earlier that I'd explain how to use a panel to add multiple components to a layout region that only accepts one. A panel is a component that holds other components. Not only do panels hold multiple other components, but they can each have their own layout managers that can be different from the frame's manager. We build panels from Java's JPanel class. I'll show you how in just a minute. First, let's decide what we want to do in our window.

3. C   It's a GUI container that defines a background color.

4. D   It's a GUI container with controls designed to manage the window layout.
_______________________________________________________________________________________________________________________

• 18   How can you make a text area scroll when the text will not fit in the window?

1. A   All text areas will automatically scroll if the text will not fit.

2. B   You have to attach scroll bars to the text area in order to allow scrolling.

Scroll bars cannot be attached to a text area by themselves. The correct answer is: You must combine the text area with a scroll pane to provide scrolling capabilities. (Lesson 6, Chapter 4)

TQA-18 --- The first thing we need to do is add one more declaration to our list of window components. We can declare a JScrollPane object like this:

private JScrollPane scrollArea;
    

To make our text scroll, we'll need to do three things. First, instead of adding the textArea to the window pane, we'll pass it to the scroll pane when we call its constructor. That will tell the scroll pane what component will be scrolled. So let's replace the line that added textArea to contentPane with this line:

scrollArea = new JScrollPane(textArea);
    

3. C   You need to set the scrollable property of the text area to true.

4. D   You must combine the text area with a scroll pane to provide scrolling capabilities.
_______________________________________________________________________________________________________________________

• 19   How many items can a menu hold?

1. A   16.

2. B   24.

3. C   32.

4. D   There is no fixed limit of the number of items in a menu.

TQA-19, 21 --- In case you're wondering, you can add another menu to an existing menu, too. If you add a menu to a menu, it creates a submenu that opens when you click its name in the first menu. You can layer menus as deep as needed, one inside another. Since you already have all the tools to do that, I'm going to leave it for you to try as an exercise in the assignment.


_______________________________________________________________________________________________________________________

• 20   What is a Java menu accelerator?

1. A   A keyboard shortcut that, when used with the ALT key, activates a visible menu or menu item as if it were clicked.

2. B   A keyboard shortcut that bypasses the menu hierarchy to activate a menu item whether it is visible or not.

3. C   Another name for a keyboard shortcut.

There are many types of keyboard shortcuts, and an accelerator is only one of them. The correct answer is: A keyboard shortcut that bypasses the menu hierarchy to activate a menu item whether it is visible or not. (Lesson 7, Chapter 4)

TQA-20 --- Go ahead, try saying "menu mnemonics" five times fast! Just kidding. Our last topic for today is how to set up keyboard shortcuts for menus and menu items. A mnemonic for a menu is a key that, in combination with the ALT key, will make the menu act just like you clicked the associated menu or item. In order for a mnemonic to work, the associated menu or item must be visible. An example of using mnemonics would be hitting ALT + F to pull down the File menu, then typing O to select the Open menu item.

There are also menu accelerators that will bypass the menu hierarchy and act like the menu item was clicked, whether or not the item was visible. For example, you could type CTRL + S to save, whether or not the Save menu item is visible.

4. D   An abbreviation of a menu name so you don't have to refer to its full name.
_______________________________________________________________________________________________________________________

• 21   How many levels of submenus are allowed in Java?

1. A   One.

2. B   Two.

3. C   Three.

4. D   There is no fixed limit to submenu depth.

TQA-19, 21 --- In case you're wondering, you can add another menu to an existing menu, too. If you add a menu to a menu, it creates a submenu that opens when you click its name in the first menu. You can layer menus as deep as needed, one inside another. Since you already have all the tools to do that, I'm going to leave it for you to try as an exercise in the assignment.


_______________________________________________________________________________________________________________________

• 22   Which of the following is true about labels in Java?

1. A   A label may contain only text.

2. B   A label may contain only an image in icon format.

3. C   A label may contain either an image in icon format or text, but not both.

4. D   A label may hold both an image in icon format and text.

TQA-22 --- It turns out that Java labels (JLabel objects) are pretty flexible and can hold not only text but also images in the form of icons (ImageIcon objects). It also comes in handy that the icons in Java are flexible and can hold almost any image. The first line above creates a new label named imgLabel, and instead of putting text into the label, it creates an ImageIcon by giving it the file name of the image we want to load. If the image were in another directory, we could still load it by using its full path in place of its name above. The last parameter in the first line tells Java that we want to center the image in the label.


_______________________________________________________________________________________________________________________

• 23   What is a GUI text field?

1. A   Another term for a label.

2. B   A component that allows a user to enter text.

TQA-23 --- All that's left to add (visually) are the text fields to receive the name and address information. We can reuse our subpanel variable, smallPanel, since the object it refers to is already added to the window. Now we'll create a second subpanel object, give it a layout manager, and add the text fields to it, like so:


3. C   An area of the screen used to display text.

4. D   Any variable containing text.
_______________________________________________________________________________________________________________________

• 24   What is the purpose of a border in a GUI window?

1. A   To outline the window and hold the window's title.

2. B   To define an area within the window where we can put GUI components.

3. C   To improve readability by separating components within a window.

TQA-24 --- Next, we'll create a border for the panel. Borders are very helpful in organizing a window visually, and Java makes them easy to create. There's a Java class called BorderFactory whose only purpose is to create borders for us.

4. D   To provide a background graphic for all or part of a GUI window.
_______________________________________________________________________________________________________________________

• 25   Which of the following BoxLayout types will place components side by side?

1. A   X_AXIS.

TQA-25 --- The first line of the makeSouthRegion() method creates our outer panel, a new JPanel object named panel. The second line sets that panel's layout manager, and since we want our two subpanels side by side, we'll use the BoxLayout manager with its X_AXIS option. The third line puts a border around the outer panel using the same format that we used for our other regions, with the title we want it to show: Deliver To:.

2. B   Y_AXIS.

3. C   HORIZONTAL.

4. D   SIDE_BY_SIDE.
_______________________________________________________________________________________________________________________

• 26   Which of the following would "turn on" a radio button object named rb?

1. A   rb = true;

2. B   rb.isSelected();

3. C   rb.setSelected(true);

TQA-26 --- Let's start with our radio buttons. Radio buttons created from the JRadioButton class all have a method named setSelected() that turns their selections on or off. The method expects a Boolean parameter, so if I give it a value of true, the radio button appears selected. If I give it a value of false, the button appears unselected. Remember that only one radio button in a group can be selected at a time.

4. D   rb.setSelected(false);
_______________________________________________________________________________________________________________________

• 27   Which of the following statements will find out if a check box named cb is checked or not?

1. A   if (cb).

2. B   if (cb.setSelected(true)).

3. C   if (cb.isSelected(true)).

4. D   if (cb.isSelected()).

TQA-27 --- This code lets us check the value of each radio button to see if it's selected using the isSelected() method of the JRadioButton class. That method returns true if the button is selected and false if not. Notice that as soon as we find a button that's selected, the rest of the ifs are bypassed by the else part of the if. We only keep checking as long as we haven't found a checked button. That logic works with radio buttons in a group because only one of them can be selected at a time, and we started out with one of them selected as a default option.


_______________________________________________________________________________________________________________________

• 28   What is a tree in Java?

1. A   A tree is a branching mechanism that allows multiple decisions in one statement.

2. B   A tree is a collection in which each entry has one parent but may have multiple children.

TQA-28 --- Last are trees and graphs, often used in games and transportation systems. A tree is a list where instead of one predecessor and one successor, each item has one predecessor (its parent) and can have multiple successors (its children). A graph is a collection where there are no limits to predecessor or successor connections. Think of our airport system, where you can get from one end of the country to the other in many different ways. That's a graph.

3. C   A tree is a collection in which each entry may have multiple parents but may have only one child.

4. D   A tree is a method call pattern where each call may generate multiple additional calls until the process is complete.
_______________________________________________________________________________________________________________________

• 29   What arguments does Java need to create a Font object?

1. A   One argument: the font's name.

2. B   Two arguments: the font's name and its style.

3. C   Three arguments: the font's name, its style, and its size.

TQA-29 --- The first line creates a new JLabel object with the text for the first label, then assigns it to the nameLabel instance variable we created earlier. The second line shows us something new. It uses the JLabel object's setFont() method to change the default font settings for the label. That method, as you might expect, requires a Font object as its argument. The most common way to get a Font object is to simply create a new Font with the properties we want whenever we need one. And that's what we did here, although we could have named the Font object anything we wanted and used the name here just as well.

Creating a Font this way requires three arguments. The first is a font name. In this case, I just chose my personal favorite, but you can use any font you prefer. The second argument is a font style. The Font class itself includes three style options we can use here: Font.PLAIN, Font.BOLD, and Font.ITALIC. Their names are self-explanatory. To use both bold and italics at the same time, we just add them, as in the code above. The third argument is the size (in points) we want for our font. In this case, we used size 14.

4. D   Four arguments: the font's name, its style, its size, and its color.
_______________________________________________________________________________________________________________________

• 30   How do we use an iterator to get the next element in a collection?

1. A   We call the iterator's next() method.

TQA-30 --- Another call to next() gets us element E2 and puts the iterator between E2 and E3. Wherever we are in the list, the iterator's hasNext() method will tell us whether there are more elements in front of our position. Once we have reached the end of the list, hasNext() tells us we can't go any further.

2. B   We call the collection's iterate() method.

3. C   We use the iterator's get() method.

4. D   We increase the iterator's index by one.
_______________________________________________________________________________________________________________________

• 31   Which of the following is an error condition that can occur when moving through a list using an iterator?

1. A   You could try to move past the end of the list.

2. B   You could get a file read or write error

?

Iterators do not read or write files. The correct answer is: You could try to move past the end of the list. (Lesson 11, Chapter 2)

3. C   The iterator type may not match the collection type.

4. D   An empty class might create a null iterator.
_______________________________________________________________________________________________________________________

• 32   How many tabs does a tabbed pane have by default when you first create it?

1. A   None. All tabs have to be added by the program.

TQA-32 --- This statement creates the tabbed windowpane. At this point, it has no tabs and no GUI components to display. We can add as many tabs to it as we want, but we'll only need two. We'll name them "Player View" and "Team View." The Player View tab will show us all the information about a single player, just like we set up in Lesson 10. The Team View will show us a scrollable list of all the players and their statistics.

2. B   One. Java assumes you will need at least one tab.

3. C   Two. Java assumes you will need at least two tabs or you wouldn't be using a tabbed pane.

4. D   Three. That is the most common number used, so Java creates three tabs by default.
_______________________________________________________________________________________________________________________

• 33   What is the Collections class (not Collection)?

1. A   It is a class that includes several different types of collections.

2. B   It is an interface defining the methods that any collection must implement.

3. C   It provides a number of common methods that work with different collections.

TQA-33 --- In case you're wondering what the Collections class is, it's a very useful class. It contains a number of methods that operate on collections, including the sort() method we used above. For example, it offers a reverse() method that reverses the order of a list, min() and max() methods that find the smallest and largest elements in a collection (if the elements are comparable), a copy() method that makes a complete copy of a list, and more. If you're curious, you can check it out in the Java API.

4. D   It is an abstract class providing some of the methods that all collections use, and defining others that must be implemented in any collection.
_______________________________________________________________________________________________________________________

• 34   How is a map different from other Java data structures?

1. A   Each data element connects to the next element in the map.

2. B   It's a data structure that stores data pairs: keys and their associated values.

TQA-34 --- The last one I'll mention in detail is the map. A map also has some special properties that differentiate it from other collections.

TQA-34 --- As you learned in Lesson 10, a map is a data structure that uses two data components for each entry. One data component in a map entry is its key, the value we use to look up the entry. The other data component is its value, the information we get back when we look it up. In Lesson 10, I used the analogy of a phone book, where you look up a phone number by finding the name of the person or business you want to call. The key is the name, and the value is the phone number.

3. C   Each data element can connect to multiple related elements in a map.

4. D   It's a data structure that keeps its data sorted.
_______________________________________________________________________________________________________________________

• 35   What is a wrapper class?

1. A   A class that wraps a border around an image in a window.

2. B   A class that encloses related classes into a package.

3. C   A class that puts the finishing touches on a GUI project by adding a "theme" of related colors and style elements.

4. D   A class that wraps a primitive data type in an object so it can be stored in a collection.

TQA-35 --- Some situations in Java don't work with primitive data types. We've just seen one of them. Java's collections are set up so that they can hold any type of object, but they can't hold any of the primitive data types. The types stored in a collection must be subclasses of Object, Java's ultimate parent class. Primitive types don't meet that requirement.


_______________________________________________________________________________________________________________________

• 36   How does a map tell us when an element is not found?

1. A   The map returns a null reference.

2. B   The map throws an exception.

?

Maps do not throw exceptions for unfound conditions. The correct answer is: The map returns a null reference. (Lesson 12, Chapter 4)

3. C   The map returns an empty data object.

4. D   The map returns a value of zero.
_______________________________________________________________________________________________________________________