1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What return statement may be used in the method s()?
public static String[] s()
{
// ...
}
public static void main(String[] args)
{
String[] words = s();
// ...
}
return new String[] {"apple", "banana", "orange"};
This is correct because this is the proper way to call a String array and it is what the method is asking for.
What does the following method do?
public static int mystery(double[] a,double x)
{
int s = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] != x)
{
s = s + a[i] ;
}
}
return s;
}
Returns the sum of all elements other than x in the array.
This is correct because s only adds in a[i] if a[i] does not equal x.
Consider the following instance variable and method.
private int[] nums;
/* print the elements in nums which are a multiple of three /
public void printMultiplesOfThree()
{
/ missing code /
}
Which of the following replacements for / missing code / correctly implements the method printMultiplesOfThree()?
for (int x : nums)
{
if (x % 3 == 0)
{
System.out.println(x);
}
}
This is correct as we are looking to see if the element inside nums is a multiple of 3.
Assume the following method has been defined:
public static int mystery(String[] a, int x)
{
int c = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i].length() <= x)
{
c++;
}
}
return c;
}
What is output by the following code?
String[] b = {"rain", "beach", "any", "love", "emotion", "sunny", "go"};
System.out.println(mystery(b, 4));
4
This is correct because only 4 of the values have a length that is less or equal to 4.
If you want to use an array as a parameter in a method, in the method declaration, you must first include ________.
data type of the array
This is correct as you must identify what type the array is first before putting in an array as an argument.
What does the following algorithm do?
public static void mystery(int[] nums)
{
for (int i = 0; i < nums.length; i++)
{
if (nums[i] % 5 == 0)
{
nums[i]++;
}
}
}
Changes all values to not be a multiple of 5.
This is correct as the elements only add by 1 if it was originally a multiple of 5 hence not becoming a multiple of 5 thereafter.
Consider the following method
public static String combineCertainWords(String[] words, int start, int end)
{
String result = "";
for (String str : words)
{
result = result + str.substring(start, end);
}
return result;
}
The following code appears in another method in the same class.
String[] animals = {"bunny", "bird", "dog"};
System.out.println(combineCertainWords(animals, 2, 3));
What is printed when the code above is executed?
nrg
This is correct because the substring method first takes the starting index starting from 0 and the ending index which starts from 1 so it should only return one letter per word if the substring takes in (2,3) as the starting and ending index.
What does the following algorithm do?
public static boolean mystery(int[] nums)
{
for (int i = 1; i < nums.length; i++)
{
if (nums[i] < nums[i - 1])
{
return false;
}
}
return true;
}
Returns true if each element of the array is less than or equal to the element after.
Consider the following code segment.
int[] seq = {0, 18, 9, 6, 3, 1};
for (int k = 1; k < seq.length - 1; k++)
{
seq[k] = seq[k + 1];
}
Which of the following represents the contents of seq as a result of executing the code segment?
{0, 9, 6, 3, 1, 1}
This is correct as the only value that gets replaced or taken out in this case would be the second value as the for loop in this case meant to happen incrementally.
Consider the following methods which appear within the same class.
public static void mystery(int[] data)
{
for (int k = data.length; k > 0; k--)
{
data[k - 1] = k;
}
}
public static String toString(int[] data)
{
String str = "";
for (int d : data)
{
str = str + d + " ";
}
return str;
}
The following code segment appears in the main method of the same class.
int[] nums = {2, 4, 6, 8, 10};
mystery(nums);
System.out.println(toString(nums));
What is printed as a result of executing the code segment?
1 2 3 4 5
This is correct because every value in the array gets replaced by the counter increment.
Consider the following method:
public static void doStuff(int[] arr, int num)
{
if (/ Missing Code /)
{
for (int i = num; i < arr.length; i++)
{
arr[i] = arr[i] % 4;
}
}
}
What could be used to replace / Missing Code / so that there is no out of bounds exception?
num >= 0 && num < arr.length
Correct; Variable "n" is serving as an index for array "a." So to prevent an out of bounds exception, n must be a valid index for "a." Remember that arrays start with a zero index. So the first element would be 0, and the last would be a.length - 1.
Consider the following code segment:
double [] a = {2, 2, 2, 2, 2};
for (int i = 0; i < a.length; i++)
{
a[i] = Math.pow(a[i], i);
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
What is output?
1.0 2.0 4.0 8.0 16.0
This is correct as all the values in the array match up to the sequence of the index.
Consider the following instance variable and method.
private int[] nums;
/** @param val any int value
*/
public int mystery(int val)
{
int k = 0;
while (k < nums.length && nums[k] < val)
{
k++;
}
return k;
}
Suppose that the call mystery(8) returns a value of 5. Which of the following MUST be true about nums?
The elements at indices from 0 to 4 inclusive in nums are all less than 8.
This is correct because if the elements were 8 or greater the while loop have exited and returned those values instead.
Consider the following code segment.
int[] seq = {5, 4, 2, 8, 6, 5};
for (int k = seq.length - 1; k >= 0; k--)
{
if (seq[k] <= seq[0])
{
System.out.print(seq[k] + " " );
}
}
What will be printed as a result of executing the code segment?
5 2 4 5
This is correct as the for loop happens to work in reverse so it prints the values in reverse order.
Given the following method definition:
public static int mystery(int[] a)
{
int m = a[0];
for (int i = 0; i < a.length; i++)
{
if (m < a[i])
{
m = a[i];
}
}
return m;
}
What would be returned by mystery if it was passed the following array?
int[] a = {5, 3, 6, 8, 9};
9
This is correct because we are looking the maximum value within the array.
Consider the following instance variable and method.
private int[] nums;
/** Precondition: nums.length > 0
* @param n an int value representing a valid index of nums
*/
public int numsInfo(int n)
{
int result = n;
for (int k = n + 1; k < nums.length; k++)
{
if (nums[k] == nums[n])
{
result = k;
}
}
return result;
}
Which of the following best describes the value returned by the method numsInfo?
The index of the last element in the array which has the same value as the element at position n.
This is correct. The last appearance of the element at position n must obviously not be before position n since it definitely appears at position n. This is therefore the initial value which result is set to. The for loop iterates through each element of the array after this position, starting at the element at position n + 1 and if it is equal to the value of the element at position n, updates result to the relevant index. This continues until the end of the array is reached: at this point result is returned, and must be equal to the most recent index (i.e. the nearest to the end of the array) of an element with the same value as the element at index n.
Consider the following instance variable and method.
private String[] words;
public void mystery(int n)
{
for (int k = n; k < words.length - 1; k++)
{
words[k] = words[k + 1].substring(0, n);
}
}
Assume that words has been initialized with the following values.
{"dragon", "ogre", "troll", "goblin", "knight"}
Which of the following represents the contents of the array words as a result of the call mystery(1)?
{"dragon", "t", "g", "k", "knight"}
This is correct as int k starts from 1 so the value it starts from would be second value as it becomes converted to the third so ogre to troll in this case as the for loop returns words[k] to words[k + 1] so it starts from troll and ends to knight and the substring subtracts only first letter as indicated by substring(0,1). Since the for loop starts at index 1 and ends at index words.length - 2,, the first and last values should be unchanged.
Consider the following methods:
public static int sum(int[] nums)
{
int sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum;
} //sum
public static int[] mystery(String[] a)
{
int[] temp = new int[a.length];
for (int i = 0; i < a.length; i++)
{
temp[i] = a[i].length();
}
return temp;
} //mystery
public static void main(String[] args)
{
String[] spelling = {"boat", "bridge", "hat", "house"};
System.out.println(sum(mystery(spelling)));
}
Which of the following will be printed by the main method of the program?
18
This is correct as mystery function converts all string values in an array into the amount of length they have which then gets added up in the sum function.
Consider the following method which is intended to create an array which stores the decimal digits of the positive int num in order of their place value (e.g. units first, then tens etc.).
/** @param num a positive integer
* @return an array containing each digit of num, with the units digit at index 0
and
* higher place value digits at higher indices.
*/
public int[] digits(int num)
{
int length = 0;
int pv = 1;
while (num >= pv)
{
length += 1;
pv *= 10;
}
int[] dig = new int[length];
for (int k = 0; k < length; k++)
{
/ missing code /
}
return dig;
}
Which of the following could replace / missing code / so the method digits works as intended?
dig[k] = num % 10;
num /= 10;
This is correct. The expression num%10 returns the remainder when num is divided by 10: this will be the units value of whatever is the current value of num. On each iteration, num is then divided by 10 (using integer floor division), and this causes the place value of each digit to decrease by 1 (e.g. tens become units, hundreds become tens). On the next iteration therefore the next digit of nums will be added to the array until it contains each digit of the number.
Consider the following class.
public static int mystery(int[] data)
{
int times = 0;
int counter = data.length;
for (int j = 0; j < data.length; j++)
{
counter = 0;
for (int e : data)
{
if (e == data[j])
{
counter++;
}
}
times = times + counter;
}
System.out.println(times);
return times;
}
public static void main(String[] args)
{
int [] arr = {1, 2, 2, 4, 4};
mystery(arr);
}
What will be printed as a result of executing the code segment?
9
This is correct as the function adds the length of the array and adds how many times a value appear so we know 2 and 4 appears twice so thats 4 + 5 which is 9.
Loop 1:
e = 1 because first number in the array is 1.
e = 1 == data[0] so 1 ==1 which is true, so add one to counter.
Counter now equals 5 +1 = 6
Loop 2:
e = 2 because second number in array is 2.
2 == data[1] so 2==2 which is true, so add one to counter again.
Counter now equals 6 + 1 = 7
Loop 3:
e = 2 because third number in the array is 2.
2 == data[2] so 2==2 which is true, so add one to counter again.
Counter now equals 7 + 1 = 8
Loop 4:
e = 4 because fourth number in the array is 4.
4 == data[3] so 4==4 which is true, so add one to counter for the final time before the loop terminates.
Counter finally equals 8 + 1 = 9