CS 1713-001 Introduction to Computer Science
Spring 2001 -- Review for Exam 2 (Friday, 13 April 2001)

ALONG WITH ANSWERS TO REVIEW QUESTIONS
(Note: There are many possible correct answers.)

Practice Problems:

  1. Assume that grades is an array of ints. Write a Java code segment to accomplish each of the following:
    1. Initialize grades so that it has the values 58, 92, 86, 75, 48, 79, and 67 (in that order).
    2. Add 3 to each element in grades.
    3. Square each element of grades.
    4. Find the largest value in grades.
    5. Find the position of the largest value in grades.
    6. Use the book's insertionSort to sort grades into order.
    Answers to Part 1.

  2. Assume that fruits is an array of Strings. Write a Java code segment to accomplish each of the following:
    1. Initialize fruits so that it has the values banana, orange, grapefruit, strawberry, apple, and melon (in that order).
    2. Set the element in position 5 (the 6th element) of fruits to watermelon
    3. Find the first element of fruits, that is, the element that comes first in alphabetic order. (Use the method compareTo.)
    4. Sort fruits into order by rewriting the book's insertionSort code, using the compareTo method of the String class to compare the strings.
    Answers to Part 2.

  3. Write the following public methods.
    1. public boolean match(int [] a, int [] b) returns true if a and b are of the same length and their elements match.
    2. public boolean match(String [] a, String [] b).
    3. public int countValues(int [] a, int value) returns the number of times value appears in the array a.
    4. public int linearSearch(String [] a, String name) returns the position of an item that matches name in the array a.
    Answers to Part 3.

  4. Output the number of times each of the numbers 0 through 9 appear in a stream of 1000 random numbers that range from 0 to 19.
    Answer to Part 4.

Revision date: 2001-04-07. (Use ISO 8601, an International Standard.)