Question: Answer the Following Question correctly. 1.Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of
Answer the Following Question correctly.
1.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
2.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];
3.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++;
}
}
4.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
