What is the key difference between LinkedLists and ArrayLists in terms of element access?
LinkedLists require sequential access, while ArrayLists allow direct access using a method call.
Why are generics useful when creating ArrayLists?
They allow the compiler to perform type checking, preventing runtime errors and improving code safety.
Explain the advantage of using ArrayLists over arrays when the size of the data is unknown.
ArrayLists are dynamic and can automatically adjust their size, while arrays have a fixed size.
What is the purpose of importing 'java.util.ArrayList'?
It makes the ArrayList class available for use in your Java program.
Why are wrapper classes used with ArrayLists?
ArrayLists can only store objects, not primitive data types. Wrapper classes allow you to store primitives as objects.
What are the performance implications of using Wrapper Classes?
They are generally slower and use more memory than their primitive counterparts.
When should you consider using regular arrays instead of ArrayLists?
When working with large amounts of data and needing to optimize for performance.
Explain how ArrayLists provide dynamic resizing.
ArrayLists automatically increase their capacity when more elements are added than the current capacity allows.
What is the significance of the '<E>' in 'ArrayList<E>'?
'E' represents the type of object that the ArrayList will store. It allows you to specify the class of objects in the ArrayList.
Explain the concept of type safety in the context of ArrayLists and Generics.
Generics enforce type safety by ensuring that only objects of the specified type can be added to the ArrayList, preventing runtime ClassCastException errors.
What are the differences between ArrayLists and arrays in terms of size?
ArrayLists are dynamic in size; arrays have a fixed size.
What are the differences between ArrayLists and LinkedLists in terms of element access?
ArrayLists allow direct access; LinkedLists require sequential access.
What are the differences between using primitive types (e.g., `int`) directly and using their wrapper classes (e.g., `Integer`) in ArrayLists?
Primitive types cannot be directly stored in ArrayLists, requiring wrapper classes. Wrapper classes have overhead.
Compare the memory usage of ArrayList<Integer> vs. int[].
ArrayList<Integer> uses more memory due to object overhead for each Integer, while int[] uses less memory as it stores primitive integers directly.
Compare the performance of adding elements to the end of an ArrayList vs. adding elements to a LinkedList.
Adding to the end of an ArrayList is generally faster due to direct access, while LinkedList requires traversing to the end.
Compare the ease of use between ArrayLists and arrays when inserting elements in the middle of the list.
ArrayLists provide methods for easy insertion, while arrays require manual shifting of elements.
Compare the type safety of a raw ArrayList (without generics) and an ArrayList with generics.
ArrayList with generics provides compile-time type safety, while a raw ArrayList does not.
Compare the performance of accessing an element in an ArrayList using its index vs. accessing an element in a LinkedList using its index.
ArrayList provides O(1) (constant time) access, while LinkedList provides O(n) (linear time) access.
Compare the use of ArrayLists and arrays in terms of their ability to store null values.
Both ArrayLists and arrays can store null values.
Compare the suitability of ArrayLists and arrays for scenarios where the size of the data collection is known in advance and will not change.
Arrays are more suitable in this scenario due to their lower memory overhead and potentially faster access times.
Identify the error in the following code:
ArrayList list = new ArrayList();
list.add(5);
list.add("hello");
The ArrayList is not generic, so it can hold different types. However, this is generally bad practice, and can lead to runtime errors if you try to use the elements without knowing their type.
Identify the error in the following code:
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(3.14);
Cannot add a double (3.14) to an ArrayList of Integers. Type mismatch.
What import statement is required to use ArrayLists in Java?
import java.util.ArrayList;
Identify the error in the following code:
ArrayList<String> names = new ArrayList<String>(10);
names.add("Alice");
names.add("Bob");
The initial capacity in the constructor does not limit the number of elements that can be added. There is no error here.
What is wrong with the following code?
ArrayList<int> numbers = new ArrayList<int>();
ArrayLists cannot store primitive types directly. You must use the wrapper class 'Integer' instead of 'int'.
What is the output of the following code?
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
System.out.println(list.size());
2
What is the error in the following code?
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
String s = numbers.get(0);
Cannot assign an Integer to a String variable. Type mismatch.
What is the output of the following code?
ArrayList<Boolean> flags = new ArrayList<>();
System.out.println(flags.size());
0
What is wrong with the following code?
ArrayList<Double> values = new ArrayList<>();
values.add(10);
values.add(new Integer(20));
While technically the second add will work due to autoboxing, it's bad practice to add an Integer object to an ArrayList of Doubles.
What is the output of the following code?
ArrayList<String> words = new ArrayList<>();
words.add("hello");
words.add("world");
words.clear();
System.out.println(words.size());
0