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
public static int[] p()
{ / / ... }
public static void main(String[] args)
{
int[] arr = p();
/ / ...
}
What return statement may be used in p() ?
return new int[3];
public static int mystery(int a[], int x) {
int c = 0;
for ( int i = 0; i < a.length; i++) {
if (a[i] == x)
{ c++;
} }
return c; }
Returns a count of the number of times x appears in the array.
private int[] nums;
/ ** print the indices of all the elements in nums which are even numbers
*/ public void printEvenIndices()
{ / missing code / }
Which of the following replacements for / missing code / correctly implements the method printEvenIndices()
for (int k = 0; k < nums.length; k++)
{
if (nums[k] % 2 == 0)
{
System.out.println(k);
}
}
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 = {"aardvark", "banana", "cougar", "daikon", "elephant", "fog", "get"};
System.out.println(mystery(b, 5));
5
When you pass an array to a method, the method receives ______.
a copy of the reference to the array
public static void mystery(int[] nums)
{
for (int i = 0; i < nums.length; i++)
{
if (nums[i] % 2 != 0)
{
nums[i]++;
}
}
}
Changes all the values in the array to even numbers.
public static String mashup(String str, int[] arr)
{
String result = "";
for (int x : arr)
{
result = result + str.substring(0, x);
}
return result;
}
The following code appears in another method in the same class.
int[] nums = {1, 5, 3};
String word = "program";
System.out.println(mashup(word, nums));
pprogrpro
public static boolean mystery(int nums[])
{
for (int i = 1; i < nums.length; i++)
{
if (nums[i - 1] >= nums[i])
{
return false;
}
}
return true;
}
Returns true if each element of the array is greater than the element before.
int[] seq = {0, 1, 2, 5, 3, 7};
for (int k = 1; k < seq.length; k++)
{
seq[k] = seq[k-1];
}
Which of the following represents the contents of seq as a result of executing the code segment?
{0, 0, 0, 0, 0, 0}
Consider the following methods which appear within the same class.
public static void mystery(int[] data)
{
for (int k = 0; k < data.length - 1; k+=2)
{
int t = data[k];
data[k] = data[k+1];
data[k + 1] = t;
}
}
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 = {1, 2, 7, 3, 5};
mystery(nums);
System.out.println(toString(nums));
2 1 3 7 5
public static void doStuff (int a[], int b)
{
if (/ Missing Code /)
{
for (int i = b; i < a.length; i++)
{
a[i] = a[i] * 2;
}
}
}
b >= 0 && b < a.length
int a [] = {64 , 66 , 67 , 37 , 73 , 70 , 95 , 52 , 81 , 82};
for (int i = 0; i < a.length; i++)
{
a[i] = a[i] / 10;
}
for (int i =0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
What is output?
6 6 6 3 7 7 9 5 8 8
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(10) returns a value of 7. Which of the following MUST be true about nums?
The elements at indices from 0 to 6 inclusive in nums are all less than 10.
int[] seq = {3, 1, 8, 4, 2, 5};
for (int k = 1; k < seq.length; k++)
{
if (seq[k] >= seq[0])
{
System.out.print(seq[k] + " " + k + " ");
}
}
What is output?
8 2 4 3 5 5
public static 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 = {34, 18, 34, 38, 27, 37, 39, 21, 19}
18
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 >= 0; k--)
{
if (nums[k] == nums[n])
{
result = k;
}
}
return result;
}
The index of the first element in the array which has the same value as the element at position n.
Consider the following instance variable and method.
private String[] words;
public void mystery(int n)
{
for (int k = n; k < words.length; k += n)
{
words[k] = words[k - 1].substring(0, n);
}
}
Assume that words has been initialized with the following values.
{"abcde", "fghij", "klmno", "pqrst", "uvwxy"}
Which of the following represents the contents of the array words as a result of the call mystery(2)?
{"abcde", "fghij", "fg", "pqrst", "pq"}
public static double average(int[] nums)
{
int sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return (1.0 * sum) / nums.length;
} //average
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
What is output by running the following?
String[] spelling = {"against", "forms", "belief", "government", "democratic",
"movement", "understanding", "single", "followed", "scenario"};
System.out.println( average( mystery(spelling)));
8.1
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;
public static int mystery(int[] data)
{
int times = 0;
int counter = 0;
for (int j = 0; j < data.length; j++)
{
counter = 0;
for (int k = j; k < data.length; k++)
{
if (data[j] == data[k])
{
counter ++;
}
}
if (counter > times)
{
times = counter;
}
}
return times;
}
Assume that data has been declared and initialized as an array of integer values. Which of the following best
describes the value returned by the call mystery(data)?
The number of times that a most frequently occurring value appears in data.