Question

In: Computer Science

1) What's the contents of array vals after the following code is executed? int[] vals =...

1)

What's the contents of array vals after the following code is executed?

int[] vals = {6, 1, 4, 5, 1};
int size = vals.length;
for(int i = 0; i < size; i++)
  vals[i] = vals[i] * i;

q) [ 6, 1, 4, 5, 1 ]

b)[ 0, 1, 2, 3, 4]

c)[0, 1, 8, 15, 4]

d)[6, 2, 12, 20, 5]

2)

Consider function foo defined below:

static int[] foo(int vals[]) {
   int result[] = new int[vals.length - 1];
   for(int i = 0; i < vals.length - 1; i++)
     result[i] = Math.abs(vals[i] - vals[i+1]);
   return result;
}

If nums is a static array with the content [6, 1, 4, 5, 1], what's the result of calling foo(nums)?

a) nothing gets returned from function foo

b) [5, 3, 1, 4]

c) [5, -3, -1, 4]

d) [-5, 3, 1, -4]

3)

The method has below returns true/false depending whether target is found in vals. What's the correct way to complete the following code so that the method has does what it is intended to do?

static boolean has(int vals[], int target) {
    for(int i = 0; i < vals.length; i++)
        if (vals[i] == __________)
            return true;
    return __________;
}

a)target and false

b) i and false

c) target and true

d) i and true

Solutions

Expert Solution

Question 1

Answer is option c) [0, 1, 8, 15, 4]

Explanation

==========

Iteration 1

int[] vals = {6, 1, 4, 5, 1};
int size = vals.length;  get size of the array That is 5; size = 5
for(int i = 0; i < size; i++)  i<size 0<5 True
  vals[i] = vals[i] * i;    vals[0] = vals[0]*0 ===>  vals[0] = 6*0 vals[0] = 0

i = i+1 ====> i = 0+1 ====> i=1

Iteration 2

for( i < size; i++) i<size 1<5 True

vals[i] = vals[i] * i; vals[1] = vals[1]*1 ===> vals[1] = 1*1 ===> vals[1] = 1

i = i+1 ====> i = 1+1 ====> i=2

Iteration 3

for( i < size; i++) i<size 2<5 True

vals[i] = vals[i] * i; vals[2] = vals[2]*2===> vals[2] = 4*2 ===> vals[2] = 8

i = i+1 ====> i = 2+1 ====> i=3

Iteration 4

for( i < size; i++) i<size 3<5 True

vals[i] = vals[i] * i; vals[3] = vals[3]*3===> vals[3] = 5*3 ===> vals[3] = 15

i = i+1 ====> i = 3+1 ====> i=4

Iteration 5

for( i < size; i++) i<size 4<5 True

vals[i] = vals[i] * i; vals[4] = vals[4]*4===> vals[4] = 1*4 ===> vals[4] = 4

i = i+1 ====> i = 4+1 ====> i=5

Iteration 6

for( i < size; i++) i<size 5<5 False Exit from for loop

vals[0] = 0

vals[1] = 1

vals[2] = 8

vals[3] = 15

vals[4] = 4

Question 2

Answer is option b) [5, 3, 1, 4]

Explanation

static int[] foo(int vals[]) {
      vals[] = [6, 1, 4, 5, 1]
   int result[] = new int[vals.length - 1];
   for(int i = 0; i < vals.length - 1; i++)
     result[i] = Math.abs(vals[i] - vals[i+1]);
   return result;
}

Iteration 1

vals[] = [6, 1, 4, 5, 1]

for(int i = 0; i < vals.length - 1; i++) 0<4 True

result[i] = Math.abs(vals[i0 - vals[0+1]);   result[0] = Math.abs(6 - 1);   result[0] = 5

i = i+1 ===> i = 0+1 i =1

Iteration 2

vals[] = [6, 1, 4, 5, 1]

for( i < vals.length - 1; i++) 1<4 True

result[i] = Math.abs(vals[1] - vals[1+1]);   result[1] = Math.abs(1 - 4);  Math.abs(-3); result[1] = 3

i = i+1 ===> i = 1+1 ===> i =2

Iteration 3

vals[] = [6, 1, 4, 5, 1]

for( i < vals.length - 1; i++) 2<4 True

result[i] = Math.abs(vals[2] - vals[2+1]);   result[2] = Math.abs(4- 5);  Math.abs(-1); result[2] = 1

i = i+1 ===> i = 2+1 ===> i=3

Iteration 4

vals[] = [6, 1, 4, 5, 1]

for( i < vals.length - 1; i++) 3<4 True

result[i] = Math.abs(vals[3] - vals[3+1]);   result[3] = Math.abs(5- 1);  Math.abs(4); result[3] = 4

i = i+1 ===> i = 3+1 ===> i=4

Iteration 5

for( i < vals.length - 1; i++) 4<4 True False Exit from for loop

result[0] = 5

result[1] = 3

result[2] = 1

result[3] = 4

Question 3

Answer is option a) target and false

tatic boolean has(int vals[], int target) {
    for(int i = 0; i < vals.length; i++)
        if (vals[i] == target) found the target value in the array return true
            return true;
    return false; target Value NOT found in the array return false
}

NOTE: If you have any doubt, please let me know through comments; I will surely revert back to you.

Please give a up vote .....
Thank you...


Related Solutions

What are the contents of the array after the for-loop in the following code?
(IN C)What are the contents of the array after the for-loop in the following code?int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },{ 1, 2, 3, 4, 5 },{ 3, 6, 7, 8, 9 },{ 2, 3, 4, 5, 6 },{ 5, 6, 7, 8, 9 } };int i;int *ptr = array[ 0 ];for( i = 0; i < SIZE * SIZE; i++ ) {if( i % SIZE < 2 ) {*( ptr +...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
Show the contents of the array after the fourth iteration of selectionSort Your answer should be...
Show the contents of the array after the fourth iteration of selectionSort Your answer should be 4 lines The array values horizontally for iteration 1 The array values horizontally for iteration 2 The array values horizontally for iteration 3 The array values horizontally for iteration 4
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
1 Draw a stack diagram to show how the following code is executed and write the...
1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10)) 2 Draw a stack diagram to show how the following code is executed...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT