1.The following statement gets an element from position 4 in an array named myArray:

x = myArray[4];
What is the equivalent operation using an array list named list.?

A x = list.get();
B x = list.get(4);
C x = list.get[4];
D x = list[4];
2.Consider the following code snippet:

ArrayList num1 = new ArrayList();
int data;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 5; i++)
{
data = in.nextInt();
num1.add(data);
if (data == 0 && num1.size() > 3)
{
num1.remove(num1.size() - 1);
}
}
System.out.println("size is : " + num1.size());
What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?

A size is : 1
B size is : 0
C size is : 2
D size is : 4
3.What is the output of the following code snippet?

public static int check(ArrayList listData)
{
int sum = 0;
for (int i = 0; i < listData.size(); i++)
{
sum = sum + listData.get(i);
}
return sum;
}
public static void main(String[] args)
{
ArrayList vdata = new ArrayList();
int rsum;
for (int cnt = 0; cnt < 3; cnt++)
{
vdata.add(cnt + 1);
}
rsum = check(vdata);
System.out.println(rsum);
}
A 2
B 4
C 3
D 6
4.What is the output of the following code snippet?

ArrayList num;
num.add(4);
System.out.println(num.size());
A 4
B Error because num is not initialized
C 0
D 1
5.Consider the following code snippet:

ArrayList arrList = new ArrayList();
for (int i = 0; i < arrList.size(); i++)
{
arrList.add(i + 3);
}
What value is stored in the element of the array list at index 0? Think carefully about this.

A 0
B 3
C 6
D None
6. Which statement is true about the code snippet below?

ArrayList names = new ArrayList();
names.add("John");
names.add("Jerry");
ArrayList friends = names;
friends.add("Harry");
A The final size of names is 2; the final size offriends is 3.
B The final size of names is 3; the final size offriends is 2.
C compilation error
D The final size of names is 3; the final size of friends is 3.
7. Which statement is true about the code snippet below?

ArrayList names = new ArrayList();
names.add("John");
names.add("Jerry");
ArrayList friends = new ArrayList(names);
friends.add("Harry");
A The final size of names is 3; the final size offriends is 3.
B The final size of names is 3; the final size offriends is 2.
C The final size of names is 2; the final size of friends is 3.
D compilation error
8. Which statement is true about the code snippet below?

import java.util.*;
public class Test
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList inputs = new ArrayList();
int ind = 0;
System.out.print("Enter some numbers: ");
while (in.hasNextDouble())
{
inputs.set(ind, in.nextDouble());
ind++;
}

System.out.println(ind);
}
}
A The array list is full after 100 numbers are entered.
B The code adds all input numbers to the array list.
C The code has a run-time error.
D The code has compile-time error.
9. Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList?

A:

int max = aList[0];
for (int count = 1; count < aList.size();count++)
{
if (aList.get(count) > max)
{
max = aList.get(count);
}
}
B:
int max = 0;
for (int count = 1; count < aList.size(); count++)
{
if (aList.get(count) > max)
{
max = aList.get(count);
}
}
C:
int max = aList.get(0);
for (int count = 1; count < aList.size(); count++)
{
if (aList.get(count) > max)
{
max = aList.get(count);
}
}
D:
if (aList.size() > 0)
{
int max = aList.get(0);
for (int count = 1; count < aList.size();count++)
{
if (aList.get(count) > max)
{
max = aList.get(count);
}
}
}
10. What will be printed by the statements below?

ArrayList names = new ArrayList();
names.add("Annie");
names.add("Bob");
names.add("Charles");
names.set(2, "Doug");
names.add(0, "Evelyn");
System.out.print (names);

A [Annie, Bob, Charles, Doug, Evelyn]
B [Evelyn, Annie, Doug, Charles]
C [Evelyn, Doug, Charles]
D [Evelyn, Annie, Bob, Doug]

Q&A Education