Here are 10 essential multiple-choice questions on Java ArrayList, covering key concepts.
Question 1
What is the default initial capacity of an ArrayList when it is created using the no-arg constructor?
5
10
20
1
Question 2
What will be the output of the following code?
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); } }
[1, 3]
[2, 3]
[1, 2]
[1, 2, 3]
Question 3
Which method of ArrayList is used to add an element at a specific index?
addAtIndex()
insert()
add()
add(index, element)
Question 4
What will be the result of the following code?
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); } }
[10, 20, 50, 40]
[10, 20, 30, 40]
[10, 20, 40, 50]
[50, 20, 30, 40]
Question 5
Which of the following methods is used to check if an ArrayList is empty?
isEmptyList()
isNull()
isEmpty()
empty()
Question 6
What will be the output of the following code?
import java.util.*; public class Test { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); list.add("Date"); list.remove("Banana"); System.out.println(list); } }
[Apple, Cherry, Date]
[Apple, Banana, Cherry, Date]
[Banana, Apple, Cherry, Date]
[Apple, Cherry, Date, Banana]
Question 7
Which of the following methods returns the number of elements in an ArrayList?
size()
length()
count()
getSize()
Question 8
What will happen if you try to access an element with an index greater than the size of the ArrayList?
It throws an ArrayIndexOutOfBoundsException
It throws a NoSuchElementException
It returns null
It throws an IndexOutOfBoundsException
Question 9
What is the time complexity of the add() method in an ArrayList (amortized case)?
O(1)
O(n)
O(log n)
O(n^2)
Question 10
What is the maximum capacity of an ArrayList?
1000 elements
The maximum value of an integer
2^31 - 1 elements
There is no maximum capacity
There are 10 questions to complete.