unit 6 Flashcards
(44 cards)
how do you create a new array?
first DECLARE
(int[] scores; OR all in one line)
then INITIALIZEEEEE
(int[] scores;
scores = new int[5];)
OR
int[] scores = new int[5];
this CREATES THE MEMORY LOCATION,
unlike just declaring it w/
int[] scores;
which does not do that
what are array values set at by default for all different data types?
int: 0
double: 0.0
boolean: false
string: null
can the amount of values that an array was created with be changed?
no
declare a new string array, names, with 10 values (but do not create one)
String[] names;
you cannot input a # of values when only declaring
create a new string array, names, with 10 values (having already been declared)
names = new String[10];
create a new string array, names, with 10 values, in one line
String[] names = new String[5];
do parameters edited by a method/within a specific scope stay changed after it ends?
no
do objects edited by a method/within a specific scope stay changed after it ends?
yes
objects get passed to a method by reference
arrays are immutable (true/false)
false
int x;
System.out.print(x);
will the above compile?
no, x is not initialized
true/false: in the statement
double x = Math.random()*10;
x is between the ranges of 1-10
no, it is 0-9. you must add one to make it one to ten.
what is a foreach loop?
loop that traverses each element of the array
describe the parts of a foreach loop header
for((datatype of array ) (user designated name) : (arrayname)
remember that THERE IS NOT ITERATOR in a foreach header, rather the user designated name is the value within the 1st, 2nd, etc. element of the array
foreach generally copies each element into a new variable of the same type like string methods (true/false)
true
what datatypes can a for each loop be used on ?
arrays (and strings, returning individual characters)
what are the TWO things wrong with the following code (assuming all variables to be properly declared and initialized):
for (int evenNumbers: values) { if (values[] % 2 == 0) { System.out.println(values[] + " is even!"); } }
the code used values as the “iterated” numbers, rather than the declared new variable declared int evenNumbers. also, even if it was values there should be no [] as the new variable is not an array.
why doesnt the following work:
int[] j = {1, 2, 3, 4};
System.out.print(j/2);
because its an array not an int you need a specified method for that dumbdumb
can a for each loop alter the original array?
nah
What are some of the reasons you would use an enhanced for-each loop instead of a for loop?
I: If you wish to access every element of an array.
II: If you wish to modify elements of the array.
III: If you wish to refer to elements through a variable name instead of an array index.
A. Only I.
B. I and III only.
C. II and III only.
D. All of the Above.
B
true/false: the following array has 101 elements:
int[] x = new int[100];
False
true/false
java syntax allows programmers to use any expression of the int data type as an array subscript
true
while a program is running, it verifies that all array subscripts fall into the valid range
true
any one-dimensional array object has a length method that returns the size of the array
false, its a PROPERTY