Week 9 Flashcards

1
Q

What is the Java Collections Framework?

A

A framework in Java for storing and manipulating groups of objects

Provides interfaces, implementations, and algorithms for common data structures and collections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the core interfaces in the Collections Framework?

A

Collection<E>: A group of objects (elements)</E>

Map<K, V>: A mapping of keys to values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the Collection<E> interface?</E>

A

Methods include:

Size and emptiness checks: size(), isEmpty()

Membership operations: contains(Object), add(E), remove(Object)

Bulk operations: addAll(Collection), removeAll(Collection), retainAll(Collection)

Array conversions: toArray()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the key characteristics of a Set<E>?</E>

A

No duplicate elements

Common implementations:

HashSet<E>: Unordered, fast operations</E>

TreeSet<E>: Elements are sorted, slower than HashSet</E>

LinkedHashSet<E>: Maintains insertion order</E>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the characteristics of a List<E>?</E>

A

Ordered collection, allows duplicate elements

Methods for positional access: get(int index), add(int index, E), remove(int index)

Iteration support via ListIterator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the main implementations of List<E>?</E>

A

ArrayList<E>: Backed by a dynamically resizing array, fast random access</E>

LinkedList<E>: Doubly linked list, better for frequent insertions/removals</E>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Queue<E>?</E>

A

Collection for processing elements, typically in first-in-first-out (FIFO) order

Methods:

Add elements: add(E), offer(E)

Access head: peek(), poll(), remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Deque<E>?</E>

A

Double-ended queue, allows insertions and deletions at both ends

Examples: ArrayDeque, LinkedList (as a Deque)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Map<K, V>?

A

Maps keys to values; each key maps to at most one value

Methods include:

Basic operations: put(K, V), get(Object), remove(Object)

Bulk operations: putAll(Map), clear()

Views: keySet(), values(), entrySet()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the main implementations of Map<K, V>?

A

HashMap<K, V>: Unordered, fast operations

TreeMap<K, V>: Keys sorted in natural or custom order

LinkedHashMap<K, V>: Maintains insertion order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an Iterator<E>?</E>

A

Used to traverse a collection.
Methods:

hasNext(): Checks if the next element exists

next(): Retrieves the next element

remove(): Removes the last element returned by next()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a ListIterator<E>?</E>

A

Extends Iterator with bi-directional traversal:

hasPrevious(), previous().

add(E), set(E).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the advantages of using the Java Collections Framework?

A

Simplifies programming by providing ready-to-use data structures

Ensures type safety with generics

Improves performance with optimized implementations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What should you know about the order and uniqueness in collections?

A

Set: No duplicates, unordered or ordered based on implementation

List: Allows duplicates, ordered

Queue: FIFO or based on other policies, allows duplicates

Map: No duplicate keys, values can be duplicated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Java Reflection?

A

The ability of a class or object to examine and manipulate itself

Enables accessing constructors, methods, and fields at runtime

Allows modifying private fields, invoking methods, and constructing new objects dynamically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What package supports Reflection in Java?

A

The java.lang.reflect package

17
Q

How does Reflection interact with Java security?

A

The JVM’s security manager restricts Reflection to prevent unauthorized access to private fields or methods

Privileges can be granted in specific scenarios (e.g., IDE debugging)

18
Q

What is the entry point for Reflection in Java?

A

The java.lang.Class object:

Immutable instance created by the JVM for every object type

Provides methods to query object properties and create new objects

19
Q

How can you get a Class object?

A

Using getClass() on an object instance:

Class<?> c = obj.getClass();

Using .class on a type:

Class<?> c = int.class;

Using Class.forName(String) with a fully qualified name:

Class<?> c = Class.forName(“java.lang.String”);

20
Q

What are some key methods in the Class object?

A

getFields(): Public fields, including inherited ones

getDeclaredFields(): All fields, irrespective of access modifiers, excluding inherited ones

getMethods(): Public methods, including inherited ones

getDeclaredMethods(): All methods, irrespective of access modifiers, excluding inherited ones

getConstructors(): Public constructors.
getDeclaredConstructors(): All constructors, including private ones

21
Q

How are objects instantiated reflectively?

A

Two methods are commonly used:

Class.newInstance():

Invokes no-argument constructor.

Requires the constructor to be visible.

Constructor.newInstance():

Can invoke any constructor, including private ones (if allowed)

Preferred method

22
Q

What is AccessibleObject.setAccessible()?

A

Temporarily overrides JVM security to allow access to private members

Usage depends on the security manager and JVM policy

23
Q

What are the limitations and risks of Reflection?

A

Performance overhead: Reflective operations are slower than direct calls

Security concerns: Reflection can access private fields and methods

Compatibility issues: Reflective code may break with JVM upgrades

Potential side effects: May render code non-portable and dysfunctional

24
Q

What are common use cases for Reflection?

A

Debugging: Inspecting running programs

Testing: Forcing specific states or ensuring code coverage

Frameworks: Dynamically handling objects and classes

25
Q

What are the performance considerations for Reflection?

A

Reflective operations should be avoided in performance-sensitive code due to their slower execution compared to non-reflective counterparts