CMSC 131 (Summer 2019) Week 09 Study Questions Flashcards

1
Q

Define the terms mutable and immutable.

A

The state of a mutable object may change over time. If an object is immutable, it’s state is set when it is constructed and will never vary after that.

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

Are String objects mutable?

A

NO

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

Are Integer objects mutable? (If you’re not sure, inspect the online API documentation for the Integer class and find out!)

A

NO

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

True/False – Aliasing can lead to problems if the object is mutable.

A

True

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

True/False –Aliasing can lead to problems if the object is immutable.

A

False

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

Suppose you are passing (or returning) an array of primitives to/from a method. Is it safe to make a reference copy only?

A

NO

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

Suppose you are passing (or returning) an array of references to immutable objects to/from a method. Is it safe to make a reference copy only? Is it safe to make a shallow copy?

A

NO, YES

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

Suppose you are passing or returning an array of references to mutable objects to/from a method. Is it safe to make a reference copy only? Is it safe to make a shallow copy?

A

NO, NO

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

How many ints are created by the statement: int[] a = new int[5];

A

5 ints are created – all 0.

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

How many Strings are created by the statement: String[] a = new String[5]; (Hint: The answer to this question and the previous question are different!)

A

None! (Just 5 null references are created – no objects are instantiated).

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

Are the elements of an array of primitives automatically initialized? If so, to what values?

A

Yes, 0

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

Are the elements of an array of references to objects initialized? If so, to what values?

A

Yes, null

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