Search Tutorials


Java ArrayList MCQ Questions and Answers | JavaInUse

Java ArrayList MCQ Questions and Answers

Q. What is the correct way to import the ArrayList class in Java?

A. import java.util.ArrayList;
B. import java.util.List;
C. import java.collection.ArrayList;
D. import java.array.ArrayList;

Q. How do you create an ArrayList in Java?

A. ArrayList list = new ArrayList();
B. ArrayList<Integer> list = new ArrayList<>();
C. ArrayList<Object> list = ArrayList<>();
D. All of the above

Q. How do you add elements to an ArrayList?

A. list.add(element)
B. list.append(element)
C. list[size] = element
D. list.insert(element)

Q. How do you get the size of an ArrayList?

A. list.length()
B. list.size()
C. list.count()
D. list.getSize()

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

A. list.remove(element)
B. list.delete(element)
C. list.removeAt(index)
D. Both A and C

Q. How do you iterate through an ArrayList?

A. for (int i = 0; i < list.size(); i++) {
B. for (Object obj : list) {
C. for (int i = 0; i <= list.size(); i++) {
D. for (int i = 1; i < list.length(); i++) {

Q. What is the output of list.contains(null) if an ArrayList contains a null value?

A. true
B. false
C. It depends on the elements in the list
D. It will throw an error

Q. How do you clear all elements from an ArrayList?

A. list.clear()
B. list.remove()
C. list.deleteAll()
D. list = null

Q. How do you convert an ArrayList to an array?

A. list.toArray()
B. list.toArray(new Object[list.size()])
C. list.toArray(new Integer[list.size()])
D. Both B and C

Q. How do you sort an ArrayList of integers in ascending order?

A. Collections.sort(list)
B. list.sort()
C. list.sort(Collections.reverseOrder())
D. list.sort(Comparator.naturalOrder())





Q. What will be the output of the following code snippet?

ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");

for (String fruit : fruits) {
    System.out.print(fruit + " ");
}
A.
Apple Banana Orange
B.
Apple
Banana
Orange
C.
[Apple, Banana, Orange]
D.
null

Q. How can you add elements to an ArrayList?

A.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
B.
ArrayList<Integer> numbers = new ArrayList<>(2);
numbers[0] = 1;
numbers[1] = 2;
C.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.set(0, 1);
numbers.set(1, 2);
D.
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2));

Q. What will be the output of the following code snippet?

ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");

colors.remove("Green");

for (String color : colors) {
    System.out.print(color + " ");
}
A.
Red Blue
B.
Red
C.
Green Blue
D.
Red Green

Q. How can you get the size of an ArrayList?

A.
ArrayList<String> animals = new ArrayList<>();
int size = animals.size();
B.
ArrayList<String> animals = new ArrayList<>();
int size = animals.length;
C.
ArrayList<String> animals = new ArrayList<>();
int size = animals.getCount();
D.
ArrayList<String> animals = new ArrayList<>();
int size = animals.capacity();

Q. What will be the output of the following code snippet?

ArrayList<Integer> numbers = new ArrayList<>(3);
numbers.add(1);
numbers.add(2);

for (int i = 0; i < numbers.size(); i++) {
    System.out.print(numbers.get(i) + " ");
}
A.
1 2
B.
1
2
null
C.
null 1 2
D.
null null

Q. How can you check if an ArrayList is empty?

A.
ArrayList<String> letters = new ArrayList<>();
if (letters.isEmpty()) {
    // ArrayList is empty
}
B.
ArrayList<String> letters = new ArrayList<>();
if (letters.size() == 0) {
    // ArrayList is empty
}
C.
ArrayList<String> letters = new ArrayList<>();
if (letters.length == 0) {
    // ArrayList is empty
}
D.
ArrayList<String> letters = new ArrayList<>();
if (letters.count() == 0) {
    // ArrayList is empty
}

Q. What will be the output of the following code snippet?

ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

for (int i = 0; i < numbers.size(); i++) {
    numbers.set(i, numbers.get(i) * 2);
}

for (int number : numbers) {
    System.out.print(number + " ");
}
A.
2 4 6 8 10
B.
1 2 3 4 5
C.
2 4 6
D.
10 8 6 4 2

Q. How can you remove all elements from an ArrayList?

A.
ArrayList<String> shapes = new ArrayList<>();
shapes.clear();
B.
ArrayList<String> shapes = new ArrayList<>();
shapes.remove();
C.
ArrayList<String> shapes = new ArrayList<>();
shapes.setSize(0);
D.
ArrayList<String> shapes = new ArrayList<>();
shapes.setCapacity(0);

Q. What will be the output of the following code snippet?

ArrayList<String> words = new ArrayList<>(Arrays.asList("Hello", "World", "Java"));

words.set(1, "Spring");

for (String word : words) {
    System.out.print(word + " ");
}
A.
Hello Spring Java
B.
Hello World Java
C.
Hello Java
D.
Hello World

Q. How can you get the index of a specific element in an ArrayList?

A.
ArrayList<String> countries = new ArrayList<>();
int index = countries.indexOf("India");
B.
ArrayList<String> countries = new ArrayList<>();
int index = countries.getIndex("India");
C.
ArrayList<String> countries = new ArrayList<>();
int index = countries.find("India");
D.
ArrayList<String> countries = new ArrayList<>();
int index = countries.search("India");