Chapter 6ArraysMultiple Choice1)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 aboveAnswer: D2)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]Answer: B3)Consider the following array:What is the value of myArray[myArray[1] – myArray[0]](a)7(b)9(c)-3(d)6Answer: CCopyright © 2006 Pearson Education Addison-Wesley. All rights reserved. 12 Walter Savitch •Absolute Java2/e: Chapter 6 Test Bank4)The subscript of the first indexed variable in an array is:(a)0(b)1(c)2(d)3Answer: A5)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 aboveAnswer: B6)An ArrayIndexOutOfBounds error is a:(a)compiler error(b)syntax error(c)logic error(d)all of the aboveAnswer: C7)Which of the following initializer lists correctly initializes the indexed variables of an array namedmyDoubles?(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};Answer: C8)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.Answer: D9)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]Answer: CCopyright © 2006 Pearson Education Addison-Wesley. All rights reserved.Chapter 6 Arrays 3 10)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 aboveAnswer: C11) A _________ can occur if a programmer allows an accessor method to return a reference to an arrayinstance variable.(a)short circuit(b)privacy leak(c)partially filled array(d)syntax errorAnswer: B12) A ________ loop is a good way to step through the elements of an array and perform some programaction on each indexed variable.(a)while(b)do…while(c)for(d)all of the aboveAnswer: CTrue/False1)An array is a collection of variables all of the same type.Answer:True2)An array has only one public instance variable, which is named length.Answer:True3)An arrays length instance variables value can be changed by a program.Answer:False4)An array name references a memory address.Answer:True5)You can only use array indexed variables as arguments to methods.Answer:FalseCopyright © 2004 Addison-Wesley. All rights reserved.4 Walter Savitch •Absolute Java2/e: Chapter 6 Test Bank6) A method can not change the values stored in the indexed variables of an array argument.Answer:False7)Java allows you to declare arrays with more than one index.Answer:True8)Arrays are objects that are created with new just like class objects.Answer:TrueShort Answer/Essay1)Write a Java statement that declares and creates an array of Strings named Breeds. Your arrayshould 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 toinitialize 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 arraychar[] 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 integerscontained within the array.Answer:public int sumArray(int[] a)Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.Chapter 6 Arrays 5 {int sum = 0;for(int i =0; i < a.length; i++)sum += a[i];return sum;}6)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 187)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 + " ");Copyright © 2004 Addison-Wesley. All rights reserved.6 Walter Savitch •Absolute Java2/e: Chapter 6 Test BankSystem.out.println();Answer:0 1 2 3 4 5 6 7 8 98)Create a Java method that will take any number of double arguments and return the smallest of thegroup.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;}Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.。