Week 2 Flashcards
What are the three principles of Object Oriented Programming (OOP).
Encapsulation
* Direct access to components is restricted
Abstraction
* Complexity is reduced
* All but essential details are removed
Inheritance
* Method of deriving one class from another
In Java can you have two methods with the same name which differ only by return type.
You cannot have two methods with the same name which differ only be return type.
Are arrays objects?
Which data types can arrays contain?
Arrays are not objects.
Arrays can conatin primitive data types or references to objects.
What are the default values for the following:
int
boolean
double/float
char
objects
int: 0 boolean: false double/float: 0.0 char: \u0000 (null character) objects: null
In Java can you have arrays with different data types?
No, you cannot. They must be the same type.
In Java create an integer array called implicitLengthArray containing the following values, {1, 2, 3, 5}
int[] implicitLengthArray = {1, 2, 3, 5}
Create a dynamically a new array with 3 integers.
int[] explicitLengthArray = new int[3];
What does the following code do?
int[] explicitLengthArray = new int[3]; // Copy an array (by reference) int[] copyArray = explicitLengthArray; copyArray[0] = 80000; System.out.println(explicitLengthArray[0]);
This is a copy of by reference. They are referencing the same object, so any changes done may effect the other array.
What does the Arrays.copyOfRange do? What do we need to import in order for it to work?
We need to
~~~
import java.util.Arrays;
~~~
An example of this is:
char[] originalCharArray = {'a', 'B', 'c', 'D', '5'}; char[] subsetCharArray = Arrays.copyOfRange(originalCharArray, 1, 4);
copyOfRange takes two values, it is inclusive of the first value, but not inclusive of the last value.
What are the arguments? What do they do?
System.arraycopy(originalCharArray, 1, subsetCharArray, 0, 2);
The arguments are as follows
1. the array to be copied from
2. The index of the original array to start copying from
3. The array being copied to
4. The index of the array being copied to should start
5. The number of elements that should.
How do we declare a multidimensional array.
String nested[][] = {{"Apple", "Bread", "Corn"}, {"Aardvark", "Buffalo"}}