COMPUTER SCIENCE TEST
ARRAYS
1.The code prints the first 10 terms of fibonacci series using arrays. Choose the appropriate
stmt.
[Hint A series of numbers in which each number (Fibonacci number) is the sum of the two
preceding numbers. The series is 0, 1, 1, 2, 3, 5, 8, etc.]
int a[]=new int[10];
a[0]=0;
a[1]=1;
for(int i=2;i<a.length;i++)
a[i]=_________ + ___________;
(a) a[i-1]+a[i-2]
(b) a[i-1]+a[i-1]
(c) a[i-2]+a[i-2]
2.Arrange the following in the descending order of number of bytes occupied.
(i) char x[40]
(ii) int a[4][4]
(iii)long b[20]
(a) (iii), (ii), (i)
(b) (iii), (i), (ii)
(c) (ii), (iii), (i)
(d) (i), (ii), (iii)
3. Sam performs bubble sort on the following array to organise the elements in ascending
order:
{5,6,2,3}
What would be the array after the two comparison?
(a) {5,2,6,3}
(b) {5,2,3,6}
(c) {5,5,6,3}
(d) {5,6,2,3}
4.Fill in the blanks:
import java.util.*;
class binarysearch
{
int numbers[] = {2, 4, 5, 11, 13, 20, 34, 39, 50, 52, 68, 74, 85, 92};
int low = 0;
int high = numbers.length - 1;
int mid = 0;
boolean found = false;
System.out.println("Enter the value you would like to search for: ");
Scanner keyboard = new Scanner(System.in);
int key = keyboard.nextInt();
while (low <= high && _____(1)________ )found==false
{
mid = _____(2)_______ (low+high)/2
if (key >_______(3)_______ ) numbers[mid]
{
low = mid + 1;
}
else if (key < numbers[mid] )
{
_____(4)_________high=mid-1;
}
else
{
System.out.println("The value " + key + " was found in position " + mid);
found = true;
}
}
}
}
5. This code is to improve the best case efficiency in bubble sort
boolean swapped = _______________; true
for(int j=arr.length-1; j>=0 && ________________swapped or swapped==true; j--)
{
swapped = false;
for(int k=0; k<j; k++)
{
if(arr[k] > _____________) arr[k+1]
{
int temp = arr[k];
arr[k] = ____________; arr[k+1]
arr[k+1] = temp;
swapped = _____________;true
}
}
}
6. The following code stores a highly negative element in the initial cell so that no element is
lesser than the first element.
int a[]={-999,7,8,5,2,4,6,3};
int i,j;
for(i=1;i<a.length;i++)
{
int t=a[i];
j=i-1;
while(___________________) a[j]>t
{
______________________ a[j+1]=a[j];
______________________j--;
}
___________________=t; a[j+1]=t;
}
7. Give the output of the following program code:
char arr[] = { 'a', 'b', 'c', 'A', 'B', 'C' };
char abb[ ] = arr;
System.out.println(Math.pow(arr.length, 3));
System.out.println(arr[0] = = arr[3]);
System.out.println(abb = = arr);
216.0
false
true
8. What will be the values stored in array ar[] after the following program is executed.
void main()
{
int i, j=1;
int dividend = 2;
int N=30;
int ar[] = {2,0,0,0,0,0,0,0,0,0);
while(dividend<=N)
{
for (int i=2; i<dividend; i++)
{
if (dividend%i==0)
break;
if(i==dividend-1)
ar[j++] = dividend;
}
dividend++;
}
}
2 3 5 7 11 13 17 19 23 29
9. Predict the output:
char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' };
System.out.println(arr);
(a) ‘j’
(b) Base address
(c) 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g'
(d) java programming
10. Assume int [ ] [ ] x = { {8,9,7,11,5} , {5,6,7},{1,2,3,4}}, what are x[0].length, x[1].length,
andx[2].length ? (a) 2, 3 and 5 (b) 5, 3 and 4(c) 5, 5 and 3(d) 2, 2 and 2