Chapter 6: Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

The statement System.out.println(values[7]); will output 7 output 18 output nothing cause an ArrayOutOfBoundsException to be thrown cause a syntax error

cause an ArrayOutOfBoundsException to be thrown

Which of the following statements loops through every element in the ArrayList alist? for (Object item : alist) for (item in alist) for (Object item = null; item : alist) for (Object alist : item) for (alist : Object item)

for (Object item : alist)

Which of the following code could be used to compute the total number of bars sold by the children? for(int j=0; j<12; j++) sum+= candy[j]; for(int j=0; j<12; j++) candy[j] = sum; for(int j=0; j<12; j++) sum = candy[j]; for(int j=0; j<12; j++) sum += [j]; for(int j=0; j<12; j++) [j] += sum;

for(int j=0; j<12; j++) sum+= candy[j];

Both the Insertion Sort and the Selection Sort algorithms have efficiencies on the order of ____ where n is the number of values in the array being sorted.

n^2

If an int array is passed as a parameter to a method, which of the following would define the parameter list for the method header? (int[ ]) (int a[ ]) (int[ ] a) (int a) (a[ ])

(int[ ] a)

Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for firstEmpireBank = new BankAccount[1000]; a reference variable to the memory that stores all 1000 BankAccount entries 1000 reference variables, each of which point to a single BankAccount entry a single BankAccount entry 1000 BankAccount entries 1000 reference variables and 1000 BankAccount entries

1000 reference variables, each of which point to a single BankAccount entry

For questions 1-4, assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 What is returned by values[3]? 9 12 2 6 3

2

An int array stores the following values. Use the array to answer questions 9 - 12. 9 4 12 2 6 8 18 Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm? 9, 4, 12, 2, 6, 8, 18 4, 9, 12, 2, 6, 8, 18 2, 4, 12, 9, 6, 8, 18 2, 4, 6, 8, 9, 12, 18 2, 4, 9, 12, 6, 8, 18

2, 4, 12, 9, 6, 8, 18

Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm? 9, 4, 12, 2, 6, 8, 18 2, 4, 9, 6, 12, 8, 18 2, 4, 12, 9, 6, 8, 18 2, 4, 6, 8, 9, 12, 18 2, 4, 12, 6, 8, 9, 18

2, 4, 12, 9, 6, 8, 18

Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm? 9, 4, 12, 2, 6, 8, 18 2, 4, 6, 9, 12, 8, 18 2, 4, 6, 8, 9, 12, 18 2, 4, 6, 9, 8, 12, 18 2, 4, 6, 8, 12, 9, 18

2, 4, 6, 8, 12, 9, 18

How many passes will it take in all for Selection Sort to sort this array? 2 4 5 6 7

6

What is the value of values.length? 0 5 6 7 18

7

If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown? IndexOutOfBoundsException ArrayIndexOutOfBoundsException NegativeArraySizeException NullPointerException ArithmeticException

ArrayIndexOutOfBoundsException

Which of the following correctly declares an ArrayList of Car objects? ArrayList Car; ArrayList cars = Car; ArrayList Car[] cars; ArrayList<Car> cars; ArrayList[Car] cars;

ArrayList<Car> cars;

What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0. for(j=0;j<list.length.j++) if(list[j] < temp) c++; It finds the smallest value and stores it in temp It finds the largest value and stores it in temp It counts the number of elements equal to the smallest value in list It counts the number of elements in list that are less than temp It sorts the values in list to be in ascending order

It counts the number of elements in list that are less than temp

What does the following method do? public int question15( ) { int value1 = 0; int value2 = 0; for(int j=0; j<12; j++) if(candy[j] > value1) { value1 = candy[j]; value2 = j; } return value2; } It returns the total number of candy bars sold It returns the total number of children who sold 0 candy bars It returns the total number of children who sold more than 0 candy bars It returns the number of candy bars sold by the child who sold the most candy bars It returns the index of the child who sold the most candy bars

It returns the index of the child who sold the most candy bars

Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr? It stores 5 elements with legal indices between 1 and 5 It stores 5 elements with legal indices between 0 and 4 It stores 4 elements with legal indices between 1 and 4 It stores 6 elements with legal indices between 0 and 5 It stores 5 elements with legal indices between 0 and 5

