Question: Please answer with the correct answer. 1.The binary search is faster than the linear search, providing a) the size of the array is a power
Please answer with the correct answer.
1.The binary search is faster than the linear search, providing
a) the size of the array is a power of two.
b) the elements of the array are ordered.
c) the elements of the array are unordered.
d) the element being searched for is actually in the array.
2.It may be necessary to "grow" an array when reading inputs because
a) the number of inputs may not be known in advance.
b) arrays in Java must be resized after every 100 elements.
c) arrays are based on powers of two.
d) the only alternative is a bounds exception.
3.Which statements about array algorithms are true?
I. The array algorithms are building blocks for many programs that process arrays
II. Java contains ready-made array algorithms for every problem situation
III. It is inefficient to make multiple passes through an array if you can do everything in one pass
a) I, II
b) I, III
c) II, III
d) I, II, III
4.Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution?
a) structure a program using methods
b) adapt a built-in array algorithm
c) decompose the problem into steps
d) assemble a test program and run it
5.Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithms might be adapted for this?
a) find the minimum
b) remove an element
c) add an element
d) calculate the sum of the elements
6.Which code snippet finds the largest value in an array that is only partially full?
a)
double largest = values[0];
for (int i = 1; i < values.length; i++)
{ if (values[i] > largest)
{ largest = values[i];
}
}
b)
double largest = values[0];
for (int i = 1; i < values.length; i++)
{ if (values[i] < largest)
{ largest = values[i];
}
}
c)
double largest = values[0];
for (int i = 1; i < currSize; i++)
{ if (values[i] > largest)
{ largest = values[i];
}
}
d)
double largest = values[0];
for (int i = 1; i < currSize; i++)
{ if (values[i] < largest)
{ largest = values[i];
}
}
7.Which code snippet calculates the sum of all the elements in even positions in an array?
a)
int sum = 0;
for (int i = 1; i < values.length; i+=2)
{ sum++;
}
b)
int sum = 0;
for (int i = 0; i < values.length; i++)
{ sum++;
}
c)
int sum = 0;
for (int i = 0; i < values.length; i++)
{ sum += values[i];
}
d)
int sum = 0;
for (int i = 0; i < values.length; i = i + 2)
{ sum += values[i];
}
8.If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen?
a) The program will crash.
b) The program will not compile.
c) The program will run but will produce an uncertain result.
d) The compiler will automatically correct the error.
9. Which statements are true regarding the differences between arrays and array lists?
I. Arrays are better if the size of a collection will not change
II. Array lists are more efficient than arrays
III. Array lists are easier to use than arrays
a) I, II
b) I, III
c) II, III
d) I, II, III
10.The following statement gets an element from position 4 in an array:
x = a[4];
What is the equivalent operation using an array list?
a) x = a.get(4);
b) x = a.get();
c) x = a.get[4];
d) x = a[4];
11.Which code snippet calculates the sum of all the even elements in an array values?
a)
int sum = 0;
for (int i = 0; i < values.length; i++)
{ if ((values[i] % 2) == 0)
{ sum += values[i];
}
}
b)
int sum = 0;
for (int i = 0; i < values.length; i++)
{ if ((values[i] % 2) == 0)
{ sum++;
}
}
c)
int sum = 0;
for (int i = 0; i < values.length; i++)
{ if ((values[i] / 2) == 0)
{ sum += values[i];
}
}
d)
int sum = 0;
for (int i = 0; i < values.length; i++)
{ if ((values[i] / 2) == 0)
{ sum++;
}
}
12.Which one of the following code snippets accepts the integer input in an array list named num1 and stores the odd integers of num1 in another array list named oddnum?
a)
ArrayListnum1 = new ArrayList ();
ArrayListoddnum = new ArrayList ();
int data;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{ data = in.nextInt();
num1.add(data);
if (num1.get(i) % 2 == 0)
{ oddnum.add(num1.get(i));
}
}
b)
ArrayListnum1 = new ArrayList ();
ArrayListoddnum = new ArrayList ();
int data;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{ data = in.nextInt();
num1.add(data);
if (num1.get(i) % 2 != 0)
{ oddnum.add(num1.get(i));
}
}
c)
ArrayListnum1 = new ArrayList ();
ArrayListoddnum = new ArrayList ();
int data;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{ data = in.nextInt();
num1.add(data);
if (num1.get(i) % 2 == 0)
{ oddnum.add(num1[i]);
}
}
d)
ArrayListnum1;
ArrayListoddnum = new ArrayList ();
int data;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{ data = in.nextInt();
num1.add(data);
if (num1[i] % 2 != 0)
{ oddnum.add(num1[i]);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
