Computer Science 1 Java - Arrays

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

C

15) Given the following declaration: int[][] m = new int[5][6]; Which of the following statements is true? A) m[0].length has the value 5 B) m[2][4] represents the element stored in the 2nd row and the 4th column of m C) The name m represents a two-dimensional array of 30 int values D) None of these are true E) m.length has the value 6

False (An ArrayList declared with no type, or declared with type Object, stores Objects, which means it can store objects of any type (since all classes have Object as an ancestor))

An ArrayList can store any type of object, but it is not possible to store objects of two different types in the same ArrayList. (True or False)

True (The add method on the ArrayList class which is used to add elements to the list, takes an Object as a parameter, so only objects can be added to an ArrayList)

An ArrayList can store only objects, not primitive types. (True or False)

A (The correct code in iterates through all 12 elements of candy, adding each value to sum)

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. Which of the following code could be used to compute the total number of bars sold by the children? A) for(int j=0; j<12; j++) sum+= candy[j]; B) for(int j=0; j<12; j++) candy[j] = sum; C) for(int j=0; j<12; j++) sum += [j]; D) for(int j=0; j<12; j++) [j] += sum; E) for(int j=0; j<12; j++) sum = candy[j];

B (Both sorting algorithms use two nested loops which both execute approximately n times apiece, giving a complexity of n * n or n2 for both)

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. A) n B) n^2 C) n^3 D) n * log n E) Insertion sort has an efficiency of n and Selection Sort has an efficiency of n^2

False (A binary search examines the middle element and moves left or right depending on whether the key is less than or greater than the middle element, so half the remaining elements are eliminated in each pass)

Each pass of a binary search eliminates approximately half the remaining elements from consideration. (True or False)

1 2 3 4 5 6 7 8 9 10 11 12

Given the array declaration below and using numbers 1, 2, 3, ..., what would myVals content look like? int[][] myVals = new int[3][] myVals[0] = new int[3]; myVals[1] = new int[5]; myVals[2] = new int[4];

12

Given the array declaration below, how many elements can the myVals store? int[][] myVals = new int[3][] myVals[0] = new int[3]; myVals[1] = new int[5]; myVals[2] = new int[4];

someNumbers[2][3] (Remember that the first index is 0)

How would you identify the location of the 99 that is stored in the array? int[][] someNumbers = {{8, 9, 10, 11}, {1, 3, 12, 15}, {5, 9, 44, 99} };

False (The "=" is an assignment operator. If the two variables are primitives, than the left-hand variable gets a copy of the right-hand variable (so if a and b are int values and b = 5, then a would become 5). However, since a and b are arrays, the reference variable a is set to the reference variable b, resulting in both a and b referencing the same array in memory, or they are now aliases of each other)

If a and b are both int arrays, then a = b; will copy all elements of b into a. (True or False)

False (Time efficiency is not measured in real time units, since it is used only to compare algorithms to each other. It is a measure of how long it takes an algorithm to run, expressed in terms of the size of the input.)

The time efficiency of an algorithm is the number of milliseconds it takes the algorithm to complete. (True or False)

4 (A one dimensional array has a length field that holds the number of elements in the array. With a two-dimensional array, the length field holds the number of rows in the array. The value of rents.length is 4 because there are four rows in the array)

Using the array rents: int[][] rents = { {400, 450, 510}, {500, 560, 630}, {625, 676, 740}, {1000, 1250, 1600} }; What is the value of rents.length?

A (Different sorting algorithms require a different number of instructions when executing. The Selection Sort for instance usually requires more instructions than the Insertion Sort. So, we compare sorting algorithms by the number of instructions that each takes to execute to sort the array. We might count the maximum number of instructions that a sorting algorithm will execute in the worst case, or the minimum number in the best case, or count on average the number of instructions executed. If two sorting algorithms require roughly the same number of instructions to sort an array, then we might also examine the amount of memory space required)

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

import java.util.ArrayList;

What import is required to use ArrayList?

5 33

What is the printout of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { System.out.print(m(values[row]) + " "); } } public static int m(int[] list) { int v = list[0]; for (int i = 1; i < list.length; i++) if (v < list[i]) v = list[i]; return v; } }

3

What is the printout of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); } }

True

When you create an array using the following statement, the element values are automatically initialized to 0. int[][] matrix = new int[5][5]; (True or False)

float elapsedTimes[] = {11.47, 12.04, 11.72, 13.88}; int[] scores = new int[30]; char grades[] = {'a', 'b', 'c', 'd', 'f'};

Which of the following are valid declarations?

A (The proper syntax to declare an ArrayList of Cars must include the type and ArrayLists do not use [])

Which of the following correctly declares an ArrayList of Car objects? A) ArrayList<Car> cars; B) ArrayList Car; C) ArrayList cars = Car; D) ArrayList[Car] cars; E) ArrayList Car[] cars;

C (The type must be on both sides of = and ArrayLists cannot store primitive types)

Which of the following is the correct syntax to construct an <code>ArrayList</code> to store integers? A) ArrayList list<integer> = new ArrayList<integer>(); B) ArrayList list = new ArrayList(); C) ArrayList<Integer> list = new ArrayList<Integer>(); D) ArrayList[int] list = new ArrayList[int](); E) ArrayList<Integer> list = new ArrayList();

C (After one pass, the array would be 2, 4, 12, 9, 6, 8, 18. The second pass would look to swap the item in array index 1 (4) with the smallest value after 2 (4). So, 4 would swap with 4 and the array would stay the same as it was after the first pass)

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 ( / used to represent spaces) A) 2, 4, 6, 8, 9, 12, 18 B) 9, 4, 12, 2, 6, 8, 18 C) 2, 4, 12, 9, 6, 8, 18 D) 2, 4, 9, 6, 12, 8, 18 E) 2, 4, 12, 6, 8, 9, 18

B (The add method adds the given item to the end of the list. alist.set(alist.size()-1, item); replaces the last element with item is not correct because it replaces the last element with item)

Which of the following statements adds item to the end of the ArrayList alist? A) alist[alist.size()-1] = item; B) alist.add(item); C) item.addtoEnd(alist); D) alist.add(item); and alist.set(alist.size()-1, item); E) alist.set(alist.size()-1, item);

A

Which of the following statements are correct? A) char[][] charArray = {{'a', 'b'}, {'c', 'd'}}; B) char[2][]charArray = {{'a', 'b'}, {'c', 'd'}}; C) char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}}; D) char[][] charArray = {'a', 'b'};

A (In a proper foreach loop. Every element in alist is assigned to the variable item in turn)

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

months.add(2, "July"); (NOTE: If the position number of the ArrayList is not a valid position, an error message will be generated)

Write the statement to insert "July" in the third position of the ArrayList months.

False (It must be sorted before you can use a binary search)

You could us a binary search to see if 66 is in the following array: int myNums[]={2, 44, 5, 66, 78, 90, 23, 66}; (True or False)

[10, 30, 40, 20, 60, 50]

public static void mystery1(ArrayList<Integer> list) for (int i = list.size() - 1; i > 0; i--) { if (list.get(i) < list.get(i - 1)) { int element = list.get(i); list.remove(i); list.add(0, element); } } System.out.println(list); } What will be the output of the code if list is [30, 20, 10, 60, 50, 40]? ( Answer in the form [#, #, #, ....]


संबंधित स्टडी सेट्स

Preguntas de Latinos en los EE.UU.

View Set

Soc 1 - Inquizitive Chapter 1 2017

View Set

Psych 21A Ch. 4 Review Questions

View Set

Life Ins. Policy Provisions, Options and Riders (ch.4)

View Set