An Introduction to ArrayList in Java

Multiple choice question based on Java program on arraylist used in java along with its answer and explanation in detail

PROGRAMMING

3/25/20242 min read

MacBook Pro with images of computer language codes
MacBook Pro with images of computer language codes

Introduction to ArrayList in Java

An ArrayList is a part of the Java Collection Framework and is used to store a dynamic collection of elements. It is similar to an array, but with the advantage of being resizable. In Java, ArrayLists are implemented using the ArrayList class.

Advantages of ArrayList

ArrayLists have several advantages over arrays:

  • Dynamic size: Unlike arrays, ArrayLists can grow or shrink dynamically as elements are added or removed.
  • Easy insertion and deletion: Elements can be easily inserted or deleted at any position in an ArrayList.
  • Flexibility: ArrayLists can store elements of any type, including objects.

Java Program using ArrayList

Let's take a look at a simple Java program that demonstrates the usage of ArrayList:

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList
        ArrayList fruits = new ArrayList<>();

        // Add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Get the size of the ArrayList
        int size = fruits.size();
        System.out.println("Size of the ArrayList: " + size);

        // Access elements in the ArrayList
        System.out.println("First fruit: " + fruits.get(0));
        System.out.println("Last fruit: " + fruits.get(size - 1));

        // Remove an element from the ArrayList
        fruits.remove(1);
        System.out.println("Updated ArrayList: " + fruits);
    }
}

In this program, we create an ArrayList called "fruits" to store strings. We add three fruits to the ArrayList using the add() method. We then retrieve the size of the ArrayList using the size() method and print it. Next, we access elements in the ArrayList using the get() method and print the first and last fruit. Finally, we remove an element from the ArrayList using the remove() method and print the updated ArrayList.

Multiple Choice Questions

  1. What is the output of the following code?

        ArrayList numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        System.out.println(numbers.size());
        
    1. 0
    2. 1
    3. 2
    4. 3

    Answer: d) 3

    Explanation: The size() method returns the number of elements in the ArrayList, which in this case is 3.

  2. What is the output of the following code?

        ArrayList colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Blue");
        colors.add("Green");
        System.out.println(colors.get(1));
        
    1. Red
    2. Blue
    3. Green

    Answer: b) Blue

    Explanation: The get(index) method is used to retrieve the element at the specified index, starting from 0. In this case, the element at index 1 is "Blue".

  3. What is the output of the following code?

        ArrayList numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.remove(1);
        System.out.println(numbers);
        
    1. [10]
    2. [20, 30]
    3. [10, 30]
    4. [10, 20, 30]

    Answer: c) [10, 30]

    Explanation: The remove(index) method is used to remove the element at the specified index. In this case, the element at index 1 (20) is removed, resulting in the ArrayList [10, 30].

  4. Which method is used to add an element to an ArrayList?

    1. add()
    2. insert()
    3. push()
    4. append()

    Answer: a) add()

    Explanation: The add() method is used to add an element to the end of an ArrayList.

  5. Which method is used to retrieve the size of an ArrayList?

    1. size()
    2. length()
    3. count()
    4. getLength()

    Answer: a) size()

    Explanation: The size() method returns the number of elements in an ArrayList.

Conclusion

ArrayLists are a powerful data structure in Java that allow for dynamic storage of elements. They provide flexibility and ease of use compared to arrays. By understanding the basic operations and methods of ArrayLists, you can effectively utilize them in your Java programs.