arrays Flashcards

1
Q

Visualize an array as a row of boxes or elements, each one with an index number under it. We use zero-based indexing with arrays, so the first box is always numbered

A

0

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

True or false :important Point : Data types may not be mixed in the elements of an array. (T!)

A

true

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

Arrays are ______with respect to data type. This means all the data values must be of the same type.

A

homogeneous

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

the operator we use in creating an array is the square brackets [ ], also called the . (T!)

A

ARRAY INDEX OPERATOR

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

Arrays are in Java.They are declared, and then instantiated.

A

objects

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

is preferred way. (hard to miss seeing that array index operator when it’s in the middle)

A

Datatype [ ] arrayName

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

datatype may be a

A

primitive, or any object.

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

instantiate or build the array, we use the new keyword and then specify how many elements we want.

A

int [ ] myIntArray = new int [10]

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

once created, the array size is fixed, or

A

immutable

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

last element of any array will have an index number equal to the

A

(number of elements – -1 )

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

Java offers a shortcut way to declare, create and populate an array all at once by enclosing the data values inside curly braces. (T!

A

String [ ] monthsArray = {“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”, ”Nov”,”Dec”};

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

hen array is created, Java will automatically assign a default value to each element. (T!)

A

Arrays of primitive numerics (i.e. int, short, double, float, etc) get a zero value assigned to each element. Boolean array elements get initialized to are false, char array elements get assigned the unicode character of ‘u0000’.Any array of objects, such as Strings, will gets NULL assigned to each element.

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

Q. should we be using the < operator or the <= operator in the loop continuation condition expression?

A

A. ALWAYS use the < operator with arrayName.length… using <= will send you out of bounds.(T

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

Math.random( ) returns a double value ranging from

A

000 up to 0.999999999999999. Note it does not return 1.0(T!)

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

(int)(Math.random() * (highValue – lowValue + 1) + lowValue);

A

general formula for generating a range of values using Math.random

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

(int)(Math.random() * (highValue – lowValue + 1) + lowValue);

A

general formula for generating a range of values using Math.random

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

An array is called a random access type of structure because access to any element in the array takes the same amount of time. (T!

A

Q. How does this work?A. Program just has to calculate the OFFSET from the starting address of element 0 of the array by multiplying the data type size in bits by the index number of the desired element. OFFSET = (data type size in bits) * element index number

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

So, what would the starting address of element [10] be?Offset = index# * data type size = 10 * 32 = 320 bits.

A

by doing this simple calculation, your program can get access to any element in the array in the same amount of time.

Therefore, start address of element[10] would be bit 1000 + 320=1320.For element[67], offset would be 67 *32 bits = 2144 bits.Therefore, start address of element[67] would be 1000 + 2144=3144

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

T or F

A

t takes the same amount of time to retrieve the value at element[16] as it does to retrieve the value at element[98].

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

Arrays: Benefits and Disadvantages:

A

Advantages:Retrieving data from any array element takes the same amount of time if you know what element it is in.DisadvantagesArray size is fixed (immutable) once created. If you run out of space you need to create a new larger array and copy to it.Inserting a new value into the middle of a sorted array is not simple. It involves a lot of copying of the element values and then creating a new array to hold the re-sorted elements. If your data needs to be sorted and it does not change much and you need fast access to it, then an array is a good choice. (fast retrieval…T!)

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

arraylist is different in which ways?

A

An ArrayList is not restricted to holding values of just one data type like an array. It is heterogeneous, because it holds the references to the memory addresses of objects rather than the objects themselves. (T!)2) ArrayLists are dynamic (i.e. elastic). If you fill up all the slots, it automatically creates new ones so that you can add more. (T!)

22
Q

ArrayLists and vectors, a close cousin of the arrayList, can only store object references ( the memory addresses of objects), not primitive data values such as ints or floats.

A

T

23
Q

T or F With the exception of the array data type( it’s homogeneous…T!) all collection objects are capable of storing more than one type of object.

A

T

24
Q

T or F This is due to the fact that when we store objects in an arrayList or vector, we are actually just storing the memory addresses of those objects, not the objects themselves. (T!)

A

T

25
Q

ArrayList myStringList = new ArrayList();

A

will only store strings

26
Q

find out how many FILLED elements are in an arrayList object,

A

u use the .size() method.

27
Q

EnsureCapacity(int n); //i

A

ncreases capacity to ‘n’ elements

28
Q

trimToSize(); //del

A

deletes empty elements on the end to save memory.a

29
Q

Add( obj); //adds specified object to end of list.add(int index, obj); //inserts specified object to element at // specified index. Other values will “move over” to let //the new one in. No overwrite will occur..remove(int index); //deletes element at this index and t

A

true

30
Q

Add( obj); //adds specified object to end of list.add(int index, obj); //inserts specified object to element at // specified index. Other values will “move over” to let //the new one in. No overwrite will occur..remove(int index); //deletes element at this index and t

A

true

31
Q

True or F Traversing a collection object such as an array, an arrayList, or a vector means that each and every value in the collection is visited during the traversal operation. (T!)

A

T

32
Q

The enhanced for loop (or for:each loop) is good if you want to traverse every element in the data structure. (T!)

A

True

33
Q

HOWEVER, you cannot use the for:each loop for these situations:If you need to change the contents of an elementIf you need to traverse the elements in reverse orderIf you need to access only some, not all, of the elementsIf you need to access more than one array or ArrayList at the same time inside the loopIf you need to refer to the index number of a particular element.

A

do any of the above operations, you need to use a regular for loop. (T!

34
Q

Can data types be mixed in an array

A

no fucker

35
Q

Two D array first int[][] tableArray = new int [2][3]

Is 2 the row?

A

2 rows 3 columns

36
Q

A void method does notreturn a value

A

true(is a keyword)

37
Q

A non-void method does return a value

A

true (non void is not a keyword)

38
Q

Java methods that don’t return anything are usually called in a statement, and are not usually part of a mathematical expression. (T!)

A

Again, these are called void methods. (T!)

39
Q

KEY POINT: if you want to use an method that is an object method, then you have to create an object of the class first. (T!)

A

True

40
Q

The method is associated with or attached to the object. If you don’t have an object created, you cannot call the method. (T!)

A

True

41
Q

Benefit of a class method…

A

YOU DO NOT HAVE TO CREATE AN OBJECT OF THE CLASS IN WHICH THE METHOD IS DEFINED IN ORDER TO CALL THE METHOD. (T!)
To call this method, we use the ClassName.methodName() syntax (T!)

42
Q

How do you designate a method as a static or class method?

A

You include the static keyword in the method definition header.

43
Q

General syntax to define a method:

A

modifier returnValueType methodName (parameter list){//method body statements go here} // end method

44
Q

static keyword is used to create a

A
class method
Class method? Means you don’t have to create an object of the class in order to call the method
45
Q

4 parts of a method header

A

modifier – is optional. Keywords such as static, public, protected, or private may appear here. More on these later returnValueType- if the method is a function that will return a value to the line that called it, the datatype of the returned value is specified here.methodName- Name of the method should indicate what it does by using a VERB (an action word) as first part of name.parameter list; You specify what arguments or parameters (data values) are to be accepted by the method by listing them in the parameter list. You specify the data type and provide a formal parameter name for each argument that will be passed in.

46
Q

Having two or more methods in the same class with the same name but different parameter lists is called method overloading. (T!)

A

true

47
Q

Swap after one complete pass through the array the largest value in the array will end up in the

A

last element.

48
Q

How many comparisons of values are made in one pass of a bubble sort? (T!)

A

Number of comparisons is (array.length – 1)…one less than the number of elements. If there are 5 elements, we make (5-1) or 4 comparisons. (T!)

49
Q

How many passes do you have to make in a bubble sort to completely sort an array of ‘n’ elements? (T

A

Number of passes to completely sort an array of ‘n’ elements is equal to ‘n-1’. (T!) gives us a clue to what our loop continuation condition has to be to avoid the out of bounds error…it will be (array.length – 1)

50
Q

Caracteristics of recursion(T!

A

All problems/solutions involving recursion have the following characteristics:1)One or more base cases (AKA stopping cases) are used to stop the method from making a recursive call. 2)For fibonacci, the two base cases are when n=1 or n=0…if n=1 we return 1, or if n = 0 we return a zero. In all other cases we call the method recursively.3)Every recursive call reduces the original problem one step closer to the base case(s

51
Q

selection sort

A

each pass results in one less element to look at during the next pass