Page contents

Absolute Java 5th Edition by Walter Savitch –  Test Bank

Instant delivery only

  • ISBN-10 ‏ : ‎ 0132830310
  • ISBN-13 ‏ : ‎ 978-0132830317

In Stock

Original price was: $60.00.Current price is: $28.00.

Add to Wishlist
Add to Wishlist
Compare
SKU:tb1002056

Absolute Java 5th Edition by Walter Savitch –  Test Bank

Chapter 6
Arrays
 Multiple Choice
1) The individual variables that together make up the array are referred to as:
(a) indexed variables
(b) subscripted variables
(c) elements of the array
(d) all of the above
Answer: D
Answer: What is the correct expression for accessing the 5th element in an array named colors?
(e) colors[3]
(f) colors[4]
(g) colors[5]
(h) colors[6]
Answer: B
Answer: Consider the following array:
myArray[0] 7
myArray[1] 9
myArray[2] -3
myArray[3] 6
myArray[4] 1
myArray[5] -1

What is the value of myArray[myArray[1] – myArray[0]]
(i) 7
(j) 9
(k) -3
(l) 6
Answer: C
Answer: The subscript of the first indexed variable in an array is:
(m) 0
(n) 1
(o) 2
(p) 3
Answer: A
Answer: The correct syntax for accessing the length of an array named Numbers is:
(q) Numbers.length()
(r) Numbers.length
(s) both A and B
(t) none of the above
Answer: B
Answer: An ArrayIndexOutOfBounds error is a:
(u) compiler error
(v) syntax error
(w) logic error
(x) all of the above
Answer: C
Answer: Which of the following initializer lists correctly initializes the indexed variables of an array named myDoubles?
(y) double myDoubles[double] = {0.0, 1.0, 1.5, 2.0, 2.5};
(z) double myDoubles[5] = new double(0.0, 1.0, 1.5, 2.0, 2.5);
(aa) double[] myDoubles = {0.0, 1.0, 1.5, 2.0, 2.5};
(bb) array myDoubles[double] = {0.0, 1.0, 1.5, 2.0, 2.5};
Answer: C
Answer: The base type of an array may be all of the following but:
(cc) string
(dd) boolean
(ee) long
(ff) all of these may be a base type of an array.
Answer: D
Answer: The correct syntax for passing an array as an argument in a method is:
(gg) a[]
(hh) a()
(ii) a
(jj) a[0]..a[a.length]
Answer: C
Answer: Partially filled arrays require:
(kk) a variable to track the number of array positions used
(ll) the use of local variables
(mm) the use of global variables
(nn) all of the above
Answer: A
Answer: Java provides a looping mechanism for objects of a collection. This looping mechanism is called a __________ loop.
(oo) While
(pp) For
(qq) For each
(rr) All of the above
Answer: C
Answer: A _________ can occur if a programmer allows an accessor method to return a reference to an array instance variable.
(ss) short circuit
(tt) privacy leak
(uu) partially filled array
(vv) syntax error
Answer: B
Answer: The name of the sorting algorithm that locates the smallest unsorted value in an array and places it in the next sorted position of the array is called:
(ww) bubble sort
(xx) merge sort
(yy) radix sort
(zz) selection sort
Answer: D
Answer: A value of an enumerated type is similar to a/an:
(aaa) Instance variable
(bbb) Object of a class
(ccc) Named constant
(ddd) None of the above
Answer: C
Answer: An array with more than one index is called a/an:
(eee) partially filled array
(fff) multidimensional array
(ggg) bidirectional array
(hhh) one dimensional array
Answer: B
Answer: A type of array in which different rows can have different number of columns is called a/an:
(iii) partially filled array
(jjj) ragged array
(kkk) initialized array
(lll) none of the above
Answer: B
Answer: A ________ loop is a good way to step through the elements of an array and perform some program action on each indexed variable.
(mmm) while
(nnn) do…while
(ooo) for
(ppp) all of the above
Answer: C
 True/False
1) An array is a collection of variables all of the same type.
Answer: True
Answer: An array has only one public instance variable, which is named length.
Answer: True
Answer: An arrays length instance variables value can be changed by a program.
Answer: False
Answer: An array of chars is the same as a String in Java.
Answer: False
Answer: An array name references a memory address.
Answer: True
Answer: You can only use array indexed variables as arguments to methods.
Answer: False
Answer: A method can not change the values stored in the indexed variables of an array argument.
Answer: False
Answer: A collection class is a class whose objects store a collection of values.
Answer: True
Answer: You may cycle through elements of a collection object using a for loop.
Answer: False
Answer: In a vararg specification the ellipsis is not part of the Java syntax.
Answer: False
Answer: A variable of an enumerated type can have the special value null.
Answer: True
Answer: Java allows you to declare arrays with more than one index.
Answer: True
Answer: A one dimensional array is also called an array of arrays.
Answer: False
Answer: Arrays are objects that are created with new just like class objects.
Answer: True
 Short Answer/Essay
