AP Computer Science A Unit 7 Progress Check: MCQE Exam
Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing. Assume that the Thing class has been properly defined and includes a no-parameter constructor.
ArrayList<Thing> a = /* missing code */;
Which of the following can be used to replace /* missing code */ so that the statement works as intended? - Answer -B. new ArrayList<Thing>()
Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values.
ArrayList<Integer> numbers = /* missing code */;
Which of the following can be used to replace /* missing code */ so that the statement works as intended?
new ArrayList()
new ArrayList<Integer>
new ArrayList<Integer>() - Answer -C. I and III only
Consider the following statement, which is intended to create an ArrayList named arrList
to store elements only of type String.
/* missing code */ = new ArrayList<String>();
Which of the following can be used to replace /* missing code */ so that the statement works as intended? - Answer -E. ArrayList<String> arrList
Consider the following code segment.
ArrayList<Integer> nums = new ArrayList<>();
nums.add(3);
nums.add(2);
nums.add(1);
nums.add(0);
nums.add(0, 4);
nums.set(3, 2);
nums.remove(3);
nums.add(2, 0);
Which of the following represents the contents of nums after the code segment has been executed? - Answer -D. [4, 3, 0, 2, 0] Consider the following code segment.
ArrayList<String> syllables = new ArrayList<String>();
syllables.add("LA");
syllables.add(0, "DI");
syllables.set(1, "TU");
syllables.add("DA");
syllables.add(2, syllables.get(0));
syllables.remove(1);
System.out.println(syllables.toString());
What is printed as a result of executing the code segment? - Answer -B. [DI, DI, DA]
Consider the following code segment.
ArrayList<Integer> vals = new ArrayList<Integer>();
vals.add(vals.size(), vals.size());
vals.add(vals.size() - 1, vals.size() + 1);
vals.add(vals.size() - 2, vals.size() + 2);
System.out.println(vals.toString());
What is printed as a result of executing the code segment? - Answer -E. [4, 2, 0]
Consider the following code segment.
ArrayList<Integer> myList = new ArrayList();
for (int i = 0; i < 4; i++)
{
myList.add(i + 1);
}
for (int i = 0; i < 4; i++)
{
if (i % 2 == 0)
{
System.out.print(myList.get(i) + " ");
}
}
What output is produced as a result of executing the code segment? - Answer -D. 1 3
Consider the following code segment.
ArrayList<String> words = new ArrayList<String>();
words.add("mat");
words.add("new");
words.add("open");
words.add("pet");
Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing. Assume that the Thing class has been properly defined and includes a no-parameter constructor.
ArrayList<Thing> a = /* missing code */;
Which of the following can be used to replace /* missing code */ so that the statement works as intended? - Answer -B. new ArrayList<Thing>()
Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values.
ArrayList<Integer> numbers = /* missing code */;
Which of the following can be used to replace /* missing code */ so that the statement works as intended?
new ArrayList()
new ArrayList<Integer>
new ArrayList<Integer>() - Answer -C. I and III only
Consider the following statement, which is intended to create an ArrayList named arrList
to store elements only of type String.
/* missing code */ = new ArrayList<String>();
Which of the following can be used to replace /* missing code */ so that the statement works as intended? - Answer -E. ArrayList<String> arrList
Consider the following code segment.
ArrayList<Integer> nums = new ArrayList<>();
nums.add(3);
nums.add(2);
nums.add(1);
nums.add(0);
nums.add(0, 4);
nums.set(3, 2);
nums.remove(3);
nums.add(2, 0);
Which of the following represents the contents of nums after the code segment has been executed? - Answer -D. [4, 3, 0, 2, 0] Consider the following code segment.
ArrayList<String> syllables = new ArrayList<String>();
syllables.add("LA");
syllables.add(0, "DI");
syllables.set(1, "TU");
syllables.add("DA");
syllables.add(2, syllables.get(0));
syllables.remove(1);
System.out.println(syllables.toString());
What is printed as a result of executing the code segment? - Answer -B. [DI, DI, DA]
Consider the following code segment.
ArrayList<Integer> vals = new ArrayList<Integer>();
vals.add(vals.size(), vals.size());
vals.add(vals.size() - 1, vals.size() + 1);
vals.add(vals.size() - 2, vals.size() + 2);
System.out.println(vals.toString());
What is printed as a result of executing the code segment? - Answer -E. [4, 2, 0]
Consider the following code segment.
ArrayList<Integer> myList = new ArrayList();
for (int i = 0; i < 4; i++)
{
myList.add(i + 1);
}
for (int i = 0; i < 4; i++)
{
if (i % 2 == 0)
{
System.out.print(myList.get(i) + " ");
}
}
What output is produced as a result of executing the code segment? - Answer -D. 1 3
Consider the following code segment.
ArrayList<String> words = new ArrayList<String>();
words.add("mat");
words.add("new");
words.add("open");
words.add("pet");