CMSC 131 (Summer 2019) Week 09 Study Questions Flashcards
Define the terms mutable and immutable.
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.
Are String objects mutable?
NO
Are Integer objects mutable? (If you’re not sure, inspect the online API documentation for the Integer class and find out!)
NO
True/False – Aliasing can lead to problems if the object is mutable.
True
True/False –Aliasing can lead to problems if the object is immutable.
False
Suppose you are passing (or returning) an array of primitives to/from a method. Is it safe to make a reference copy only?
NO
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?
NO, YES
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?
NO, NO
How many ints are created by the statement: int[] a = new int[5];
5 ints are created – all 0.
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!)
None! (Just 5 null references are created – no objects are instantiated).
Are the elements of an array of primitives automatically initialized? If so, to what values?
Yes, 0
Are the elements of an array of references to objects initialized? If so, to what values?
Yes, null