1)
We have a loop from i=10 to i < 30
And we're incrementing +3 every time
So, possible values of i are
10, 13, 16, 19, 22, 25, 28
We're adding i to values ArrayList if it gives 1 when we module with 5
So only 16 gives 1 when we module it with 5
So the answer is 16 which nums() method returns
2)
Initialized array with 3 elements
in the first for loop
for k=0 to k<A.length
A[k] = A.length-k;
means A[0] = 3-0 =3
A[1] = 3-1 = 2;
A[2] = 3-2 = 1;
A = [3,2,1];
In the second loop,
for k=0 to k<A.length-1
A[k+1] = A[k];
A[0+1] = A[0] = 3;
A[1+1] = A[1] = 3;
so, A = [3, 3, 3];
In the third loop we're printing array values
So the answer is 3 3 3
3)
We have arr ={1,2,3,4}
we have a loop
for k=1;k<arr.length-1;k++
arr[k] = arr[k+1]
arr[1] = arr[1+1] = 3
arr[2] = arr[2+1] = 4
arr = [1,3,4,4]
Answer is 1 3 4 4
4)
Here we have array a = {2,2,2,2}
mul = 1
In the loop we're taking each element and multiplying it with mul
So in first iteration mul = 1*2 = 2
second iteration mul = 2*2 = 4
third iteration mul = 4*2 = 8
fourth iteration mul = 8*2 =16
Answer is 16
Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30;...
Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...
Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...
Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new String(arr.length]; for (int j - 0; j < arr.length; j++) String sm = arr[i]; for (int k = j + 1; k < arr.length; k++) if (arr[k].length() < sm.length) sm - arr[k]; // Line 12 result[j] = SM; return result; Consider the following code segment. String[] test Two - {"last", "day" of "the", school","year"); String[] resultTrostrar Method(test Two) How many times is the line...
Consider the following code segment. ArrayList<String> towns = new ArrayList<String> (); towns.add("Baltimore"); towns.add("Bethesda"); towns.add("Wheaton"); towns.add("Greenbelt"); towns.add("Rockville"); for (int k = 1; k< towns.size(); k ++) System.out.print(towns.get(k) + " "); What is printed as a result of executing the code segment? Baltimore Bethesda Wheaton Greenbelt IndexOutOfBoundsException message Baltimore Bethesda Wheaton Greenbelt Rockville Bethesda Wheaton Greenbelt Rockville Refer to the doSomething method: public static void doSomething (ArrayList list, int i, intj) SomeType temp = list.get(i); list.set(i, list.get()); list.set(j, temp); Which best describes...
Consider the following C++ code segment: for (int i = 0; i <n; ++i) { for (int j = 0; j <m; ++j) if (i != j) cout << "0"; else cout << "1"; } } Which of the options below gives the correct output if the value of nis 2and the value of mis 3? 1. 100010 2. 011101 3. 100100 4. 010001
public class F2{ public static int foo(ArrayList<Integer> a) { int sum = 0; for(int i = 0; i <a.size(); i++){ if(i % 3 == 1) sum += a.get(i); else if(i % 3 == 2) sum -= a.get(i); else sum++; } return sum; } } What do the following method calls return? 1. foo(new ArrayList<>(Arrays.asList(1,2,3,4))) 2. foo(new ArrayList<>()) 3. foo(new ArrayList<>(Arrays.asList(1,2,-3,4325,-2))) 4. foo(new ArrayList<>(Arrays.asList(0,0,0)))
in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...
QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++) { //outer int index = i; for (int j = i + 1; j < arr.length; i++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; }//for i In the array= 16 13 15 14 19 24 9 3, the index of the smallest number at the outer iteration 4 = Answer
This implements a static void stack_push(Stack<Integer> stack) for(int i = 0; i < 5; i++) stack.push(i); LinkedList ArrayList Queue Stack
what is the output of the following code segment? C++ g. int arr[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) arr[i][j] =i*4 + j; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cout << arr[i][j] << " "; h. int next(int & x) { x= x + 1; return (x + 1); int main() { int y = 10;...