Quiz Questions
For Chapter 8
THIS PAGE CONTAINS A SAMPLE quiz on material from
Chapter 8 of this on-line
Java textbook. You should be able to answer these questions after
studying that chapter. Sample answers to all the quiz questions can
be found here.
Question 1:
What does the computer do when it executes the following statement?
Try to give as complete an answer as possible.
Color[] pallette = new Color[12];
Question 2:
What is meant by the basetype of an array?
Question 3:
What does it mean to sort an array?
Question 4:
What is meant by a dynamic array? What is the advantage of
a dynamic array over a regular array?
Question 5:
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 null;
String ans = "";
for (int i = 0; i < str.length; i++) {
ans = ans + str[i];
return ans;
}
Question 6:
Show the exact output produced by the following code segment.
char[][] pic = new char[6][6];
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++) {
if ( i == j || i == 0 || i == 5 )
pic[i][j] = '*';
else
pic[i][j] = '.';
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++)
System.out.print(pic[i][j]);
System.out.println();
}
Question 7:
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 8:
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.
Question 9:
Suppose that a class, Employee, is defined as follows:
class Employee {
String lastName;
String firstName;
double hourlyWage;
int yearsWithCompany;
}
Suppose that data about 100 employees is already stored in an
array:
Employee[] employeeData = new Employee[100];
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.
Question 10:
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.