unit 6 Flashcards

1
Q

how do you create a new array?

A

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

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

what are array values set at by default for all different data types?

A

int: 0
double: 0.0
boolean: false
string: null

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

can the amount of values that an array was created with be changed?

A

no

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

declare a new string array, names, with 10 values (but do not create one)

A

String[] names;

you cannot input a # of values when only declaring

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

create a new string array, names, with 10 values (having already been declared)

A

names = new String[10];

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

create a new string array, names, with 10 values, in one line

A

String[] names = new String[5];

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

do parameters edited by a method/within a specific scope stay changed after it ends?

A

no

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

do objects edited by a method/within a specific scope stay changed after it ends?

A

yes

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

objects get passed to a method by reference

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

arrays are immutable (true/false)

A

false

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

int x;
System.out.print(x);
will the above compile?

A

no, x is not initialized

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

true/false: in the statement
double x = Math.random()*10;
x is between the ranges of 1-10

A

no, it is 0-9. you must add one to make it one to ten.

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

what is a foreach loop?

A

loop that traverses each element of the array

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

describe the parts of a foreach loop header

A

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

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

foreach generally copies each element into a new variable of the same type like string methods (true/false)

A

true

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

what datatypes can a for each loop be used on ?

A

arrays (and strings, returning individual characters)

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

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!");
        }
    }
A

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.

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

why doesnt the following work:

int[] j = {1, 2, 3, 4};
System.out.print(j/2);

A

because its an array not an int you need a specified method for that dumbdumb

19
Q

can a for each loop alter the original array?

A

nah

20
Q

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.

A

B

21
Q

true/false: the following array has 101 elements:

int[] x = new int[100];

A

False

22
Q

true/false

java syntax allows programmers to use any expression of the int data type as an array subscript

A

true

23
Q

while a program is running, it verifies that all array subscripts fall into the valid range

A

true

24
Q

any one-dimensional array object has a length method that returns the size of the array

A

false, its a PROPERTY

25
Q

CAN YOU COMPARE OBJECTS LIKE ARRAYS

A

N O

26
Q

Consider the following method, which is intended to return the index of the first negative integer in a given array of integers.

public int positionOfFirstNegative(int[] values)

{

int index = 0;

while (values[index] >= 0)

{

index++;

}

return index;

}

What precondition is needed on the values array so that the method will work as intended?

Responses
A
The array values must contain at least one negative integer.

B
The array values must contain at least one nonnegative integer.

C
The array values must contain at least one positive integer.

D
No precondition is needed. The method will never work as intended.

E
No precondition is needed. The method will always work as intended.

A

option a, as
1. the array cannot be empty, as java will throw an exception and 2. the array must have a negaive or else the iterator increases beyond the last index of the elements and causes an exception to be thrown

27
Q

Consider the following class definition.

public class Toy

{

private int yearFirstSold;

public int getYearFirstSold()

{

return yearFirstSold;

}

/* There may be instance variables, constructors, and other methods not shown. */

}

The following code segment, which appears in a class other than Toy, prints the year each Toy object in toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized array of Toy objects.

for (Toy k : toyArray)

{

System.out.println(k.getYearFirstSold());

}

Which of the following could be used in place of the given code segment to produce the same output?

I.

for (int k = 0; k < toyArray.length; k++)

{

System.out.println(getYearFirstSold(k));

}

II.

for (int k = 0; k < toyArray.length; k++)

{

System.out.println(k.getYearFirstSold());

}

III.

for (int k = 0; k < toyArray.length; k++)

{

System.out.println(toyArray[k].getYearFirstSold());

}

Responses
A
I only

B
II only

C
III only

D
I and II

E
II and III

A

C, as segment I uses k as a parameter, and segment II uses k as an object, whereas III uses k as the correct index of an array

28
Q

Consider the following code segment.

int[] arr = {4, 3, 2, 1, 0};

int total = 0;

for (int k = 0; k <= total; k++)

{

if (arr[k] % 2 == 0)

{

total += arr[k];

}

else

{

total -= arr[k];

}

}

System.out.print(total);

What, if anything, is printed as a result of executing the code segment?

Responses
A
2

B
1

C
0

D
-4

E
Nothing is printed because the code segment causes a runtime error.

A

B, when k gets to 2 total = 1 and therefore does not match the for loop conditions

29
Q

Consider the following code segment.

