Please answert he following question correctly. 1.Suppose you wish to process an
ID: 3607681 • Letter: P
Question
Please answert he following question correctly.
1.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
2.Which code snippet finds the largest value in an array that is only partially full?
a)
b)
c)
d)
3.Which code snippet calculates the sum of all the elements in even positions in an array?
a)
b)
c)
d)
4.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.
5. 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
6.The following statement gets an element from position 4 in an array:
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];
7.Which code snippet calculates the sum of all the even elements in an array values?
a)
b)
c)
d)
8.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)
b)
c)
d)
Explanation / Answer
1) b) remove an element
We are concentrating on eliminating duplicates. So, we need to remove.
2) d)
double largest = values[0];
for (int i = 1; i < currSize; i++)
{
if (values[i] < largest)
{
largest = values[i];
}
}
Since the array is partially full, we need to traverse till the current size only.
3) d)
int sum = 0;
for (int i = 0; i < values.length; i = i + 2)
{
sum += values[i];
}
i should start with 0 since it is also an even number and each time need to increment it by 2 where the work is done by i = i + 2
4) b) The program will not compile.
Will encounter following error
symbol: method size()
location: variable a of type String
1 error