Quiz about Java ArrayList

Java ArrayList

Last Updated :
Discuss
Comments

Question 1

What is the default initial capacity of an ArrayList when it is created using the no-arg constructor?


  • A

    5

  • B

    10

  • C

    20

  • D

    1

Question 2

What will be the output of the following code?

Java
import java.util.*;
public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.remove(1);
        System.out.println(list);
    }
}


  • A

    [1, 3]


  • B

    [2, 3]

  • C

    [1, 2]

  • D

    [1, 2, 3]

Question 3

Which method of ArrayList is used to add an element at a specific index?


  • A

    addAtIndex()

  • B

    insert()

  • C

    add()

  • D

    add(index, element)

Question 4

What will be the result of the following code?

Java
import java.util.*;
public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.set(2, 50);
        System.out.println(list);
    }
}


  • A

    [10, 20, 50, 40]


  • B

    [10, 20, 30, 40]


  • C

    [10, 20, 40, 50]

  • D

    [50, 20, 30, 40]


Question 5

Which of the following methods is used to check if an ArrayList is empty?

  • A

    isEmptyList()

  • B

    isNull()

  • C

    isEmpty()


  • D

    empty()

Question 6

Which of the following methods returns the number of elements in an ArrayList?


  • A

    size()

  • B

    length()

  • C

    count()


  • D

    getSize()

Question 7

What is the time complexity of the add() method in an ArrayList (amortized case)?

  • A

    O(1)


  • B

    O(n)


  • C

    O(log n)

  • D

    O(n^2)

There are 7 questions to complete.

Take a part in the ongoing discussion