1. byte, short, int, long, double, float, boolean, char

2. The output is false. It is also false for the 2nd set of inputs. It does not change because the == comparison checks for memory address, not equality between objects.

3. It has 1 instance variable.

4. 2 instance methods, 1 static method, and 3 static variables.

5. It doesn’t compile because it attempts to assign a double value to an int. To make it run correctly, you can make diceOne and diceTwo doubles, or put an (int) cast in front of each Math.floor(...).

6. double

7.

int x = (int) Math.floor(Math.random() * 101);

if (x < 51)

x = -x;

else

x = x % 51 + 1;

8.

public boolean sorted(int[] array)

{

for (int i = 0; i < array.length - 1; i++)

{

if (array[i] > array[i+1])

return false;

}

return true;

}

9. Scanner eliminates the need for such complicated code. The biggest side effect is that you have to deal with an exception.

10.

3: returns the substring from (6, 9]

11: returns the substring from (30, 33]

16: the first index is past the end of the string, so it throws an error.

(): substring defaults to an input of 0, and this yields a negative index, causing an error.

2a: this is not an integer, which is what substring requires. it causes an error.

11.

if (index < 1 || index > 12)

System.out.println(“Not a valid month.”);

12.

PrimeFactorTest first defines a method readInt, which takes a string called prompt, returns an integer, and throws the IOException. It first sets an int called result to 0. It then creates a new buffer reader that takes input from the keyboard. It then prints the prompt that tells the user to input a number. The loop attempts to parse the input into an integer and return it. If it can’t, it tells the user to retry with a valid input. This continues until the user inputs a number.

PrimeFactorTest then defines a method called isPrime, which takes an integer n and returns a boolean value. It creates a for loop that checks every integer between 2 and n to see if it divides n. If it finds a divisor, it returns false and the loop automatically breaks. Otherwise, the loop finishes and the method returns true.

PrimeFactorTest then defines a method called printPrimeFactors, which takes an integer n and an integer start, and returns void. It first creates a for loop, which loops from every integer between start and n. If the number divides n and is prime, it prints the number.

PrimeFactorTest has a main method. It first creates an integer n, which is set to the result of the readInt method with input “Enter a number, or -1 to quit: “. It then begins a while loop with condition n > 0. The loop first prints n followed by a semicolon. It then runs printPrimeFactors on inputs n and 2. It then prints a new line. For the last step, it sets n to the result of the readInt method with input “Enter a number, or -1 to quit: ”.

13. It is inefficient because it checks even numbers besides 2 to be prime factors. Because 2 is the only even prime, you can easily eliminate half the steps. It can also stop checking numbers greater than n/2, because these numbers will not divide n. To improve the efficiency, I would change the isPrime method and the printPrimeFactors method to:

public static boolean isPrime(int n)

{

for (int i = 2; i < n/2; i++)

{

if (n % i == 0)

return false;

}

return true;

}

public static void printPrimeFactors(int n)

{

if (n % 2 == 0)

System.out.print(2 + “ “);

for (int i = 3; i <= n/2; i + 2)

{

if (n % i == 0 & isPrime(i))

System.out.print i + “ “;

}

}