All Flashcards
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
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
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
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
2
What is the error in the following code?
ArrayList
Cannot assign an Integer to a String variable. Type mismatch.
What is the output of the following code?
ArrayList
0
What is wrong with the following code?
ArrayList
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
0
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' 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 is the definition of Java Collections Framework?
A set of data structures used to store data in Java, including sets, lists, deques, and maps.
What is a Set?
A data type where each item occurs only once, and the data is unordered.
What is a List?
A collection of items that can repeat, and the data is ordered.
What is a Deque?
Similar to lists, but items can only be added or removed at the beginning and the end.
What is a Map?
Pairs with a key and a value to represent pairs of items.
What is an ArrayList?
A dynamic array that can grow or shrink in size during runtime.
What are Generics?
A feature that allows specifying the type of objects an ArrayList can hold, enabling compile-time type checking.
What is a Wrapper Class?
A class that encapsulates a primitive data type, like Integer for int or Double for double.
What is autoboxing?
Automatic conversion between primitive data types and their corresponding wrapper classes.
What is unboxing?
Automatic conversion from wrapper class objects to their corresponding primitive data types.