Midterm 2 review sheet - cs 313e
Selected Solutions. Many answers can be found in the online class notes.
This review sheet is not complete. The exam will cover everything we have covered all semester, through ArrayLists.
1. If binary search is used to search the array containing -8, 6, 12, 100, 18 for the key -3, what happens? Explain your answer.
2. If binary search is used to search the array containing 22, 75, 81,
100, 150, 211, 290 for the key 211, what happens? Which array elements
are compared to the key during the search?
3. Write a insertionSort method.
4. Write a test that uses your insertionSort method to sort the array of integers given in question 1. Print the sorted array.
5. Write a main method that prints an error message if there aren't 10
command line arguments, and otherwise prints the average of the 10
floating-point numbers to the screen.
public static void main(String args)
if(args.length != 10) System.out.println("Error");
double sum = 0;
for(int i = 0; i < args.length; i++)
sum += Double.parseDouble(args[i]);
6. Write a line of Java code that prints "array uninitialized" if the
array reference for arr doesn't point to an array yet, and otherwise
prints all elements of the array.
... // more code
// your code goes here
if(arr == null) System.out.println("array uninitialized");
for(int i = 0; i < arr.length; i++)
for(int j = 0; j < arr[i].length; i++)
System.out.print(a[i][j] + " ");
7. Write Java code that reads a file called "definitions.txt" and prints the number of lines in the file.
// method must advertise IOException
File file = new File("definitions.txt");
Scanner scan = new Scanner(file);
int count = 0;
8. Assume that the file "definitions.txt" contains lines that have a
word, followed by a dash, and then a definition (one word and
definition per line). Print the words (only) to a file called
9. Call the toString() method from the Arrays class on the array mentioned in #1.
10. Call the equals() method from the Arrays class on the arrays given
in #1 and #2. Print a message indicating whether the arrays are equal.
11. Write one line of Java code that creates a 2-dimensional array of
ints. The first row contains 2, 3, and 1, the second row contains -5
and 7, and the 3rd row contains 1, 11, 111, and 1111.
12. What is the process of abstraction? Give an example.
13. What is generalization?
14. Draw an inheritance hierarchy for these classes in UML: Shape,
Rectangle, Square, Circle, Triangle, RightTriangle, ShapeWithThreeSides
15. How is a module represented in Java?
16. What is encapsulation, and how is it implemented in Java?
17. What are the differences between an abstract class and an interface?
18. Write your own interface, and then write a class that implements it.
19. Create an ArrayList of Rectangle objects, and then use a loop to print the area of each rectangle in the list.
20. Using the Circle example we did in class, re-write the CircleMain
class so that your program reads 5 radius values from the user, and
only prints the area of the circle if the radius value is positive.
Otherwise your program just prints an error message.