In: Computer Science
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
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...