week04 inheritande&exceptions Flashcards
What is an array in Java?
An array is a fixed-size collection that can store multiple items of the same type. The size is set when the array is created and cannot change. Arrays are zero-indexed, which means the first element is at index 0.
True or false?
“You can change the size of an array after it is created.”
False. Once created, the size of a Java array cannot change. You must create a new array if you need a different size.
How do you find the number of elements in an array?
Use the length
property of the array. if array
is an array, array.length
gives you the number of elements.
What should you be careful about with arrays of objects?
An array of objects is initialized with null
for each element. You must assign an object to each position before using it. Accessing null
elements will cause a NullPointerException
.
True or false:
“System.arraycopy() creates a deep copy of an array.”
False. It creates a shallow copy, meaning it copies the references, not the actual objects.
What is a multi-dimensional array in Java?
It’s an array of arrays. You can imagine it is like a table with rows and columns.
What is an ArrayList in Java?
An ArrayList is a list that backed by an array. It can grow and shrink. It’s part of Java’s collections Framework and provides more functions than a simple array.
True or false:
“An ArrayList has a fixed size”
False. Unlike arrays, an ArrayList can change its size dynamically.
What are key differences between an ArrayList and an array?
ArrayList can change size, only stores objects, and has many methods. Arrays have fixed size, can store primitives, have no methods, and can be multi-dimensional.
When is it better to use an ArrayList?
Use ArrayList when you need a dynamic collection that chagnes size, when you’re storing objects, and when you want useful methods like add
, remove
, and contains
.
When should you use an array instead of an ArrayList?
Use an array when you know the collection size won’t change, need to store primitives, or don’t need the extra features of ArrayList.
How do you find the size of an array in Java?
Use the length
property. Example: arrayName.length
returns the size as an int
.
What can an array contain in Java?
A single kind of ‘thing’ - primitives, object of a class, but all must be the same type.
What is composition in OOP?
A ‘has-a’ relationship where a class includes object references as instance variables, indicating ownership
What is aggregation in OOP?
A specific ‘has-a’ relationship where a class contains a collection of objects, which can exist independently.