private int[ ] a = {1, 3, -5, -2};
public void multAll(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt;
i++;
} // end while
} // end method
{1, 3, -5, -2}
This would be true if the contents of arrays could not be changed but they can.
{3, 9, -15, -6}
This code multiplies each value in a by the passed amt which is 3 in this case.
{2, 6, -10, -4}
This would be correct if we called multAll(2) instead of multAll(3).
The code will never stop executing due to an infinite loop
The variable i starts at 0 and increments each time through the loop and stops when it equals the number of items in a.
boolean temp = false;
for (int i = 0; i < a.length; i++)
{
temp = (a[i] == val);
}
return temp;
whenever the first element in a is equal to val
It is the last value in a that controls the final state of temp, as the loop is progressing through the array from 0 to the end.
Whenever a contains any element which equals val
Because temp is reset every time through the loop, only the last element controls whether the final value is true or false.
Whenever the last element in a is equal to val
Because each time through the loop temp is reset, it will only be returned as true if the last value in a is equal to val.
Whenever more than 1 element in a is equal to val
Because temp is reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for just the last value to be equal to val.
Whenever exactly 1 element in a is equal to val
Because temp is reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for several elements to be equal to val.
Consider the following data field and method findLongest. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example given the code below the call findLongest(10) should return 3, the length of the longest consecutive block of 10s. Which of the following best describes the value actually returned by a call to findLongest?
private int[] nums = {7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10};
public int findLongest(int target)
{
int lenCount = 0; // length of current consecutive numbers
int maxLen = 0; // max length of consecutive numbers
for (int k = 0; k < nums.length; k++)
{
if (nums[k] == target)
{
lenCount++;
} else if (lenCount > maxLen)
{
maxLen = lenCount;
}
}
if (lenCount > maxLen)
{
maxLen = lenCount;
}
return maxLen;
}
It is the length of the shortest consecutive block of the value target in nums
It doesnβt reset lenCount ever, so it just counts all the times the target value appears in the array.
It is the length of the array nums
The only count happens when lenCount is incremented when nums[k] == target. nums.length is only used to stop the loop.
It is the length of the first consecutive block of the value target in nums
It doesnβt reset lenCount ever, so it just counts all the times the target value appears in the array.
It is the number of occurrences of the value target in nums
The variable lenCount is incremented each time the current array element is the same value as the target. It is never reset so it counts the number of occurrences of the value target in nums. The method returns maxLen which is set to lenCount after the loop finishes if lenCount is greater than maxLen.
It is the length of the last consecutive block of the value target in nums
It doesnβt reset lenCount ever, so it just counts all the times the target value appears in the array.
You can step through the code above with the Java Visualizer by clicking the following link Viz-3. Can you fix the code in the Java Visualizer so that it works as intended?
Given the following array instance variable and method, which of the following best describes the contents of myStuff in terms of m and n after int m = mystery(n); 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)
All values in positions m+1 through myStuff.length-1 are greater than or equal to n.
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.
All values in position 0 through m are less than n.
Mystery steps backwards through the array and quits the first time the value at the current index is less than the passed num ( n). This would be true if we went forward through the array and returned when it found a value greater than the passed num ( n).
All values in position m+1 through myStuff.length-1 are less than n.
This would be true if it returned when it found a value at the current index that was greater than num ( n).
The smallest value is at position m.
The condition compares the value at the current index of the array to the passed num. It returns the first time the condition is met so nothing is known about the values which are unchecked. One of the unchecked values could be smaller.
The largest value that is smaller than n is at position m.
The condition checks for any value that is smaller than the passed num and returns from mystery the first time that the condition is encountered. The values are not ordered so we donβt know if this is the largest value smaller than n.
private int[] arr;
// precondition: arr.length != 0
public int checkArray()
{
int loc = arr.length / 2;
for (int k = 0; k < arr.length; k++)
{
if (arr[k] > arr[loc])
{
loc = k;
}
}
return loc;
}
Returns the index of the largest value in array arr.
This code sets loc to the middle of the array and then loops through all the array elements. If the value at the current index is greater than the value at loc then it changes loc to the current index. It returns loc, which is the index of the largest value in the array.
Returns the index of the first element in array arr whose value is greater than arr[loc].
This would be true if there was a return loc after loc = k in the if block.
Returns the index of the last element in array arr whose value is greater than arr[loc].
This would be true if it returned loc after setting loc = k and if it started at the end of the array and looped toward the beginning of the array.
Returns the largest value in array arr.
It returns the index to the largest value in array arr, not the largest value.
Returns the index of the largest value in the second half of array arr.
k loops from 0 to arr.length - 1. So it checks all of the elements in the array.
int[] a = {7, 3, -1};
public static int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}
4
This would be true if it was return (a[1] *= 2);, which would change the value at a[1].
2
The statement a[1]--; is the same as a[1] = a[1] - 1; so this will change the 3 to 2. The return (a[1] * 2) does not change the value at a[1].
12
This would be true if array indices started at 1 instead of 0 and if the code changed the value at index 1 to the current value times two.
6
This would be true if array indices started at 1 rather than 0.
3
This canβt be true because a[1]--; means the same as a[1] = a[1] - 1; so the 3 changes to 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.
for (int i = 1; i < k; i++)
{
if (arr[i] < someValue)
{
System.out.print("HELLO")
}
}
k - 1
This loop will start at 1 and continue until k is reached as long as arr[i] < someValue is true. The last time the loop executes, i will equal k-1, if the condition is always true. The number of times a loop executes is equal to the largest value when the loop executes minus the smallest value plus one. In this case that is (k - 1) - 1 + 1 which equals k - 1.
k + 1
This would be true if arr[i] < someValue was always true and the loop started at 0 instead of 1 and continued while it was less than or equal to k.
k
This would be true if arr[i] < someValue was always true and the loop started at 0 instead of 1.
1
This would be the case if only one element in the array would fulfill the condition that arr[i] < someValue.
0
This is the minimum number of times that HELLO could be executed. This would be true if k was less than i initially.
Consider the following method changeArray. An array is created that contains {2, 8, 10, 9, 6} and is passed to changeArray. What are the contents of the array after the changeArray method executes?
public static void changeArray(int[] data)
{
for (int k = data.length - 1; k > 0; k--)
data[k - 1] = data[k] + data[k - 1];
}
{2, 6, 2, -1, -3}
This would be correct if data[k] was modified in the for-loop. In this for-loop, data[k - 1] is the element that changes.
{-23, -21, -13, -3, 6}
This would be correct if data[k - 1] was subtracted from data[k]. Notice that for every instance of the for-loop, data[k] and data[k - 1] are added together and data[k - 1] is set to that value.
{10, 18, 19, 15, 6}
This would be correct if the for-loop began at 1 and continued to data.length - 1. Notice the for-loop indexing.
This method results in an IndexOutOfBounds exception.
The indexing of this method is correct. The for-loop begins at the last valid index and ends when k is equal to 0, and the method does not access any values other than the ones specified.
{35, 33, 25, 15, 6}
This method starts at the last valid index of the array and adds the value of the previous element to the element at index k - 1.
Given the following code segment, which of the following will cause an infinite loop? Assume that temp is an int variable initialized to be greater than zero and that a is an array of ints.
for ( int k = 0; k < a.length; k++ )
{
while ( a[ k ] < temp )
{
a[ k ] *= 2;
}
}
The values donβt matter this will always cause an infinite loop.
An infinite loop will not always occur in this code segment.
Whenever a includes a value that is less than or equal to zero.
When a contains a value that is less than or equal to zero then multiplying that value by 2 will never make the result larger than temp (which was set to some value > 0), so an infinite loop will occur.
Whenever a has values larger then temp.
Values larger then temp will not cause an infinite loop.
When all values in a are larger than temp.
Values larger then temp will not cause an infinite loop.
Whenever a includes a value equal to temp.
Values equal to temp will not cause the infinite loop.
You can step through the code above using the Java Visualizer by clicking on the following link Viz-10. Can you fix the code so that it wonβt result in an infinite loop?
Consider the following data field and incomplete method, partialSum, which is intended to return an integer array sum such that for all i, sum[i] is equal to arr[0] + arr[1] + ... + arr[i]. For instance, if arr contains the values {1, 4, 1, 3}, the array sum will contain the values {1, 5, 6, 9}. Which of the following is true about the two implementations of missing code on line 9 that are proposed?