Saturday, August 3, 2024

ArrayList MCQs

 ArrayList MCQs

What is the initial capacity of an ArrayList in Java when it is created with no arguments?


A. 5

B. 10

C. 15

D. 20

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


A. addAt(index, element)

B. insert(index, element)

C. add(index, element)

D. set(index, element)

What will be the output of the following code?


java

Copy code

ArrayList<String> list = new ArrayList<>();

list.add("A");

list.add("B");

list.add("C");

list.remove(1);

System.out.println(list);

A. [A, B, C]

B. [A, C]

C. [B, C]

D. [A]

Which method is used to retrieve an element from an ArrayList at a specific index?


A. get(index)

B. fetch(index)

C. retrieve(index)

D. elementAt(index)

How do you check if an ArrayList contains a specific element?


A. contains(element)

B. has(element)

C. find(element)

D. includes(element)

Which method removes all elements from an ArrayList?


A. clear()

B. delete()

C. removeAll()

D. empty()

What is the time complexity of accessing an element at a specific index in an ArrayList?


A. O(1)

B. O(n)

C. O(log n)

D. O(n^2)

Which method is used to check the size of an ArrayList?


A. length()

B. size()

C. count()

D. getSize()

How can you add all elements of one ArrayList to another ArrayList?


A. addAll(Collection c)

B. insertAll(Collection c)

C. appendAll(Collection c)

D. merge(Collection c)

What will be the output of the following code?


java

Copy code

ArrayList<Integer> list = new ArrayList<>();

list.add(1);

list.add(2);

list.add(3);

list.set(1, 5);

System.out.println(list);

A. [1, 5, 3]

B. [1, 2, 3]

C. [5, 2, 3]

D. [1, 5]

Which method is used to remove an element from an ArrayList at a specific index?


A. remove(index)

B. delete(index)

C. discard(index)

D. erase(index)

What is the default behavior of the add() method when adding an element to an ArrayList?


A. Adds the element at the end of the list.

B. Adds the element at the beginning of the list.

C. Inserts the element at a specific position.

D. Replaces the element at the end of the list.

Which of the following statements is true about ArrayList in Java?


A. ArrayList is synchronized.

B. ArrayList is thread-safe.

C. ArrayList allows duplicate elements.

D. ArrayList does not allow null elements.

How do you check if an ArrayList is empty?


A. isEmpty()

B. empty()

C. isNull()

D. hasNoElements()

Which method is used to sort an ArrayList of elements?


A. sort(Comparator c)

B. order()

C. arrange()

D. organize()

What is the time complexity of removing an element by index in an ArrayList?


A. O(1)

B. O(n)

C. O(log n)

D. O(n^2)

What will be the output of the following code?


java

Copy code

ArrayList<String> list = new ArrayList<>();

list.add("A");

list.add("B");

list.add("C");

list.remove("B");

System.out.println(list);

A. [A, B, C]

B. [A, C]

C. [B, C]

D. [A, B]

Which method is used to ensure that a specified element is present in an ArrayList?


A. add(element)

B. ensure(element)

C. check(element)

D. confirm(element)

What will list.indexOf("C") return if "C" is the third element in the ArrayList?


A. 2

B. 3

C. 1

D. 0

Which method is used to create a new ArrayList from an existing one?


A. clone()

B. copy()

C. duplicate()

D. new ArrayList<>(existingList)

Answers:

B. 10

C. add(index, element)

B. [A, C]

A. get(index)

A. contains(element)

A. clear()

A. O(1)

B. size()

A. addAll(Collection c)

A. [1, 5, 3]

A. remove(index)

A. Adds the element at the end of the list.

C. ArrayList allows duplicate elements.

A. isEmpty()

A. sort(Comparator c)

B. O(n)

B. [A, C]

A. add(element)

A. 2

D. new ArrayList<>(existingList)

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...