zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion Bank

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.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

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.

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 vs. int[].

ArrayList 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 numbers = new ArrayList(); 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 names = new ArrayList(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 numbers = new 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 list = new ArrayList<>(); list.add("apple"); list.add("banana"); System.out.println(list.size());

2

What is the error in the following code? ArrayList 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 flags = new ArrayList<>(); System.out.println(flags.size());

0

What is wrong with the following code? ArrayList 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 words = new ArrayList<>(); words.add("hello"); words.add("world"); words.clear(); System.out.println(words.size());

0