Javanotes 5.1.2, Answers For Quiz On Chapter 7
Javanotes 5.1.2, Answers For Quiz On Chapter 7
Question 1: What does the computer do when it executes the following statement?
Try to give as complete an answer as possible.
Color[] palette = new Color[12];
Answer: The base type of an array refers to the type of the items that can be
stored in that array. For example, the base type of the array in the
previous problem is Color .
Answer: To sort an array means to rearrange the items in the array so that they
are in increasing or decreasing order (according to some criterion).
Question 4: What is the main advantage of binary search over linear search? What is
the main disadvantage?
Answer: The advantage of binary search is that it is much faster. On a list of one
million items, linear search would take an average of five hundred
thousand steps to find an item, whereas binary search would take only
20 steps. The disadvantage is that binary search only works on a sorted
list, so some extra work must be done to keep the sort the list or to
keep the list in sorted order as it is created.
Answer: A dynamic array is like an array in that it is a data structure that stores
a sequence of items, all of the same type, in numbered locations. It is
different from an array in that there is no preset upper limit on the
number of items that it can contain. This is an advantage in situations
where a reasonable value for the size of the array is not known at the
time it is created.
Assume that the list is not empty and that all the items in the list are
non-null. Write a code segment that will find and print the string in the
list that comes first in lexicographic order. How would your answer
change if strlst were declared to be of type ArrayList instead of
ArrayList<String> ?
Answer: Strings can be compared for lexicographic order using the compareTo
instance method in the String class. We can find the smallest string as
follows:
String smallest = strlst.get(0);
for (int i = 1; i < strlist.size(); i++) {
String nextString = strlst.get(i);
if ( nextString.compareTo(smallest) < 0 )
smallest = nextString;
}
System.out.println("The smallest string
lexicographically is " + smallest);
Question 7: What is the purpose of the following subroutine? What is the meaning of
the value that it returns, in terms of the value of its parameter?
static String concat( String[] str ) {
if (str == null)
return "";
String ans = "";
for (int i = 0; i < str.length; i++) {
ans = ans + str[i];
return ans;
}
Answer: The purpose of the subroutine is to chain all the strings in an array of
strings into one long string. If the array parameter is null, then there are
no strings, and the empty string is returned. Otherwise, the value
returned is the string made up of all the strings from the array. For
example, if stringList is an array declared as
String[] stringList = { "Put 'em ", "all", " together"
};
Question 8: Show the exact output produced by the following code segment.
char[][] pic = new char[6][6];
for (int i = 0; i < 6; i++)
Answer: The output consists of six lines, with each line containing six characters.
In the first line, i is 0, so all the characters are *'s. In the last line, i is 5,
so all the characters are *'s. In each of the four lines in the middle, one
of the characters is a * and the rest are periods. The output is
******
.*....
..*...
...*..
....*.
******
It might help to look at the array items that are printed on each line.
Note that pic[row][col] is '*' if row is 0 or if row is 5 or if row and col
are equal.
pic[0][0] pic[0][1] pic[0][2] pic[0][3] pic[0][4]
pic[0][5]
pic[1][0] pic[1][1] pic[1][2] pic[1][3] pic[1][4]
pic[1][5]
pic[2][0] pic[2][1] pic[2][2] pic[2][3] pic[2][4]
pic[2][5]
pic[3][0] pic[3][1] pic[3][2] pic[3][3] pic[3][4]
pic[3][5]
pic[4][0] pic[4][1] pic[4][2] pic[4][3] pic[4][4]
pic[4][5]
pic[5][0] pic[5][1] pic[5][2] pic[5][3] pic[5][4]
pic[5][5]
Question 9: Write a complete subroutine that finds the largest value in an array of
ints . The subroutine should have one parameter, which is an array of
type int[] . The largest number in the array should be returned as the
value of the subroutine.
Question 10: Suppose that temperature measurements were made on each day of
1999 in each of 100 cities. The measurements have been stored in an
array
int[][] temps = new int[100][365];
where temps[c][d] holds the measurement for city number c on the dth
day of the year. Write a code segment that will print out the average
temperature, over the course of the whole year, for each city. The
average temperature for a city can be obtained by adding up all 365
measurements for that city and dividing the answer by 365.0.
Adding up all the temperatures for a given city itself requires a for loop,
so the code segment looks like this:
for (int city = 0; city < 100; city++) {
int total = 0; // total of temperatures for this
city
for (int day = 0; day < 365; day++)
total = total + temps[city][day];
double avg = total / 365.0; // average temp for
this city
System.out.println("Average temp for city number "
+ city + " is " + avg);
}
Write a code segment that will output the first name, last name, and
hourly wage of each employee who has been with the company for 20
years or more.
Answer: (The data for the i-th employee is stored in an object that can be
referred to as employeeData[i] . The four pieces of data about that
employee are members of this object and can be referred to as:
employeeData[i].firstName
employeeData[i].lastName
employeeData[i].hourlyWage
employeeData[i].yearsWithCompany
The code segment uses a for loop to consider each employee in the
array.)
for (int i=0; i < 100; i++) {
if ( employeeData[i].yearsWithCompany >= 20 )
System.out.println(employeeData[i].firstName + "
" +
employeeData[i].lastName + ": " +
employeeData[i].hourlyWage);
}
Question 12: Suppose that A has been declared and initialized with the statement
double[] A = new double[20];
and suppose that A has already been filled with 20 values. Write a
program segment that will find the average of all the non-zero numbers
in the array. (The average is the sum of the numbers, divided by the
number of numbers. Note that you will have to count the number of
non-zero entries in the array.) Declare any variables that you use.
Answer: (There is one problem with this problem. What happens if all the entries
in the array A are zero? In that case, the number of non-zero entries is
zero, and the average of non-zero entries is undefined. In my answer, I
assign the "undefined" value, Double.NaN , to the average in this case,
but this is somewhat arbitrary.)
int nonzeroCt = 0; // The number of non-zero entries in
the array.
double total = 0; // The total of all the grades in
the array.
double average; // The average of the non-zero
entries in the array.
for (int i = 0; i < 20; i++) {
if (A[i] != 0) { // Process this non-zero
entry.
total += A[i]; // Add it to the total.
nonzeroCt++; // Count it.
}
}
if (nonzeroCt > 0)
average = total / nonzeroCt;
else // (The average is undefined in this case)
average = Double.NaN;