1) Write a Java statement that declares and creates an array of Strings named Breeds. Your array should be large enough to hold the names of 100 dog breeds.
Answer: String[] Breeds = new String[100];
2) Declare and create an integer array that will contain the numbers 1 through 100. Use a for loop to initialize the indexed variables.
Answer:
int[] wholeNumbers = new int[100];
for(int i = 0; i < 100; ++i)
wholeNumbers[i] = i + 1;
3) What are three ways you can use the square brackets [ ] with an array name?
Answer: First, the square brackets can be used to create a type name, such as double[] score. Second, the square brackets can be used with an integer value as part of the special syntax Java uses to create a new array, as in score = new double[5]. The third use of square brackets is to name an indexed variable of the array, such as score[1].
4) Given the following character array
char[] h = {‘H’, ‘E’, ‘L’, ‘L’, ‘O’};
Write a Java statement that will create a new String object from the character array.
Answer: String s = new String(h);
5) Write a Java method that takes an integer array as a formal parameter and returns the sum of integers contained within the array.
Answer:
public int sumArray(int[] a)
{
int sum = 0;
for(int i =0; i < a.length; i++)
sum += a[i];
return sum;
}
6) How are arrays tested to see if they contain the same contents?
Answer: The best way to test two arrays to see if the contents are the same is to write a method that accomplishes the task. This method would iterate through each array comparing indexed variables. If arrays are tested for equality using the == operator, you are only testing to see if the arrays reference the same memory location. It is quite common for two arrays to reference different memory locations, but contain the same elements.
7) Explain what the main methods array parameter, args, is used for.
Answer: Args allows the program to obtain input from the command line at runtime. To take advantage of this feature, the program is invoked with the java command, followed by the program name, and a list of arguments. Java stores these arguments in the args[] array.
8) Write a Java method as well as any facilitator methods needed to perform a sort on an array of whole numbers in descending order.
Answer:
/**
Precondition: The array has values.
Action: Sorts a so that a[0] >= a[1] >= … >= a[a.length]
*/
public static void selectionSort(int[] a)
{
int indexOfLargest;
for(int i=0; i < a.length; ++i)
{
indexOfLargest = LargestIndex(i, a);
swap(i, indexOfLargest, a);
}
}
/**
Returns the index of the largest value among
a[start], a[start + 1], … a[a.length-1]
*/
private static int LargestIndex(int start, int[] a)
{
int max = a[start];
int indexOfMax = start;
for(int i=start + 1; i < a.length; i++)
{
if(a[i] > max)
{
max = a[i];
indexOfMax = i;
}
}
return indexOfMax;
}
/**
Precondition: i and j are legal indices for the array a.
Postcondition: Values of a[i] and a[j] have been interchanged
*/
private static void swap(int i, int j, int[] a)
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
9) Discuss how you could represent a table of related records using a multidimensional array.
Answer: If a multidimensional array is viewed as a structure with rows and columns, one can easily see how it can be used to represent a table of related records. For example, a person has a name, street address, city, state and zip code. An array with 5 columns and as many rows as needed can be created to handle such a structure. To retrieve the information regarding a specific person, the array can be probed at a specific row iterating through the columns to retrieve the information.
10) Declare and create a multidimensional array to hold the first and last names of 10 people.
Answer: String[][] Names = new String[10][2];
11) Declare and create a 10 x 10 multidimensional array of doubles.
Answer: Double[][] myDoubles = new Double[10][10];
12) Initialize the array created in number 11 above to -1.0.
Answer:
for(int row = 0; row < myDoubles.length; row++)
for(int column = 0; column < myDoubles[row].length; column++)
myDoubles[row][column] = -1.0;
13) Write a complete Java console application that prompts the user for a series of quiz scores. The user should type -1 to signify that the input of quiz scores is complete. Your program should then average the scores and display the result back to the user.
Answer:
import java.util.*;
public class Average
{
public static void main(String args[])
{
Scanner stdin = new Scanner(System.in);
int quizGrade = 0;
int quizTotal = 0;
int quizCount = 0;
while(quizGrade != -1)
{
System.out.println(“Enter the quiz grade or -1 to exit”);
quizGrade = stdin.nextInt();
quizTotal += quizGrade;
quizCount++;
}
System.out.println(“The quiz average is ” +
QuizAverage(quizTotal, quizCount));
}
public static double QuizAverage(int total, int count)
{
return total/count;
}
}
14) What is the output of the following code?
int[] numbers = new int[10];
for(int i=0; i < numbers.length; ++i)
numbers[i] = i * 2;
for(int i=0; i < numbers.length; ++i)
System.out.print(numbers[i] + ” “);
System.out.println();
Answer: 0 2 4 6 8 10 12 14 16 18
15) What is the output of the following code?
int[] numbers = new int[10];
for(int i=0; i < numbers.length; ++i)
numbers[i] = i * 2;
for(int i=0; i < numbers.length; ++i)
System.out.print(numbers[i] / 2 + ” “);
System.out.println();
Answer: 0 1 2 3 4 5 6 7 8 9
16) Write Java statements to create a collection of integers, and to initialize each element of the collection to -1 using a for each statement.
int[] x = new int[10];
for(int element: x)
element = -1;
17) Create a Java method that will take any number of double arguments and return the smallest of the group.
public static double min(double… arg)
{
if(arg.length == 0)
{
System.out.println(“Fatal Error: minimum of zero values.”);
System.exit(0);
}
double smallest = arg[0];
for(int i = 1; i < arg.length; i++)
if(arg[i] < smallest)
smallest = arg[i];
return smallest;
}
18) Write a Java statement that creates an enumerated type called months that contains the twelve different months in a year.
enum Months {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER,
DECEMBER};
19) Write a Java statement that prints the month July to the console window from the enumerated list crated in question number 18 above.
System.out.println(Months.JULY);

Reviews

There are no reviews yet.

Write a review

Your email address will not be published. Required fields are marked *

Product has been added to your cart