Study Guide For Chapter 3

Chapter 3 Covered

  1. Classes
  2. Static variables and methods
  3. Random Numbers
  4. Input and Output
  5. Exceptions

This test will also test your understanding of the MagPieLab.

Be sure to review the sample multiple choice questions of Chapter 3 and practice with http://javabat.com Especially Warmup-1, String-1, Array-1, and with 1 loop Warmup-2, String-2, Array-2,

If you still haven't mastered loops from Chapter 2, make sure you can loop through an array or an ArrayList. The Daily Quiz keys are posted under files at PlusPortals

The free response questions will require to write methods as well as a class. There will be no questions on input on the AP Exam, but there may be questions about output.

Review Exercises

  • R3.1. Consider another variation of the algorithm for determining the maximum value. Here, we compute the maximum value of an array of numbers.
double max = 0; // Contains an error!
for (x : values)
{
   if (x > max) max = x;
}

However, this approach contains a subtle error. What is the error, and how can you fix it?

  • R3.2. For each of the following sets of values, write code that fills an array a with the values.

To help you get started, here is some "starter code" that will make an array a, fill it with 0's and print the contents of a

public class ArrayExperimenting 
{
	public static void main(String[] args) {
		int [] a = new int[10];
		for (int i=0; i<10; i++)
		{
			a[i]=0;
		}
		System.out.println("Contents of a:");
        for (int element:a)
        		System.out.print(element);
        System.out.println();
	}
}
(a)  1 2 3 4 5 6 7 8 9 10

(b)  0 2 4 6 8 10 12 14 16 18 20

(c)  1 4 9 16 25 36 49 64 81 100

(d)  0 0 0 0 0 0 0 0 0 0

(e)  1 4 9 16 9 7 4 9 11

Use a loop when appropriate.

  • R3.3. What is wrong with the following loop?
double[] data = new double[10];
for (int i = 1; i <= 10; i++ ) data[i] = i * i;

Explain two ways of fixing the error.

  • R3.4. How do you perform the following tasks with arrays in Java?
 
(a) Test that two arrays contain the same elements in the same order

(b) Copy one array to another

(c) Fill an array with zeroes, overwriting all elements in it

(d) Remove all elements from an array list

  • R3.5. True or false?
(a) A method cannot return a two-dimensional array.

(b) A method can change the length of an array parameter.

(c) A method can change the length of an array list that is passed as a parameter.

(d) An array list can hold values of any type.

  • R3.6 How can you write a method that returns a random int ?