It stores 5 elements with legal indices between 0 and 4

To initialize a String array names to store the three Strings "Huey", "Duey" and "Louie", you would do String names = {"Huey", "Duey", "Louie"}; String[ ] names = {"Huey", "Duey", "Louie"}; String[ ] names = new String{"Huey", "Duey", "Louie"}; String names[3] = {"Huey", "Duey", "Louie"}; String names; names[0] = "Huey"; names[1] = "Duey"; names[2] = "Louie";

String[ ] names = {"Huey", "Duey", "Louie"};

Which of the following is a legal way to declare and instantiate an array of 10 Strings? String s = new String(10); String[10] s = new String; String[ ] s = new String[10]; String s = new String[10]; String[ ] s = new String;

String[ ] s = new String[10];

For questions 13 - 15, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. What does the following code do? int value1 = scan.nextInt( ); int value2 = scan.nextInt( ); bars[value1] += value2; adds 1 to the number of bars sold by child value1 and child value2 adds 1 to the number of bars sold by child value1 adds value1 to the number of bars sold by child value2 adds value2 to the number of bars sold by child value1 inputs a new value for the number of bars sold by both child value1 and child value2

adds value2 to the number of bars sold by child value1

Which of the following statements adds item to the end of the ArrayList alist? alist.add(item); alist.set(alist.size()-1, item); alist[alist.size()-1] = item; item.addtoEnd(alist); Both a and b.

alist.add(item);

Which of the following expressions gives the last element in the ArrayList alist? alist.last() alist.get(alist.last()) alist.get(alist.length()) alist.get(alist.size()) alist.get(alist.size()-1)

alist.get(alist.size()-1)

If x is a char, and values is an int array, then values[x] causes a syntax error causes an Exception to be thrown casts x as an int based on x's position in the alphabet (for instance, if x is 'a' then it uses 0 and if x is 'z' then it uses 25) casts x as an int based on x's Unicode value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122) casts x as an int based on the digit that is stored in x (for instance, if x is '3' it uses 3) but throws an exception if x does not store a digit

casts x as an int based on x's Unicode value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122)

Which of the following loops would adequately add 1 to each element stored in values? for(j=1;j<values.length;j++) values[j]++; for(j=0;j<values.length;j++) values[j]++; for(j=0;j<=values.length;j++) values[j]++; for(j=0;j<values.length-1;j++) values[j]++; for(j=1;j<values.length-1;j++) values[j]++;

for(j=0;j<values.length;j++) values[j]++;

To declare a two-dimensional int array called threeD, which of the following would you use? int[2] twoD; int[ , ] twoD; int[ ][ ] twoD; int [ [ ] ] twoD; int[ ] twoD[2];

int[ ][ ] twoD;

The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only. foo = 0; for(j=0; j<list.length; j++) if (list[j] > foo) foo = list[j]; it stores the smallest value in list (the minimum) in foo it stores the largest value in list (the maximum) in foo it stores every value in list, one at a time, in foo, until the loop terminates it counts the number of elements in list that are greater than foo it counts the number of elements in list that are less than foo

it stores the largest value in list (the maximum) in foo

In Java, arrays are primitive data types objects interfaces primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object Strings

objects

The "off-by-one" error associated with arrays arises because the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far the last array index is at length + 1 and loops may only iterate to length, missing one the last array element ends at length - 1 and loops may go one too far programmers write a loop that goes from 0 to length - 1 whereas the array actually goes from 1 to length none of the above, the "off-by-one" error has nothing to do with arrays

the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far

We compare sorting algorithms by examining the number of instructions executed by the sorting algorithm the number of instructions in the algorithm itself (its length) the types of loops used in the sorting algorithm the amount of memory space required by the algorithm whether the resulting array is completely sorted or only partially sorted

the number of instructions executed by the sorting algorithm


Ensembles d'études connexes

Acceleration Down an Incline Lab

View Set

CHAPTER 6: Creating a "More Perfect Union" 1783-1800 chương 6 : tạo ra một liên minh hoàn hảo hơn 1783-1800

View Set

N406 Exam 2 PrepU Questions (KIDNEYS)

View Set