Review for Final Exam

1. Answer all the questions on the review sheets for the midterms, and answer all questions on the sample midterms.

2. We have discussed in detail three methods which all classes inherit from the Object class. Give the method header for each, and describe how each works.
See class notes

3. Write a Circle class in which  you override the three methods discussed in #2. What does it mean to override a method?
Done in class

4. If class A is a subclass of class B, write the header for class A.
Done in class

5. Describe how the instanceof operator is used, and then give an example use of it.
See class notes

6. Look at your Circle class, and for each instance variable and local variable, write down the scope of each.
Done in class

7. When we declare an array of objects, e.g.
Circle[] circleArray;
how much memory have we set aside? Would your answer be different if we declared an array of ints, e.g.
int[] intArray;

We've set aside 4 bytes to store the address of an array.

8. If I now allocate memory for my circleArray, e.g.
circleArray = new Circle[50];
how much memory is set aside when this statement is executed? What value is stored now in Circle[0]?

50*4 = 200 bytes

9. Write a method that takes a 2D array of type int, and swaps the entry in row 1, column 1 with the entry in the last row and column.

public static void swapEm(int[][] arr) {
    int numRows = arr.length;
    int lastRowLength = arr[numRows.length-1].length;

    int temp = arr[0][0];
    arr[0][0] = arr[numRows-1][lastRowLength-1];
    arr[numRows-1][lastRowLength-1] = temp;

10. Write a single Java statement that creates an array of 3 Strings containing the strings "hello", "world", "hello".
String[] arr = {"hello", "world", "hello"};

11. Write a method that takes a one dimensional array of doubles, and returns the sum of the array's entries. Use a for-each loop.

public static double sum(double[] arr) {
    double total = 0;
    for(double num : arr) {
        total += num;
    return total;

12. I've declared the array myArray as follows:
char[] myArray = new char[25];
Assume I've initialized the entries in myArray. Write Java code that creates a new reference variable that points to this same array.

char[] arr2 = myArray;

13. Now write Java code that creates a distinct array that has the same entries as myArray (see #12).

char[] arr = new char[25];
for(int i = 0; i < arr.length; i++) {
    arr[i] = myArray[i];

14. Write a method that takes two arrays of doubles and returns the average of the values in both arrays.

public static double avg(double[] one, double[] two) {
    double sum = 0;
    for(int i = 0; i < one.length; i++)
        sum += one[i];
    for(int i = 0; i < two.length; i++)
        sum += two[i];

    int length = one.length + two.length;
    if(length > 0)
        return sum/length;
    else return 0;

15. Write a program that includes a method that takes two arrays of integers and returns the array that contains the largest entry. The program should contain a main method that calls this method.

I leave it to you to write the main method.

public static int[] maxEntry(int[] one, int[] two) {

    if(one == null || two == null) return null;
    else if(one.length == 0) return two;
    else if(two.length == 0) return one;

    int max1 = one[0];
    int max2 = two[0];

    for(int i = 1; i < one.length; i++){
        if(max1 < one[i]) max1 = one[i];

    for(int i = 1; i < two.length; i++) {
        if(max2 < two[i]) max2 = two[i];

    if(max1 > max2) return one;
    return two;

16. Write a method union that takes two arrays a and b of integers, and returns an array that contains the integers that are in one or both arrays (a and b). The returned array should not contain duplicates.

public static int[] union(int[] a, int[] b) {
    int sizeA = a.length;
    for(int i = 1; i < a.length; i++) {
        if(a[i] == a[i-1]) sizeA--;

    int sizeB = b.length;
    for(int i = 1; i < b.length; i++) {
        if(b[i] == b[i-1]) sizeB--;

    int size = sizeA+sizeB;
    int[] u = new int[size];
    int j = 1;
    u[0] = a[0];
    for(int i = 1; i < a.length; i++) {
        if(a[i] != a[i-1]) {
            u[j] = a[i];

    for(int i = 0; i < b.length; i++) {
        if(i == 0) {
            if(notInA(a, b[i])) {
                u[j] = b[i];
        else if((b[i] != b[i-1] && notInA(a, b[i])) {
            u[j] = b[i];

    // u may be longer than the union
    int[] uFinal = new int[j];
    for(int i = 0; i < j; i++) {
        uFinal[i] = u[i];

    return uFinal;

public static boolean notInA(int[] a, int c) {
    for(int i = 0; i < a.length; i++) {
        if(c == a[i]) return false;
    return true;

17. Write a program that prompts the user for positive integers, and stops prompting when 0 or a negative value is entered. Print the average, min and max for the positive integers.

We did this in class.

18. Write a program that uses our DrawingPanel class to draw a smiley face.

19. Write a program that takes some number of integers as command-line arguments, and prints the sum of the integers.

public static void main(String[] args) {
    int sum = 0;
    for(int i = 0; i < args.length; i++)
        sum+= Integer.parseInt(args[i]);

20. Review your programming assignments - you may see questions about them on the final exam.

21. When can you use a for each loop in place of a for loop? Given this array:
int[] arr = new int[30];
// assume arr's entries have been initialized
a) Can you use a for each loop to print arr's entries?
b) Can you use a for each loop to set each of arr's entries to 1?

Yes for a). No for b).

22. Write a Square class that implements the following interface. You will need to include an instance variable to store the square's side length. Include constructors, getters and setters in your class, and over-ride the toString() method. Also write a Circle class that implements the Shapes interface.

You will not be asked about interfaces.

public interface Shapes {

    double perimeter(); // returns the perimeter of the shape

    double area(); // returns the area of the shape

23. Name two differences in abstract classes and interfaces.

24. Define polymorphism. Write a short program that creates a Shapes array of size 5, and initializes some of the entries to be Squares and some to be Circles. Call perimeter(), area() and toString() on the array entries. Then try calling getSide() on one of the array entries that refers to a Square object. What happens? Explain how polymorphism is at work in your program.

See in-class examples on polymorphism.

25. Write down the prototype of each method we have studied from the String class, and describe briefly how each is called and what it does. Give example calls for each.

26. Repeat #25 for the Character class.

27. Repeat #25 for the Random class.

28. Repeat #25 for the Scanner class.

29. Write a method that takes a 2D array of Strings, and returns the length of the shortest string in the array.

public static int shortest(String[][] arr) {
    int min = arr[0][0];

    for(int i = 0; i < arr.length; i++) {
        for(int j = 0; j < arr[i].length; j++) {
            if(min > arr[i][j].length()) min = arr[i][j].length();

    return min;

30. Consider the following method:
public static int mystery(int[] list) {
    int x = 0;
    for(int i = 1; i < list.length; i++) {
       int y = list[i] - list[0];
       if(y > x) {
          x = y;
    return x;

For each of the following arrays, indicate what this method would return if it was called on the given array.

{3, 12}
{4, 2, 10, 8}
{8, 2, 10, 4, 10, 9}

31. Write a static method isPalindrome() that takes a String as a parameter and returns true if the String is a palindrome and false otherwise. A String is a palindrome if its reverse is the same string as itself. Disregard differences in case, e.g., "Mom" and "RAdar" are palindromes.

public static boolean isPalindrome(String str) {
    if(s == null || s.length() == 0) return true;

    String s = str.toLowerCase();
    for(int i = 0; i < s.length()/2; i++) {
        if(s.charAt(i) != s.charAt(s.length()-1-i) return false;
    return true;

32. Write a static method countLastDigits() that takes an array of integers as a parameter and determines how many of the integers end in 0, how many end in 1, how many end in 2, etc. Your method will return an array of counts - the number of integers that end in 0 should be the element at index 0 in the returned array, the number of integers that end in 1 should be the element at index 1 in the array, and so forth. If the method is called on the array {9, 29, 44, 103, 2, 52, 12, 12, 86, 35, 20}, the returned array should be {1, 0, 4, 1, 1, 1, 1, 0, 0, 2} since 1 integer ends in 0 (20), no integers end in 1, 4 integers end in 2 (2, 52, 12, 12), etc.