Home  | 


 ONLINE SESSION     E  

Covering Gaddis chapter 7 

 

Answer the following multiple choice questions for chapter 7

Multiple Choice  ch 7

  1. In an array declaration, this indicates the number of elements that the array will have.
    1. subscript
    2. size declarator
    3. element sum
    4. reference variable
  1. Each element of an array is accessed by a number known as a(n)      .
    1. subscript
    2. size declarator
    3. address
    4. specifier
  1. The first subscript in an array is always      _.
    1. 1
    2. 0
    3. -1
    4. 1 less than the number of elements
  1. The last subscript in an array is always      .
    1. 100
    2. 0
    3. -1
    4. 1 less than the number of elements
  1. Array bounds checking happens      .
    1. when the program is compiled
    2. when the program is saved
    3. when the program runs
    4. when the program is loaded into memory
  1. This array field holds the number of elements that the array has.
    1. size
    2. elements
    3. length
    4. width
  1. To insert an item at a specific location in an ArrayList object, you use this method.
    1. store
    2. insert
    3. add
    4. get
  1. To delete an item from an ArrayList object, you use this method.
    1. remove
    2. delete
    3. erase
    4. get
  1. To determine the number of items stored in an ArrayList object, you use this method.
    1. size
    2. capacity
    3. items
    4. length

 

 

 DEFINE THE FOLLOWING TERMS

 

Arrays

Subscripts

String arrays

Passing arguments in arrays

Passing an array

Arraylist

Boolean

REPRESENTATIVE SAMPLE PROGRAMS FOR CHAPTER 7

SIMILAR PROGRAMS  are in the chapter examples that you downloaded

optional - not to be turned in

type in your name and RUN THEM  using JGrasp


 

THE FIRST  USES ARRAYS

import java.util.Scanner;


/**
Regular wages only.
*/

public class Regular
{
public static void main(String[] args)
{


int[] hours = new int[5];
double[] rate = new double[5];
double[] wages = new double[5];

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the hours worked by each employee.
System.out.println("Enter the hours worked.");

for (int index = 0; index < 5; index++)
{
System.out.print( "Employee #" + (index + 1) + ": ");
hours[index] = keyboard.nextInt();
}

// Get the pay rate for each employee.
System.out.println("Enter the pay rate.");

for (int index = 0; index < 5; index++)
{
System.out.print( "Employee #" + (index + 1) + ": ");
rate[index] = keyboard.nextDouble();
}


// Display each employee's gross pay.
System.out.println( "Here is the gross pay for each employee:");


for (int index = 0; index < 5; index++)
{
wages[index] = hours[index] * rate[index];

System.out.println("Employee #" + (index + 1) +
": $" + wages[index]);
}
}
}

 


 

 

THE SECOND   USES ARRAYLIST

-------------------------------------------------------------------------------------

import java.util.ArrayList; // Needed for ArrayList class

/**
This program demonstrates inserting an item.
*/

public class ArrayListInsert
{
public static void main(String[] args)
{
// Create an ArrayList to hold some names.
ArrayList<String> nameList = new ArrayList<String>();

// Add some names to the ArrayList.
nameList.add("James");
nameList.add("Catherine");
nameList.add("Bill");

// Display the items in nameList and their indices.
for (int index = 0; index < nameList.size(); index++)
{
System.out.println("Index: " + index + " Name: " +
nameList.get(index));
}

// Now insert an item at index 1.
nameList.add(1, "Mary");

System.out.println("Mary was added at index 1. " +
"Here are the items now.");

// Display the items in nameList and their indices.
for (int index = 0; index < nameList.size(); index++)
{
System.out.println("Index: " + index + " Name: " +
nameList.get(index));
}
}
}

HINTS FOR CHAPTER  7

Read the following hints for correcting your JAVA programs

CH 7 HINTS

The following list describes several errors that are commonly committed when learning this chapter’s topics:

·         Using an invalid subscript. Java does not allow you to use a subscript value that is outside the range of valid subscripts for an array.

·         Confusing the contents of an integer array element with the element’s subscript. An element’s subscript and the value stored in the element are not the same thing. The subscript identifies an element, which holds a value.

·         Causing an off-by-one error. When processing arrays, the subscripts start at zero and end at one less than the number of elements in the array. Off-by-one errors are commonly caused when a loop uses an initial subscript of one and/or uses a maximum subscript that is equal to the number of elements in the array.

·         Using the = operator to copy an array. Assigning one array reference variable to another with the = operator merely copies the address in one variable to the other. To copy an array, you should copy the individual elements of one array to another.

·         Using the == operator to compare two arrays. You cannot use the == operator to compare two array reference variables and determine whether the arrays are equal. When you use the ==operator with reference variables, the operator compares the memory addresses that the variables contain, not the contents of the objects referenced by the variables.

·        ·        

 | 

 

Chapter 7

Multiple Choice

 

1. b

2. a

3. b

4. d

5. c

6. c

7. c

8. a

9. a