Chapter 6 Arrays – absolute java test bank
Download file with the answers
Chapter 6 Arrays - absolute java test bank
1 file(s) 102.63 KB
Not a member!
Create a FREE account here to get access and download this file with answers
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
2) What is the correct expression for accessing the 5th element in an array named colors?
(a) colors[3]
(b) colors[4]
(c) colors[5]
(d) colors[6]
3) 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]]
(a) 7
(b) 9
(c) -3
(d) 6
4) The subscript of the first indexed variable in an array is:
(a) 0
(b) 1
(c) 2
(d) 3
5) The correct syntax for accessing the length of an array named Numbers is:
(a) Numbers.length()
(b) Numbers.length
(c) both A and B
(d) none of the above
6) An ArrayIndexOutOfBounds error is a:
(a) compiler error
(b) syntax error
(c) logic error
(d) all of the above
7) Which of the following initializer lists correctly initializes the indexed variables of an array named myDoubles?
(a) double myDoubles[double] = {0.0, 1.0, 1.5, 2.0, 2.5};
(b) double myDoubles[5] = new double(0.0, 1.0, 1.5, 2.0, 2.5);
(c) double[] myDoubles = {0.0, 1.0, 1.5, 2.0, 2.5};
(d) array myDoubles[double] = {0.0, 1.0, 1.5, 2.0, 2.5};
8) The base type of an array may be all of the following but:
(a) string
(b) boolean
(c) long
(d) all of these may be a base type of an array.
9) The correct syntax for passing an array as an argument in a method is:
(a) a[]
(b) a()
(c) a
(d) a[0]..a[a.length]
10) Partially filled arrays require:
(a) a variable to track the number of array positions used
(b) the use of local variables
(c) the use of global variables
(d) all of the above
11) Java provides a looping mechanism for objects of a collection. This looping mechanism is called a __________ loop.
(a) While
(b) For
(c) For each
(d) All of the above
12) A _________ can occur if a programmer allows an accessor method to return a reference to an array instance variable.
(a) short circuit
(b) privacy leak
(c) partially filled array
(d) syntax error
13) 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:
(a) bubble sort
(b) merge sort
(c) radix sort
(d) selection sort
14) A value of an enumerated type is similar to a/an:
(a) Instance variable
(b) Object of a class
(c) Named constant
(d) None of the above
15) An array with more than one index is called a/an:
(a) partially filled array
(b) multidimensional array
(c) bidirectional array
(d) one dimensional array
16) A type of array in which different rows can have different number of columns is called a/an:
(a) partially filled array
(b) ragged array
(c) initialized array
(d) none of the above
17) A ________ loop is a good way to step through the elements of an array and perform some program action on each indexed variable.
(a) while
(b) do…while
(c) for
(d) all of the above
18) This type of loop provides a convenient mechanism to iterate through every element of an array.
(a) for each
(b) for
(c) do while
(d) while
True/False
1) An array is a collection of variables all of the same type.
2) An array has only one public instance variable, which is named length.
3) An arrays length instance variables value can be changed by a program.
4) An array of chars is the same as a String in Java.
5) An array name references a memory address.
6) You can only use array indexed variables as arguments to methods.
7) A method can not change the values stored in the indexed variables of an array argument.
8) A collection class is a class whose objects store a collection of values.
9) You may cycle through elements of a collection object using a for loop.
10) In a vararg specification the ellipsis is not part of the Java syntax.
11) A variable of an enumerated type can have the special value null.
12) Java allows you to declare arrays with more than one index.
13) A one dimensional array is also called an array of arrays.
14) Arrays are objects that are created with new just like class objects.
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);
Leave a reply