Which of the following is the correct syntax to get the last element from an ArrayList named nums

9.18. Easy Multiple Choice Questions¶

These problems are easier than most of those that you will usually see on the AP CS A exam.

    8-11-1: Which index is the last element in a list called nums at?

  • nums.length
  • You can't use length on lists and the last index is one less than the size.
  • nums.length - 1
  • You can't use length on lists, use size instead.
  • nums.size()
  • Since the first element in a list is at index 0 the last element is at the size minus 1.
  • nums.size() - 1
  • The last element is at the size of the list minus 1.

    8-11-2: Which of the following is a reason to use an array instead of an ArrayList?

  • An array has faster access to its elements than a list does.
  • Since an ArrayList is implemented by an array, it has the same access time.
  • An array knows it length, but a list doesn't know its length.
  • Lists do know their length, but they don't make it public.
  • An ArrayList can allocate more space than it needs.
  • Every time an ArrayList fills up a new array is created that is twice as big. This can lead to extra space that is wasted.

    8-11-3: Which of the following is a reason to use an ArrayList instead of an array?

  • An ArrayList can grow or shrink as needed, while an array is always the same size.
  • This is the main advantage to an ArrayList.
  • You can use a for-each loop on an ArrayList, but not in an array.
  • You can use a for-each loop on either an ArrayList or array.
  • You can store objects in an ArrayList, but not in an array.
  • Arrays can also store objects of the same type.

    8-11-4: Which of the following is the correct way to get the first value in a list called nums?

  • nums[0]
  • This is how you get the first value in an array, but not in a list.
  • nums[1]
  • This is how you get the second value in an array. Remember that this is a list and that the first item in an array is at index 0.
  • nums.first()
  • The List interface doesn't have a first method.
  • nums.get(0)
  • Use the get method to get a value from a list and the first element in a list is at index 0.
  • nums.get(1)
  • This would return the second element in a list. Remember that the first element in a list or array is at index 0.

    8-11-5: Which of the following is the correct way to set the second value in a list called nums to 5?

  • nums[1] = 5;
  • This is how you set the second value in an array, but not in a list.
  • nums[2] = 5;
  • This is how you set the third value in an array, but not in a list.
  • nums.set(5, 1);
  • This would the value at index 5 to 1.
  • nums.set(1, 5);
  • This sets the second value in the list to 5.
  • nums.set(2, 5);
  • This would set the third value in the list to 5. Remember that the first value is at index 0.

    8-11-6: Which of the following is the correct way to remove the value 3 from the list nums = [5, 3, 2, 1]?

  • nums.remove(3);
  • This would remove the value at index 3 which is 1.
  • nums.remove(0);
  • This would remove the value at index 0 which is 5.
  • nums.remove(1);
  • This would remove the value at index 1 which is 3.
  • nums.remove(2);
  • This would remove the value at index 2 which is 2.

    8-11-7: Which of the following is the correct way to add 2 between the 1 and 3 in the following list nums = [1, 3, 4]?

  • nums.add(2, 0);
  • This would add 0 at index 2. Remember that the method is add(index, obj).
  • nums.add(2, 1);
  • This would add 1 at index 2. Remember that the method is add(index, obj)
  • nums.add(0, 2);
  • This would add 2 at index 0 which would result in [2, 1, 3, 4]
  • nums.add(1, 2);
  • This would add 2 at index 1 which would result in [1, 2, 3, 4]
  • nums.add(2, 2);
  • This would add 2 at index 2 which would result in [1, 3, 2, 4]

    8-11-8: Which of the following is false about an interface?

  • It is a type of class.
  • An interface is a special type of abstract class in Java.
  • The methods in an interface will be public and abstract.
  • The methods defined in an interface are public and abstract.
  • It is like a contract in that the class that implements the interface must provide the methods defined in the interface.
  • An interface is like a contract for the implementing classes.
  • You can create an object of an interface type.
  • You can not create an object of an interface type. This is why you create a ``List`` using the ArrayList class which implements the ``List`` interface.

    8-11-9: What will print when the following code executes?

    List<Integer> list1 = new ArrayList<Integer>(); list1.add(new Integer(1)); list1.add(new Integer(2)); list1.add(new Integer(3)); list1.remove(1); System.out.println(list1);
  • [2, 3]
  • This would be true if it was remove(0)
  • [1, 2, 3]
  • The remove will remove a value from the list, so this can't be correct.
  • [1, 2]
  • This would be true if it was remove(2)
  • [1, 3]
  • This removes the value at index 1 which is 2.

You can step through the code above by clicking on the following Ex-8-11-9.

    8-11-10: What will print when the following code executes?

    List<String> list1 = new ArrayList<String>(); list1.add("Anaya"); list1.add("Layla"); list1.add("Sharrie"); list1.set(0, "Destini"); list1.add(0, "Sarah"); System.out.println(list1);
  • ["Sarah", "Destini", "Layla", "Sharrie"]
  • The list is first ["Anaya", "Layla", "Sharrie"] and then ["Destini, "Layla", "Sharrie"] and finally ["Sarah", "Destini, "Layla", "Sharrie"]
  • ["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]
  • The set replaces the value at index 0.
  • ["Sarah", "Layla", "Sharrie"]
  • This would be true if the second add was a set.
  • ["Destini", "Layla", "Sharrie", "Sarah"]
  • This would be true if the last add didn't have an index of 0.

You can step through the code above by clicking on the following Ex-8-11-10.

Get first and last elements from ArrayList in Java

Given an array list, find the first and last elements of it.

Examples:

Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60

Why ArrayList is better than Array?

The limitation with array is that it has a fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink.

On the other ArrayList can dynamically grow and shrink after addition and removal of elements (See the images below). Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy. Let’s see the diagrams to understand the addition and removal of elements from ArrayList and then we will see the programs.

Adding Element in ArrayList at specified position:

Removing Element from ArrayList:

There is a list of several tutorials on ArrayList at the end of this guide, refer it to understand and learn ArrayList concept fully.

Java ArrayList

last modified November 12, 2021

Java ArrayList tutorial shows how to work with ArrayList collection in Java. Located in the java.util package, ArrayList is an important collection of the Java collections framework.

Java collections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details. A collection is an object that represents a group of objects.

Video liên quan

Postingan terbaru

LIHAT SEMUA