In: Computer Science
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 of code executes?
int[] numbers = new int[5];
4.What would be the results after the following code was executed?
int[] x = {23, 55, 83, 19};
int[] y = {36, 78, 12, 24};
x = y;
y = x;
5.In order to do a binary search on an array,
Question 5 options:
a. the values of the array must be numeric.
b. the array must first be sorted in ascending order.
c. you must first do a sequential search of the array to assure the element you are looking for is there.
d. there are no requirements.
6. The ______ search repeatedly divides the portion of an array being searched in half.
7.What is the value of x after the following statements execute?
Int[] numbers = { 10, 20, 30, 40, 50 };
int x = numbers[2] + numbers[3];
8. The _________ search steps through an array, comparing each value to the search value.
9. What will be the value of x[1] after the following code is executed?
int[] numbers = {22, 33, 44};
arrayProcess(numbers[1]);
...
public static void arrayProcess(int a)
{
a = a + 5;
}
10.The sequential search algorithm
Question 10 options:
a. requires the array to be ordered.
b. must always be implemented as a method.
c. will not execute, if the element is not in the array.
d. uses a loop to sequentially step through an array, starting with the first element.
SOLUTION 1.>
int [] numbers={22,33,44};
for(int k=0;k<3;k++)
{
numbers[k]=numbsers[k]+5;
}
output:->
this code will increase the value of each element
of array by 5 so new array will be like numbers[]={27,38,49};
numbers[]={27,38,49}
hence numbers[1] is 38
CORRECT ANSWER IS : 38
SOLUTION 2.>
ARRAY_SIZE=5;
double[]x=new double[ARRAY_SIZE];
for(int i=1;i<=ARRAY_SIZE;i++)
{
x[i]=10.0;
}
OUTPUT:
All element of X array will become 10.0
hence array will look like this X[]={10.0,10.0,10.0,10.0,10.0};
SOLUTION 3.>
value of numbers[3] will be garbage values because
array is not initialized with any values
SOLUTION 4.>
on assigning (x=y)
x become y so x={36, 78, 12, 24};
and y become x then y={36, 78, 12, 24};
so correct answer is both become same and equal to y
x={36, 78, 12, 24};
y={36, 78, 12, 24};
SOLUTION 5.>
In order to do binar search on array array must be sorted
becaues binary search uses mid to reduce the search space after every
iteration
so correct option is :
[ b. the array must first be sorted in ascending order.]
SOLUTION 6.>
THE _BINARY_SEARCH _ search repeatedly divides the portion of an array being
searched in half.
CORRECT ANSWER IS : BINARY SEARCH
EXPLAINATION :
binary search first find mid then it compare x with value at mid
if(x>a[mid])
then goes to right half for search
else
it goes to left part for searching .
SOLUTION 7.>
int[] numbers = { 10, 20, 30, 40, 50 };
int x = numbers[2] + numbers[3]
numbers[2]=30;
numbers[3]=40;
hence (numbers[2] + numbers[3]) = 70
so x=70
correct answer: 70
SOLUTION 8.>
CORRECT ANSWER : The __LINEAR__ search steps through an array, comparing each value
to the search value.
explaination:
The linear search check each values of array and compre with x
if matches then return result.
eg:
int search(int x,int arr[],int n)
{
for(int i=0;i<n;i++)
{
if(arr[i]==x)
return(i);
}
}
-->
this is linear search return once value of arr[i] matches with x
and return index at which x present at array
SOLUTION 9.>
CORRECT ANSWER IS :38
EXPLAINATION:
int[] numbers = {22, 33, 44};
numbers[1]=33 because value at index in array is 33
now we are passing it in arrayProcess(numbers[1])
so 33 is passed in arrayProcess(33) function and this function
increase values by 5 since (a=a+5 inside function )
so correct result is 33+5 becomes 38
SOLUTIO 10.>
CORRECT OPTION IS (d)
EXPLAINATION:
The sequential search algorithm ->
d. uses a loop to sequentially step through an array, starting with the first element.