Code Tracing
Removing
Method Mysteries
Loops + Logic
Integers & References
200

Given list = ["A", "B", "C"].

After executing the following code:
   list.add(list.remove(0));

What does the list look like?

["B", "C", "A"]

200

Given list = [1, 2, 2, 3].

What does the list look like after this loop executes?
 
for (int i = 0; i < list.size(); i++) {
   if (list.get(i) == 2)
     list.remove(i);
}

[1, 2, 3]

200

What exception occurs if you call list.set(list.size(), "Value")?

IndexOutOfBounds

200

Why does for (int i = 0; i <= list.size(); i++) crash?

IndexOutOfBoundsException

200

ArrayList<Integer> nums = [1, 2, 3].

What is in the list after nums.remove(1)?

[1, 3]

400

Given list = [1, 2, 3, 4].
Trace the result of this loop:
for (int k = 0; k < list.size(); k++) {
     list.set(k, list.get(k) + k);

[1, 3, 5, 7]

400

To correctly remove ALL occurrences of a value using a standard for loop, what specific change must you make to the (aka the l?

Iterate backwards (i--) OR decrement i inside the if-statement (i--)

400

What is the boolean return value of list.add(*object name and value*) for an ArrayList?

true

400

list = [1, 2, 3, 4].

int count = 0.
for (int x : list) {
   if even, add x to count;
   if odd, subtract x from count.

}

What is the value of count after the code above executes?

2

400

How do you remove the VALUE 1 from ArrayList<Integer> nums?

nums.remove(Integer.valueOf(1)) OR you can cast to an object

600

Given an empty list letters.
What does the list look like after the following lines of code are executed?
 
   letters.add("A");
   letters.add(0, "B");
   letters.set(1, "C");
   letters.add(1, "D");

["B", "D", "C"]

600

Given list = [1, 2, 2, 3].
 
What remains after this loop is executed?
 
for (int i = 0; i < list.size(); i++) {
    if (list.get(i) == 2)
       list.remove(i);
}

[1, 2, 3]

600

True or False: list.contains("apple") returns true if the list contains "Apple"

False!! Java is case-sensitive

600

What happens if you add to an ArrayList inside a loop that iterates up to list.size()?

Infinite Loop. The list size will keep increasing, meaning that whichever index you are at (let's say i for example) will always be less than list.size(), so the loop will continue to run forever.

600

List a = new ArrayList();
a.add(5);
List b = a;
b.add(6);
b.add(7);

What is a.size()?

3.

Remember, objects are handled by reference in Java. "List b = a;" means that the newly created variable b points to the same ArrayList instance in memory as a.

800

Trace the output:
ArrayList<Integer> nums = new ArrayList<>();         nums.add(3);
   nums.add(4);
   nums.add(5);
   nums.add(1, nums.get(2) + nums.get(0));

[3, 8, 4, 5]

800

Given list = [10, 20, 30].

What is the output of the following line of code?

System.out.println(list.remove(1) + list.get(1));

50

800

If list is empty, what does
list.isEmpty() == (list.size() == 0)
evaluate to? 

true

800

Why is the following code invalid for an ArrayList<Blue>?  (Blue is a random class that you created)

if (list.get(i) > list.get(i+1))  

You cannot use > and < directly to compare elements that are Objects

800

True/False: ArrayList<int> list = new ArrayList<int>(); compiles.

FALSE! You must type it as <Integer>

1000

What is printed by this code?
  List<String> list = new ArrayList<>();              list.add("Cat");
  System.out.println(list.set(0, "Dog"));

Cat

1000

Write the single line of code to remove the LAST element of any ArrayList named list.

list.remove(list.size() - 1);

1000

If the goal is to add at the end of the list, the code

 list.add(5, "Hello");

 is ONLY valid if the list size is initially what number?

5

1000

What is this code effectively doing?

for (int i = 0; i < list.size() / 2; i++)
   swap(i, list.size() - 1 - i); // swaps the element at index i with the element at index (size -1 - i)

Reversing the list

1000

Does the following code return true if both are distinct String objects with value "Hello"? Explain why or why not.

list.get(0) == list.get(1)

No, you should use .equals() with Strings