ECM2414 generics Flashcards

1
Q

What is the purpose of Java generics?

A

Java generics add stability by detecting more bugs at compile time, enable code reuse with different inputs, and improve type safety by removing the need for casting.

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

When were generics introduced to Java?

A

Generics were added in Java 5.0.

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

What problem arises from using a generic Object container?

A

It requires manual casting, which can lead to runtime errors if incorrect types are used.

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

How are generic type parameters conventionally named?

A

Commonly used single, uppercase letters like:

T (Type)
E (Element)
K (Key)
V (Value)
N (Number)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a bounded type in Java generics?

A

A bounded type restricts the type parameters using extends (upper bound) or super (lower bound), e.g., <T>.</T>

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

What is type erasure in Java generics?

A

Type erasure removes all generic type information at compile time, ensuring backward compatibility with older versions of Java.

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

What are wildcard generics?

A

The ? wildcard represents an unknown type and is used for flexibility, e.g., Container<? extends Number>.

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

Why should you avoid mixing legacy and generic types?

A

Mixing them can cause unchecked warnings and defeat the purpose of generics by reintroducing potential runtime bugs.

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

What is heap pollution in Java generics?

A

Heap pollution occurs when a variable of a parameterized type refers to an object not of that type, potentially causing runtime errors.

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

What advantage do generics provide during compilation?

A

Generics enable the compiler to catch type-related errors, reducing runtime exceptions.

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