Find the return type and number of local variables for the following method
public double f(int n, int p) { double sumOfPowers = Math.pow(n,p) + Math.pow(p,n); return sumOfPowers; }
double
; there is only one local variable (sumOfPowers
)
What does the following print?
public class Main { public static void main(String[] args) { boolean[] flags = new boolean[6]; int[] nums = new int[6]; for (int i = 0; i < 6; i++) { if (i < 4) { nums[i] = i; flags[i] = (i % 3 == 1); } System.out.println(nums[i] + " " + flags[i]); } } }
0 false 1 true 2 false 3 false 0 false 0 false
What does the following code print?
public class Utility { public static double[] normalize(double[] v) { double mag = 0; for (int i = 0; i < v.length; i++) { mag += v[i]*v[i]; } mag = Math.sqrt(mag); double[] normalizedV = new double[v.length]; for (int i = 0; i < v.length; i++) { normalizedV[i] = v[i] / mag; } return normalizedV; } public static void main(String[] args) { double[] v = {4,0,3}; double[] u = normalize(v); System.out.println("(" + u[0] + "," + u[1] + "," + u[2] + ")"); } }
(0.8,0.0,0.6)
What does the following code print?
public class Main { public static void main(String[] args) { int[] a = {2,3,6,8,5,9,1,7}; int p = a[0]; for (int i=0; i < a.length; i++) { if (p < a[i]) { p = a[i]; } } System.out.println(p); } }
9
Suppose one uses a binary search to determine if 4 is present in the following array {1, 2, 3, 5, 7, 9, 10, 12, 15, 18, 19, 23}
. Give the values taken on by the high
, low
, and mid
variables, in the order they occur in the code below, as well as the return value for the method.
public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key > list[mid]) low = mid + 1; else return mid; } return -1 - low; }
lo mid hi 0 5 11 3 2 4 3 2 returns -4
The selection sort orders list or array of numbers (or some other comparable type) by performing a number of "swaps". Given the list: 6,1,9,7,5,4,3,2 -- put the list in order using a selection sort. Show the state of the list after each "swap", from the first swap made to the last, in that order.
6 1 9 7 5 4 3 2 1 6 9 7 5 4 3 2 1 2 9 7 5 4 3 6 1 2 3 7 5 4 9 6 1 2 3 4 5 7 9 6 1 2 3 4 5 7 9 6 1 2 3 4 5 6 9 7 1 2 3 4 5 6 7 9
The insertion sort orders a list or array of numbers (or some other comparable type) by performing a number of "insertions". Given the list: 6,1,9,7,5,4,3,2 -- put the list in order using an insertion sort. Show the state of the list after each "insertion", from the first insertion made to the last, in that order.
6 1 9 7 5 4 3 2 1 6 9 7 5 4 3 2 1 6 9 7 5 4 3 2 1 6 7 9 5 4 3 2 1 5 6 7 9 4 3 2 1 4 5 6 7 9 3 2 1 3 4 5 6 7 9 2 1 2 3 4 5 6 7 9
What does the following code print? Also, the array a
below is called a __________, given that the arrays it contains are of different lengths? (Fill in the blank)
public class Main { public static void main(String[] args) { int[][] a = {{1,2,3,4},{5,6},{7,8,9}}; int[] b = {10,11,12,13}; int[] c = {14,15,16,17}; System.out.println(a.length + " " + a[0].length + " " + a[1].length); b = c; b[0] = 8; for (int i = 0; i < b.length; i++) { System.out.print(c[i] + " "); } } }
3 4 2 8 15 16 17The array
a
is called a "ragged array"
When two methods share the same name but have different method signatures one has an example of ________ (Fill in the blank)
What does the following code print?
public class CursingAgain { public static int bang(int n) { return (n == 1 ? 1 : n * bang(n-1)); } public static void main(String[] args) { System.out.println(bang(5)); } }
120
What does the following code print?
public class Main { public static void main(String[] args) { char[][] myChars = {{'a','b','c'}, {'d','e','f'}, {'g','h','i'}}; for (int i = 0; i < 3; i++) { for (int j = i; j >=0; j--) { System.out.print(myChars[i][j]); } System.out.println(); } } }
a ed ihg
What does the following code print?
public class Main { public static void main(String[] args) { int[][] z = new int[3][3]; int[] r1 = {4,9,3}; int[] r2 = {6,5}; int[] r3 = {1,7,8}; int[][] x = {r1,r2,r3}; for (int i = 0; i < 3; i++) { z[i][0] = x[i][1]; } for (int i = 0; i < 3; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + z[i][j] + " "); } System.out.println(); } } }
13 9 3 11 5 8 7 8
What does the following code print?
public class MethodFun { public static void fourInTheGolfSense(int[] x, int[] y, int[] z, int d) { for (int i=0; i < x.length; i++) { z[i] = x[i] + y[i]; } int[] t = x; x = y; y = t; d = 0; for (int i=0; i < x.length; i++) { System.out.print(x[i]); } } public static void main(String[] args) { int[] a = {1,2,3}; int[] b = {4,5,6}; int[] c = {7,8,9}; int d = 1; fourInTheGolfSense(a,b,c,d); for (int i=0; i < a.length; i++) { System.out.print(a[i]); } for (int i=0; i < c.length; i++) { System.out.print(c[i]); } System.out.print(d); } }
4561235791
Explain the error in the below code.
public boolean bueno(double p) { double b = p; for (int y = 0; y < 7; y++) { p *= 1.10; boolean nuff = (b > 2*p); } return nuff; }
nuff
is out of scope
Complete the following method so that it will return the number of occurrences of the
given integer x in the given integer array a
.
public static int countOccurrences(int x, int[] a) { //////////////////// // YOUR CODE HERE // //////////////////// }
public static void countOccurrences(int x, int[] a) { int count = 0; for (int i = 0; i < a.length; i++) { if (a[i] == x) { count++; } } return count; }