Array

Section A

2005

Question 1
(c) Which element is num[9] of the array num? [3]

10th

2006

Question 3
(b) Find and correct the errors in the following program segment:-
int n[ ] = (2,4,6,8,10);
for (int i = 0; i < = 5; i++)
System.out.println("n[" + i + "]=" + n[i]); [2]

int n[] = {2,4,6,8,10};
for (int i = 0; i < 5; i++)
System.out.println("n[" + i + "]=" + n[i ]);

The array initialization needs { } not ().
The test expression should be less than size-1 because index starts from 0. So the last index is 4 of an array of size 5.

2007

Question 1
(c) Differentiate between Binary Search and Linear Search. [2]

In binary search, the search can be done only on sorted array; but in linear search search can be done in sorted or un-sorted array.
In binary search the search is done very quickly because the search starts from middle of the array. In linear search the search speed is low beacuse the search starts from the first cell and done it one by one.

2008

Question 3
(f) If array [] = {1,9,8,5,2};
(i) What is array.length()?
(ii) What is array[2]? [2]
Note: There is a question mistake in array.length(), no need of (). Neglect it.

(i) 5
(ii) 8

2009

Question 2
(d) Name the keyword that:
(i) is used for allocating memory to an array. [1]

(d) (i) new

Question 3
(d) Given that int x[ ]={2,4,6,3,5,7};
What will be the value of x[1] and x[5]? [2]

4 and 7

2010

Question 2
(d) Write the difference between length and length() functions. [2]

The length is a variable that stores the length of an array, that means the number of cells in an array.
The length() is a String class method that finds the length of a string, i.e., the number of characters in a string.

2011

Question 2
(a) State the total size in bytes of the arrays a[4] of char data type and p[4] of float data type. [2]

char a[4] : 8. (4x2)
float p[4] : 16. (4x4)

(i) Write one difference between Linear search and binary search [2]

In linear search, search can be done in sorted or un-sorted array, but in binary search, the search can be done only on sorted array.
In linear search the execution speed is very low because the search is done one by one sequentially; but in binary search, the search is done speedily because the search starts from middle of the array.

2012

Question 1
(c) What is an array? Write a statement to declare to create an integer array of 10 elements. [2]

An array is a group of predetermined contiguous memory locations for same type data that are referenced by a common name but differentiated with some integer subscripts.
Array creation statement: int a[]=new int[10];

(d) Name the search or sort algorithm that:
(i) Makes several passes through the array, selecting next smallest item in the array each time and placing it where it belongs in the array.
(ii) At each stage compares the sought key value with key value of middle element of the array. [2]

(i) Selection sort – ascending order
(ii) Binary search

2013

Question 2
(c) Write statements to show how finding the length of character array char[ ] differs from finding the length of a String object str. [2]

int l = a.length - To find the length of a character array (assumes that name of the array is a)
int n = str.length() – To find the length of the string object str.

Question 3
(g) If int n[]={1,2,3,5,7,9,13,16}, what are the values of x and y?
x=Math.pow(n[4],n[2]);
y=Math.sqrt(n[5]+n[7]); [2]

x=343.0
y=5.0

2014

Question 1
(e) Find the errors in the given program segment and re-write the statements to assign values to an integer array.
int a = new int(5);
for(int i=0;i<=5;i++) a[i]=i; [2]

int a[] = new int[5];
for(int i=0;i<5;i++) a[i]=i;

2015

Question 7
(f) What will be the code print?
int arr[]=new int[5];
System.out.println(arr);

(i) 0
(ii) value stored in arr[0]
(iii) 0000
(iv) garbage value [2]

(iv) garbage value

2016

Question 1
(e) If int x[]={4,3,7,8,9,10}; what are the values of p and q?
(i) p=x.length
(ii) q=x[2]+x[5]*x[1] [2]

(i) p=6
(i) q=37

2017

Question 1
(e) String x[ ]= {“SAMSUNG”, “NOKIA”, “SONY”, “MICROMAX”, “BLACKBERRY”};
Give the output of the following statements:
System.out.println(x[1]);
System.out.println(x[3].length()); [2]

(i) NOKIA
(ii) 8

2018

Question 1
(b) Differentiate between the searching and sorting. [2]

Searching: Finding an element in an arrray is known as searching. If an element is found the result is displayed with its position etc. If it is not found “Element is not found message is displayed”.
Sorting: Arranging element in ascending or descending order is known as sorting. If the elements are string the sorting is in alphabetical order.

Question 3
(h) Consider the following String array and give the output
String arr[]= {“DELHI”,”CHENNAI”,”MUMBAI”,”LUCKNOW”,”JAIPUR”];
System.out.println(arr[0].length()>arr[3].length());
System.out.print(arr[4].substring(0,3)); [2]

false
JAI

2019

Question 2
(e) What is the difference between linear search and binary search? [2]

Linear Search: Search can be done in srted or unsorted elements. It searches data sequentially from index 0 to index n-1. So the searching speed is lower than binary search.
Binary search: The search can be done only in sorted elements. Its first search is in the middle of the array. If the serch element is found then the search is finished. If it is not there and it is greater than the middle element then the next search is the middle of the right half of the array. If the search element is lower than the middle element is the search is in the middle of of left half of the array. Such a way the search is done.

2020

Question 2
(b) State the difference between length and length() in Java. [2]

length: The length is predefined variable to store number of cells in an array. int n=a.length stores the number of cells of array named a to the variable n.
length(): The length() is a predefined String class method to return the number of characters in a string value.