Java ArrayList MCQ - Multiple Choice Questions and Answers

1. What is an ArrayList in Java?

a) A dynamic array
b) A static array
c) A linked list
d) A hash table

Answer:

a) A dynamic array

Explanation:

ArrayList in Java is a resizable array implementation of the List interface.

2. How do you instantiate an ArrayList in Java?

a) List myList = new ArrayList();
b) Array myList = new ArrayList();
c) ArrayList myList = new ArrayList();
d) Both a and c

Answer:

d) Both a and c

Explanation:

An ArrayList can be instantiated using either the ArrayList type or the List interface.

3. Which of these is a valid way to add elements to an ArrayList?

a) add()
b) put()
c) insert()
d) append()

Answer:

a) add()

Explanation:

The add() method is used to add elements to an ArrayList.

4. How do you remove an element from an ArrayList?

a) remove()
b) delete()
c) clear()
d) discard()

Answer:

a) remove()

Explanation:

The remove() method is used to remove an element from an ArrayList.

5. How do you find the size of an ArrayList?

a) size()
b) length()
c) count()
d) getSize()

Answer:

a) size()

Explanation:

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

6. Can an ArrayList contain multiple references to the same object?

a) Yes
b) No
c) Only if the object is immutable
d) Only if the object is a String

Answer:

a) Yes

Explanation:

An ArrayList can contain multiple references to the same object.

7. What happens when you add elements to an ArrayList beyond its current capacity?

a) The add operation fails
b) The ArrayList is resized automatically
c) It throws an exception
d) The elements are not added

Answer:

b) The ArrayList is resized automatically

Explanation:

When elements are added beyond the ArrayList's capacity, it automatically resizes itself to accommodate the new elements.

8. How do you access an element in an ArrayList?

a) get(index)
b) retrieve(index)
c) fetch(index)
d) access(index)

Answer:

a) get(index)

Explanation:

The get(index) method is used to access an element from an ArrayList at the specified index.

9. Can an ArrayList contain different types of elements?

a) Yes, but only if declared with a wildcard
b) No, it can contain only one type
c) Yes, it can contain any type of object
d) Only primitive types

Answer:

c) Yes, it can contain any type of object

Explanation:

ArrayList can contain different types of objects since it stores its elements as Object references.

10. How do you update an element in an ArrayList?

a) update(index, element)
b) set(index, element)
c) change(index, element)
d) modify(index, element)

Answer:

b) set(index, element)

Explanation:

The set(index, element) method is used to update an element at the specified index in an ArrayList.

11. What is the return type of the remove() method in ArrayList?

a) void
b) boolean
c) the type of the element removed
d) int

Answer:

c) the type of the element removed

Explanation:

The remove() method returns the element that was removed from the list.

12. What does the clear() method do in an ArrayList?

a) Removes all elements
b) Clears the first element
c) Resets all elements to null
d) Shrinks the ArrayList size to zero

Answer:

a) Removes all elements

Explanation:

The clear() method removes all of the elements from the ArrayList, leaving it empty.

13. How can you iterate over the elements of an ArrayList?

a) Using a while loop
b) Using a for loop
c) Using an iterator or enhanced for loop
d) Both b and c

Answer:

d) Both b and c

Explanation:

Elements of an ArrayList can be iterated using a for loop, an enhanced for loop, or an Iterator.

14. What happens if you try to access an index that is out of bounds in an ArrayList?

a) Returns null
b) Adds a new element
c) Throws an IndexOutOfBoundsException
d) Automatically resizes the ArrayList

Answer:

c) Throws an IndexOutOfBoundsException

Explanation:

Accessing an out-of-bounds index in an ArrayList results in an IndexOutOfBoundsException.

15. Is ArrayList synchronized in Java?

a) Yes
b) No
c) Only when explicitly synchronized
d) Synchronization is not applicable

Answer:

b) No

Explanation:

ArrayList is not synchronized. If multiple threads access it concurrently and at least one of the threads modifies it structurally, it must be synchronized externally.


Comments