Review for Final Exam
The final exam will emphasize material that we have covered in class
since the last exam, but that material will not be more than 50% of the
final. The final exam is cumulative.
Topics that we have covered since midterm 2:
Command line arguments, multi-dimensional arrays, using one-dimensional
arrays for tallying problems, Arrays.toString() and
Arrays.deepToString(), defining our own types with classes (and
instantiating those classes), object-oriented concepts, subclasses and
superclasses, overriding methods from a superclass (including methods
inherited from the Object class), use of this and super, inheritance,
polymorphism, encapsulation, abstraction, instanceof operator
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. What is the type of the parameter for clone()?
3. Write a Circle class in which you override the three methods
discussed in #2. What does it mean to override
a method?
4. If class A is a subclass of class B, write the header for class A.
5. Describe how the instanceof operator is used, and then give an
example use of it.
6. Look at your Circle class, and for each instance variable and local
variable, write down the scope of each.
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;
8. Consider the declarations in #7. 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]? Have I created any Circle objects?
9. Write a method that takes a 2D array of type int, and swaps the
entry in row 1 and column 1 with the entry in the last row and last
column.
10. Write a single Java statement that creates an array of 3 Strings
containing the strings "hello", "world", "hello".
11. Write a method that takes, as its argument, a one dimensional array
of doubles, and returns the sum of the array's entries. Use a for-each
loop.
12. Write a method fileToArray() that takes a filename (a String) as
its argument, and returns an array containing all the tokens in the
file.
13. Write a method lengthArray() that takes a one-dimensional array of
Strings, and returns an array that contains the lengths of those
Strings. E.g.,
lengthArray({"hello", "hi", "cat"}) returns {5, 2, 3}.
14. Write a method that takes two arrays of doubles and returns the
average of the values in both arrays.
15. Write a program that includes a method that takes two arrays of
integers and returns the largest entry in both arrays. The
program should contain a main method that calls this method.
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.
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.
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 returns the sum of the integers on the command line.
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?
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.
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.
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.
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.
{5}
{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.
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.