7A

18 flashcardsby Aniqa
Study these flashcardsFree · spaced repetition · iPhone, Android, Mac & web

All cards

1
A search algorithm

  • arranges elements in ascending order.

  • is rarely used with arrays.

  • is rarely used with arrays.
  • is used to locate a specific item in a collection of data.
2
Once created, an array size is fixed and cannot be changed.
  • True
  • False
3
A(n) ________ is used as an index to pinpoint a specific element within an array.
  • Question 2 options:

    subscript
  • element
  • boolean value
  • range
4
In order to do a binary search on an array
  • the values of the array must be numeric.

  • the array must first be sorted.
  • you must first do a sequential search to be sure the element you are looking for is there.

  • All of these are true
5
The Java compiler will display an error message when it processes a statement that uses an invalid subscript.
  • True
  • False
6
The binary search algorithm

  • Will have a maximum number of comparisons equal to the number of elements in the array.

  • is less efficient than the sequential search algorithm.
  • will, normally, have the number of comparisons that is half the number of elements in the array.

  • will cut the portion of the array being searched in half each time it fails to locate the search value.
7
The sequential search algorithm
  • returns 1 if the value being searched for is found or -1 if the value is not found.
  • uses a loop to sequentially step through an array, starting with the first element.

  • requires the array to be ascending order.
  • All of these are true
8
To determine if two arrays are equal you must compare each of the elements of the two arrays.
  • true
  • false
9
What does the following statement do?
double[] array1 = new double[10];

  • It will allow valid subscripts in the range of 0 through 9.
  • It declares array1 to be a reference to an array of double values.
  • It creates an instance of an array of ten double values.
  • It does all of these.
10
int [ ] numbers = {50, 10, 15, 20, 25, 100, 30};
int value = 0;
for (int i = 0; i < numbers.length; i++)
value += numbers[i];
  • The value variable will contain the sum of all the values in the numbers array.
  • The value variable will contain the average of all the values in the numbers array.
  • The value variable will contain the lowest value in the numbers array.
  • The value variable will contain the highest value in the numbers array.
11
What would be the result of executing the following code?
int[] x = {0, 1, 2, 3, 4, 5};

  • An array of 6 values, ranging from 0 through 5 and referenced by the variable x will be created.
  • The variable x will contain the values 0 through 5.
  • A compiler error will occur.
  • An array of 6 values, all initialized to 0 and referenced by the variable x will be created.
12
When an array is passed to a method

  • the method has direct access to the original array.
  • it is passed just as any object would be passed
  • a reference to the array is passed.
  • All of these are true.
13
Which of the following statements is(are) true about this code?
final int ARRAY_SIZE = 10;
int[] array1 = new int[ARRAY_SIZE];
  • It will allow valid subscripts in the range of 0 through 9.
  • It creates an instance of an array of ten int values.
  • It declares array1 to be a reference to an array of int values.
  • All of these are true.
14
What would the output be?

int total = 0;
int [] nums = {40, -9, 100, 1, -15, -90, -78};

for (int x : nums)
if (nums[i] > 0)
total = total + nums[i];

System.out.println(total)
  • This will cause an error
  • 141
  • -192
  • 3
15
What is this method doing?

public static int doThings (int nums[ ], int x)
{
for (int i = 0; i < nums.length; i++)

if (nums[i] == x)

return i;
return -1;
}
  • finding the index of the last positive number in the array
  • sorting the array.
  • searching for the value of x in the array.
  • counting the number of array elements divisible by x.
16
The for-each loop is designed to let you look at both the values in an array and their indexes.
  • True
  • False
17
Given an array named arr, which statement(s) would print all the values of the array?
  • System.out.println(Arrays.toString(arr));

  • System.out.println( toString(arr));
  • System.out.println( arr.toString());
  • System.out.println( arr);
18
Given the following code, what would be the correct replacement for --Line 1-- to call the doThings method?

public static void main(String[] args)
{
int [] nums = {40, -9, 100, 1, -15, -90, -78, 99};
--- Line 1 -- // statement to call doThings method
}

public static int doThings (int nums[ ])
{
// body of method
}
  • int x = doThings(int nums[ ]);
  • int x = doThings(int nums);
  • int x = doThings(nums);

  • int x = doThings(nums[ ]);
Study all 18 cards for free