List Class Questions and Answers
Graded A+
How do you add an element to an `ArrayList`?
A) `list.addElement(value);`
B) `list.insert(value);`
✔✔C) `list.add(value);`
D) `list.put(value);`
What is the initial capacity of an empty `ArrayList` in Java?
✔✔A) 10
B) 1
C) 0
D) It depends on the type of elements stored
Which statement is true about `ArrayList` in Java?
A) It is a fixed-size data structure
B) It only holds integer values
1
,✔✔C) It resizes dynamically when elements are added
D) It does not allow duplicate elements
How do you remove an element from an `ArrayList` by its index?
✔✔A) `list.remove(index);`
B) `list.delete(index);`
C) `list.clear(index);`
D) `list.removeElementAt(index);`
Which of the following statements about arrays is correct?
✔✔A) Arrays have a fixed length once declared
B) Arrays automatically resize when needed
C) Arrays in Java cannot hold primitive data types
D) Arrays can store multiple types of data
What will happen if an `ArrayList` reaches its maximum capacity?
A) It throws an error
✔✔B) It increases its size automatically
2
, C) It replaces old elements with new ones
D) It stops accepting new elements
What happens if you try to access an array index that does not exist?
A) The program skips the operation
✔✔B) An `ArrayIndexOutOfBoundsException` is thrown
C) Java automatically resizes the array
D) The value at index 0 is returned
Which of the following correctly initializes an array with five elements?
A) `int[5] numbers;`
B) `int numbers = new int(5);`
✔✔C) `int[] numbers = new int[5];`
D) `int numbers[] = {5};`
How can you get the length of an array in Java?
✔✔A) `array.length`
B) `array.length()`
3