Java Foundations 6 Flashcards

1
Q

What is implicit (widening) type casting in Java?

A

Automatically converting a smaller type to a larger type (e.g., int to long).

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

Give an example of implicit type casting.

A

int a = 5;
double b = a;
// int to double (widening)

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

What is explicit (narrowing) type casting in Java?

A

Manually converting a larger type to a smaller type using parentheses (e.g., double to int).

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

Give an example of explicit type casting.

A

double d = 9.7;
int i = (int) d; // i becomes 9

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

Why can narrowing conversion cause data loss?

A

Because the destination type may not be able to hold all the values of the original type (e.g., decimal is truncated).

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

What are Java’s 8 primitive types?

A

byte, short, int, long, float, double, char, boolean

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

What are wrapper classes in Java?

A

Object representations of primitive types (e.g., Integer for int, Double for double).

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

Why use wrapper classes?

A

For collections (List, Map), nullability, and using utility methods (e.g., Integer.parseInt()).

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

What is autoboxing in Java?

A

Automatic conversion of a primitive to its corresponding wrapper object (e.g., int → Integer).

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

What is unboxing in Java?

A

Automatic conversion of a wrapper object to its corresponding primitive (e.g., Integer → int).

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

Example: What does this line do?

List<Integer> list = new ArrayList<>();
list.add(5);</Integer>

A

Autoboxes the int 5 into an Integer object and adds it to the list.

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

Can you cast between unrelated wrapper classes (e.g., Integer to Double)?

A

No, wrapper classes are final and don’t extend each other. You must unbox and then cast primitives.

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