1-Minute CollegeBoard Lesson
Explaining question 39 on the 2015 CollegeBoard multiple-choice in one minute, along with a popcorn hack.
Question 39 (5-Minute Lesson):
Problem:
Consider the following code segment:
List<String> students = new ArrayList<String>();
students.add("Alex");
students.add("Bob");
students.add("Carl");
for (int k = 0; k < students.size(); k++) {
System.out.println(students.set(k, "Alex") + " ");
}
System.out.println();
for (String str : students) {
System.out.print(str + " ");
}
I got this one correct. It is a matter of ArrayLists, understanding their methods, and visualizing how loops work. At this point in the test, I was able to do so pretty well. Let’s now go over each of the wrong choices and explain why they were wrong:
A: This would be the case if the set method returned the value that was stored in the element after it was assigned “Alex” instead of returning the value being replaced “Alex”.
B: In this case the lines of output are reversed.
D: This would the result if the first for loop used the get method instead of the set method.
E: The set method can be used in the System.out.print() method, because it returns the value that was at this index before it was updated to “Alex”.
Finally, choice C was correct. This is because the first for loop uses the set method to change the value of each element in students to “Alex”. When the set method is called, it returns the value that was originally at this index. So, the first loop will print Alex Bob Carl. At this point all elements have been set to “Alex”. The second for loop uses an enhanced for loop to access every element and will print Alex Alex Alex.
If you got this wrong, I would suggest going back to ArrayList methods and how to traverse an ArrayList through different methods and algorithms. Since this was the student lesson I taught, I didn’t struggle with these questions, and I also know how to help those who might be struggling.
Popcorn Hack:
Master ArrayList manipulation and loop comprehension by dissecting the code snippet, understanding the nuances of the set
method, and creating a similar exercise that involves updating ArrayList elements for a comprehensive grasp of core concepts.