CPSC 124, Spring 2006
Answers to Quiz #5

Question 1: Define the term array, as it is used in Java. (What are the essential characteristics of an array?)

Answer: An array consists of a numbered list of elements, where every element in the array has the same type. The list has a fixed length and the elements are numbered from 0 up to this length, minus 1.

Question 2: What is meant by the base type of an array.

Answer: Every element in an array acts like a separate variable. All these variables are of the same type, which is referred to as the base type of the array. That is, the base type specifies what type of value can be stored in the array.

Question 3: Explain carefully what is accomplished by this statement (and maybe draw a picture):

double[] values = new double[5];

Answer: This statement does the following: (1) An array object is created in the heap; the array can hold 5 values of type double. (2) Each of the five elements of the array is initialized to zero. (3) A variable named values of type "array of double" is declared. (4) A pointer (or "reference") to the newly created array object is stored in the variable.

Question 4: Suppose that numberList is a variable of type int[] (so it refers to an array of integers). Write a for loop that will print out each of the numbers in the array. Use System.out.println to print each number.


            for (int i = 0; i < numberList.length; i++) {
                System.out.println( numberList[i] );

Question 5: You are creating a JPanel to hold five JButtons. You can choose between using the default FlowLayout on the panel, or using a GridLayout instead. What's the difference? Exactly what effect will your choice have?

Answer: FlowLayout and GridLayout are "layout managers". The layout manager of a panel is responsible for setting the size and location of every component that is contained in the panel. The major difference between the two types of layout manager is that in a GridLayout, the components are all the same size, and they are laid out in rows and columns in a rectangular grid. In a FlowLayout, on the other hand, the components are just lined up next to each other, and each component is shown at its own natural size. Assuming that the GridLayout has just one row, then the buttons would all be that same size and would fill the panel completely. With a FlowLayout, the buttons will be different sizes, and there is likely to be extra space on either side of the buttons.

David Eck, 10 April 2006