[ Quiz Answers | Chapter Index | Main Index ]

Quiz on Chapter 7

This page contains questions on Chapter 7 of Introduction to Programming Using Java. You should be able to answer these questions after studying that chapter. Sample answers to these questions can be found here.

Question 1:

What is meant by the basetype of an array?

Question 2:

What is the purpose of the following variable-arity method? What are the values of same(1,2,3), same(17,17,17,17), and same(2)? Why?

static double same( int... value ) {
    for (int i = 1; i < value.length; i++) {
        if ( value[i-1] != value[i] )
            return false;
    }
    return true;
}

Question 3:

What does it mean to sort an array?

Question 4:

What is the main advantage of binary search over linear search? What is the main disadvantage?

Question 5:

What is meant by a dynamic array? What is the advantage of a dynamic array over a regular array?

Question 6:

What does it mean to say that ArrayList is a parameterized type?

Question 7:

Suppose that a variable strlst has been declared as

ArrayList<String> strlst = new ArrayList<String>();

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.

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++)
   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 9:

Write a complete static method that finds the largest value in an array of ints. The method 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 method.

Question 10:

Suppose that temperature measurements were made on each day of 2021 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 11:

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. Write two versions of the code segment, one that uses a regular for loop, and one that uses a for-each loop.

Question 12:

Convert the Employee class from the previous question into a record class. What changes would then need to be made to the previous question's solution?

Question 13:

Write a record class to represent dates, where a Date object contains three integer fields giving the month, day, and year of the date. The canonical constructor should throw an exception if the values for the month and day are not legal. Also include a toString() method that prints a date in a form such as "5/27/2022".

Question 14:

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 double[] sums( double[][] data ) {
    double[] answers = new double[ data.length ];
    for (int i = 0; i < data.length; i++) {
        double sum = 0;
        for (int j = 0; j < data[i].length; i++)
            sum = sum + data[i][j];
        answers[i] = sum;
    }
    return answers;
}

[ Quiz Answers | Chapter Index | Main Index ]