int[] arr = {10, 20, 30, 40, 50};

for(int x = 1; x < arr.length - 1; x++)

{

arr[x + 1] = arr[x] + arr[x + 1];

}

Which of the following represents the contents of arr after the code segment has been executed?

Responses
A
{10, 20, 30, 70, 120}

B
{10, 20, 50, 90, 50}

C
{10, 20, 50, 90, 140}

D
{10, 30, 60, 100, 50}

E
{10, 30, 60, 100, 150}

A

Answer C
Correct. The code starts with the second element of the array (i.e., the element at index 1) and adds the current element to the next element of the array. The code will set the third element to 20 + 30, which is 50; it will then set the fourth element to 50 + 40, which is 90; finally, it will set the last element to 90 + 50, which is 140.

30
Q

Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects.

public static int checkString(String[] arr)

{

int count = 0;

for (int k = 0; k < arr.length; k++)

{

if (arr[k].length() >= 3)

{

count++;

}

}

return count;

}

Which of the following code segments compile without error?

checkString(new String[]);
checkString(new String[0]);
String[] str = {“cat”, “dog”};
checkString(str);
Responses
A
II only

B
III only

C
I and III only

D
II and III only

E
I, II, and III

A

Answer D, as the size of an array,even if zero, MUST ALWAYS be established before it can be used.

31
Q

Consider the following methods.

public void modParams (int[] x, int[] y, String[] s)
{
x[1] = 5;
s[1] = new String(“five”);
s = new String[3];
s[1] = new String(“six”);
}

public void print90
{
int[] a = {1, 2, 3};
int[] b = {11, 22, 33};
String[] s = {“one”, “two”, “three”};
modParams(a, b, s);
System.out.println(a[1] + “ “ + b[1] + “ “ + s[1]);
}

What is printed when print is called?

A

5 22 five

y = x breaks the link that y had to b, now having the connection to a, or x. a whole ne memory location is received and b is abandoned.

32
Q

can you concatenate strings and integers?

A

yes (2 + 7 + “five” = “27five”)

33
Q

imagine you have 3 properly declared and initlialized arrays. the statement
y = x;
if x += 1 later on in the code, what happens to y?

A

1 is addded to y, y now reflects the memory location of x and therefore all future alterations of x are reflected by y.

34
Q

Consider the following method, which is designed to return the smallest element in the array.

int getMin(int[] nums)
{
int min = nums[0];
for (int n : nums)
statement

 return min; }

which statement should replace statement so that the method works as intended?

A. if (nums[n] < min) min = nums[n];

B. if (n[i] < min) min = n[i];

C. if (n < min) min = n;

D. if (nums[min] < min) min = nums[min];

E. if (nums[0] < min) min = nums[0];

A

C

35
Q

Consider the method below, which is intended to return true if there is at least one duplicate in the array, and false if there are no duplicates.

boolean hasDuplicate(int[] nums)
{
for (int k = 0; k < nums.length - 1; k++)
{
if (nums[k] == nums[k+1])
return true;
}
return false;
}

under which condition will the method not necessarily produce the desired result?

A. when the array is sorted in increasing order

B. When the array is sorted in decreasing order

C. When the values in the array are all positive

D. When the values in the array are all the same

E. When the array has at most 2 elements

A

C

36
Q

13-1-4: Given the following code. Which of the following would return true?

Integer int1 = new Integer(3);
Integer int2 = new Integer(3);
Integer int3 = int2;

I. (int3.equals(int2))
II. (int1.equals(int2))
III. (int3 == int2)
IV. (int1 == int2)
V. (int2 == int3)

A. I and II only
B. I, II, III, and V
C. All will return true
D. I, II, and III only
E. III, IV, and V only

A

A. How about III and V? Since int3 was set to int2 they do refer to the same object.

CORRECT ANSWER:
B. The variables int1 and int2 refer to two different objects (even though they have the same value) so IV will be false.

C. Look at IV. Are int1 and int2 referring to the same object?

D. V is also true.

E. I and II are also true since they have the same value. IV is not since they don’t refere to the same object.

37
Q

13-1-14: Consider the following method and if int[] a = {8, 3, 1}, what is the value in a[1] after m1(a); is run?

public int m1(int[] a)
{
a[1]–;
return (a[1] * 2);
}
A. 4
B. 16
C. 7
D. 2
E. 3

A

A. This would be true if it was return(a[1]*= 2);.

B. This would be true if the return statement was return (a[0]*=2);.

C. This would be true if it was a[0]–; Or it would be true if array indicies started at 1, but they start with 0.

CORRECT ANSWER:
D. The statement a[1]–; is the same as a[1] = a[1] - 1; so this will change the 3 to a 2. The return (a[1] * 2) does not change the value at a[1].

E. This can’t be true because a[1]–; means the same as a[1] = a[1] - 1; So the 3 will become a 2. Parameters are all pass by value in Java which means that a copy of the value is passed to a method. But, since an array is an object a copy of the value is a copy of the reference to the object. So changes to objects in methods are permanent.

38
Q

5-15-8: Which of the following statements are TRUE about local variables?

Local variables can be declared in the body of constructors and methods.

Local variables may only be used within the constructor or method and cannot be declared to be public or private.

When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.

A. I only
B. I and II only
C. I and III only
D. I, II, and III
E. II and III only

A

A. It’s true that the local variables can be declared in the body of constructors and methods, but there are other options that are also true about local variables.

B. Both I and II are true but III is also true regarding local variables.

C. Both I and III are true but II is also true regarding local variables.

CORRECT ANSWER:
D. Correct! All of the above are true.

E. Both of these are true but I is also true.

39
Q

5-15-6: Consider the following class with the method test. What is the output after the main method is executed calling test(s,b)?

public class Test1
{
public static void test(String str, int y)
{
str = str + “bow”;
y = y * 2;
}

public static void main(String[] args)
{
    String s = "rain";
    int b = 4;
    test(s, b);
    System.out.println("s=" + s + "; b=" + b);
} } A. s="rainbow"; b=8; B. s="rain"; b=8; C. s="rainbow"; b=4; D. s="rain"; b=4; E. s="bow"; b=4;
A

A. Strings are immutable so changing str doesn’t affect the string that s refers to.

B. Nothing done in the method test affects the value of b.

C. Strings are immutable so changing str doesn’t affect the string that s refers to.

D. Correct!

E. All changes to string s result in a new string object.

40
Q

5-15-11: What does the following code print?

int x = -5;
while (x < 0)
{
x++;
System.out.print(x + “ “);
}
A. 5 4 3 2 1
B. -5 -4 -3 -2 -1
C. -4 -3 -2 -1 0
D. -5 -4 -3 -2 -1 0
E. -4 -3 -2 -1
A. x is initialized (set) to -5 to start and incremented (x++) before the print statement executes.
B. x is incremented (x++) from -5 before the print statement executes.
C. Correct!
D. x is incremented (x++) from -5 before the print statement executes.
E. 0 is printed out the last time through the loop when x is -1 and is incremented.

A

A. x is initialized (set) to -5 to start and incremented (x++) before the print statement executes.

B. x is incremented (x++) from -5 before the print statement executes.

C. Correct!

D. x is incremented (x++) from -5 before the print statement executes.

E. 0 is printed out the last time through the loop when x is -1 and is incremented.

41
Q

6-9-2: Which of the following declarations will cause a compile time error?

A. int[] scores = null;
B. int[] scoreArray = {50,90,85};
C. String[] nameArray = new String[10];
D. String[] nameArray = {5, 3, 2};
E. int[] scores = new int[5];

A

D

42
Q

6-9-11: Consider the following data field and method. Which of the following best describes the contents of myStuff in terms of m and n after the following statement has been executed?

private int[] myStuff;

//precondition: myStuff contains
// integers in no particular order
public int mystery(int num)
{
for (int k = myStuff.length - 1; k >= 0; k–)
{
if (myStuff[k] < num)
{
return k;
}
}
return -1;
}

int m = mystery(n)
A. All values in positions m+1 through myStuff.length-1 are greater than or equal to n.
B. All values in position 0 through m are less than n.
C. All values in position m+1 through myStuff.length-1 are less than n.
D. The smallest value is at position m.
E. The largest value that is smaller than n is at position m.

A

A ✔️ Mystery steps backwards through the array until the first value less than the passed num (n) is found and then it returns the index where this value is found. Nothing is known about the elements of the array prior to the index at which the condition is met.

43
Q

does a for each variable edit the actual loop being passed thru?

A

It just sets up a variable that is set to each value in the array successively. (temp variable COPY)

44
Q
A