Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

ICSE 2022-23

CLASS X
COMPUTER APPLICATIONS

Ch.7 : Arrays

Q.1. What do you understand by an Array?


Ans. 1. Array: It is a named set of elements of similar type that are stored in contiguous
memory locations.
2. It can contain a fixed number of elements of a particular data type and each
element has an associated position number which is called as an index or
subscript which starts with 0.

Q.2. Write the syntax to create a 1D with an example


Ans. 1. The syntax for creating a 1D Array is:
datatype variable[ ]= new datatype[size];
Or
datatype[ ] variable= new datatype[size];
For e.g. int m[] = new int[10];

Q.3. How is an array initialized?


Ans. Array can be initialized by 2 methods:

1. Datatype[ ] variable= new datatype[size];


e.g., int num[ ]= new int [5];
num[0]=40; num[1]=33; num[2]=75; num[3]=90; num[4]=84;

2. Datatype variable[ ]= {data values separated by commas};

e.g., int num[ ]={40,33,75,90,84};


40 33 75 90 84

char S[ ]={‘a’ , ‘m’, ‘i’, ‘t’};


a m i t

Q.4. What do you understand by Length function of an array?


Ans. 1. It returns the number of elements of the array.
2. For e.g. int arr= new int [5];
System.out.println(arr.length);

Q.5. What are the advantages of an Array?


Ans. 1. Array can hold number of values of same type in same location.
2. Index or subscripts are generated automatically in array which helps us
toaccess any value of an array directly.

Q.6. What are the limitations of an Array?


Ans. 1. Array can hold number of values but all are of same type.
2. Excess storage in array which is unutilized will lead to memory wastage.
Q.7. What are the basic operations on Arrays?
Ans. 1. Searching
2. Sorting
3. Insertion
4. Deletion
5. Merging

Q.8. Define Searching.


Ans. 1. Searching: It is a process to determine whether a given item is present in an
array or not.
2. The items can be a number, a character, a word or a name.

Q.9. What types of searching can be performed on an array?


Ans. Two types of searching can be performed on an array.
Types of Searching

Linear search or Binary Search


sequential search

Q.10. Define Linear Search.


Ans. 1. Linear Search: It is done on unsorted as well as sorted arrays
2. It is a sequential search that starts from the first element of the array
i.e.,0th element and proceeds until it locates the element or until it reaches the
last element of the array.

Q.11. Define Binary Search.


Ans. 1. Binary Search: It is done only on sorted arrays.
2. This technique helps in locating the required element quickly from a sequential
list.

Q.12. Define Sorting.


Ans. 1. Sorting: It is a process of arranging the values in an array in ascending or
descending order.
2. Integer values can be arranged in ascending and descending order where as an
array of string can be arranged in alphabetical order.

Q.13. What are different types of Sorting?


Ans. Types of Sorting

Selection sorting Bubble sorting

Q.14. Define Selection Sorting.


Ans. 1. Selection Sorting: This technique is used to sort data items in ascending or
descending order.
2. Successive rounds are executed to select one element at a time and place it into
its final position in the array.

Q.15. Define Bubble Sorting.


Ans. 1. Bubble Sorting: This technique is used to sequentially scan elements several
times.
2. During each iteration for every pair of consecutive elements, the elements are
compared and interchanged if they are not in the sorted order.

Q.16. Differentiate between Linear Search and Binary Search.


Ans.
Linear Search Binary Search

1. It works both for sorted and 1. It takes place only on sorted data items.
unsorted arrays.

2. It begins from start of an 2. Array is divided into two halves and then
array. desired data item is matched with the
middle element in the array. If it does not
match, then the search is restarted either
in first half or in second half.

Q.17. 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]);
Ans. x=343 y=5

Q.18. Write a Java statement to compare two Strings m[i] and m[j] in a single
dimensional array.
Ans. If(m[i].compareTo(m[j])>0)

Q.19. Predict the output:


char ch[]={‘B’, ‘L’, ‘U’,’E’,’J’};
String m= new String(ch);
String n=new String(m.LowerCase());
System.out.println(m);
System.out.println(n);
Ans. m= BLUEJ
n= bluej

Q.20. Predict the output:


int p[]= {3,5,7,9,11,13,15,17,19,21);
for(i=8;i>=0;i=i-2)
{
System.out.println(p[i]);
}
Ans. 19
15
11
7
3
JAVA PROGRAMS BASED ON ARRAYS

Q.1. WAP to create an array containing three elements i.e., firstname,


middlename and lastname (string value)and print the fullname with
appropriate space in between.
Ans. class Arr1
{
public void strarr()
{
String name[];
name=new String(3);
name[0]=“Vijay”;
name[1]=“Kumar”;
name[2]=“Pandey”;
System.out.println(“Name=”+name[0]+“”+name[1]+“”+name[2])
}
}

Q.2. WAP to create an array to enter age of five persons (integer value), store
them in an array and display.
Ans. import java.util.*;
class Arr2
{
int a[];
Scanner in = new Scanner (System.in);
Arr2
{
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println(“Enter an Age=”);
a[i]=in.nextInt());
}
}
void display()
{
System.out.println(“\nValues are as follows“);
for(int i=0;i<n;i++)
{
System.out.print(““+a[i]);
}
}
}

Q.3. WAP to accept 10 numbers by the user and display only odd numbers
Ans. import java.util.*;
public class Arr3
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
{
int a=new int[n];
int b=new int[n];
int oddnum=0;
for(int i=0;i<10;i++)
{
System.out.println(“Enter 10 numbers”);
a[i]=in.nextInt());
if(a[i]%2!=0)
{
b[oddnum]=x[i];
Oddnum++;
}
}

for(int j=0;i<oddnum;j++)
{
System.out.print(b[j]);
}
}

Q.4. WAP to generate and store prime numbers 1 to 50 in an array Array_Prime.


Ans. class Array_Prime
{
public static void main(String []args)
{
int ap[]=new int[100];
int rem=0;
int i,r=0;
int k=0;
for(int num=1;num<50;num++)
{
for(i=2;i<=num-1;i++)
{
rem=num%i;
if(rem==0)
r=1;
}
if(r!=1)
{
ap[k]=num;
System.out.println(ap[k]);
}
r=0;
}
}
}

Q.5. WAP to accept 10 numbers in a SDA and linear search that given no is
present with position or not and display the relevantmessage.(linear search
for integer)
Ans. import java.util.*;
public class Linear_pos
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int i,pos,sh;pos=0;
int m[]=new int[10];

for(i=0;i<=9;i++)
{
System.out.print("Ënter number in the cell");
m[i]=in.nexInt();
}

System.out.println("Ënter the number to be searched");


sh=in.nextInt();
for(i=0;i<=9;i++)
{
if(sh==m[i])
{
pos=i+1;
break;
}
}
if(pos>0)
System.out.println("The required number"+sh+" is present at"+pos
+"position");
else
System.out.println("The required number"+sh+" is not present");
}
}

Q.6. WAP to accept 5 string in a SDA and check a given no is present or not and
display the relevant message(linear search for string).
Ans. import java.util.*;
public class Linear_string
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
String names[]={"Hiten","Swastik","Navneet","Äkansha","Shweta"};
int pos;pos=-1;
System.out.println("Ënter the name to be searched");
String sh=in.next();
for(inti=0;i<=4;i++)
{
if(sh.equalsIgnoreCase(names[i]))
{
pos=i;
break;
}
}
if(pos>=0)
System.out.println("The name "+sh+" ïs present");
else
System.out.println("The name "+sh+" is not present");

}
}
Q.7. WAP to accept number in an SDA and check that the given no is present or
not and display the relevant message. (binarysearchforinteger)
Ans. import java.util.*;
public class Binary_int
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int i,lb,ub,pos,mid,searchno;
searchno=0;pos=0;lb=0;ub=9;mid=0;
int m[]=new int[10];

for(i=0;i<=9;i++)
{
System.out.print("Enter number in ascending order in the cell");
m[i]=in.nextInt();
}

System.out.println("Ënter the number to be searched=");


searchno=in.nextInt();

while(lb<=ub)
{
pos=(lb+ub)/2;

if(searchno==m[mid])
{
pos=mid+1;
break;
}
elseif(searchno>m[mid])
lb=mid+1;
else
ub=mid-1;
}
if(pos==0)
System.out.println("The search is unsuccessful");

else
System.out.println(“The"+searchno+"is found at"+pos+"position");
}
}

Q.8. WAP to accept 5 string in a SDA and search whether the selected name is
present or not and display the relevant message.(binary search for string)
Ans. import java.util.*;
public class Binary_string
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int i,lb,ub,pos,k;k=0;
String str2;
pos=0;lb=0;ub=4;
String str[]=new String[5];
System.out.print("Ënter 5 names");
for(i=0;i<=4;i++)
{

str[i]=in.next();
}

System.out.println("Ënter the string to be searched=");


str2=in.next();

while(lb<=ub)
{
pos=(lb+ub)/2;

if(str[pos].compareTo(str2)<0)
lb=pos+1;

if(str[pos].compareTo(str2)>0)
ub=pos-1;

if(str[pos].compareTo(str2)==0)
{
k=1;
break;
}

}
if(k==1)
System.out.println("The"+str2+" is found at"+pos+"position");
else
System.out.println("The search is unsuccessful");
}
}

Q.9. WAP to sort an array using bubble sort in ascending order (on integer value)
Ans. import java.util.*;
public class Bubble_ascend
{
public static void main(String args[])
{

Scanner in = new Scanner (System.in);


int i,j,t;
int m[]=new int[10];
for(i=0;i<10;i++)
{
Sysytem.out.print(“Enter a number:”);
m[i]=in.nextInt();
}
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(m[j]>m[j+1])
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
System.out.println(“The numbers are arranged in ascending order as:”);
for(i=0;i<10;i++)
System.out.println(m[i]);
}
}

Q.10. WAP to sort an array using bubble sort in descending order. (on integer value)
Ans. import java.util.*;
public class Bubble_descend
{
public static void main(String args[])
{

Scanner in = new Scanner (System.in);


int i,j,t;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.print(“Enter numbers:”);
m[i]=In.nextInt();
}
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(m[j]<m[j+1])
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
System.out.println(“The number are arranged in descending order as:”);
for(i=0;i<10;i++)
System.out.println(m[i]);
}
}

Q.11. WAP to sort an array using bubble sort in ascending order. (on String value)
Ans. import java.util.*;
public class Bubble_st_ascend
{
public static void main(String args[])
{

Scanner in = new Scanner (System.in);


int i,j;
String t;
String m[]=new String[10];
for(i=0;i<10;i++)
{
Sysytem.out.print(“Enter names:”);
m[i]=in.next();
}
for(i=0;i<9;i++)
{
for(j=(i+1);j<=10;j++)
{
if(m[i].compareTo(m[j]>0)
{
t=m[i];
m[i]=m[j];
m[j]=t;
}
}
}
System.out.println(“The names are arranged in ascending order as:”);
for(i=0;i<10;i++)
System.out.println(m[i]);
}
}

Q.12. WAP to sort an array using selection sort in ascending order. (on integer
value)
Ans. import java.util.*;
public class Seleca_scend
{
public static void main(String args[])
{

Scanner in = new Scanner (System.in);


int i,j,t,min;
int m[]=new int[10];
for(i=0;i<10;i++)
{
Sysytem.out.print(“Enter numbers:”);
m[i]=in.nextInt();
}
for(i=0;i<9;i++)
{
min=i;
for(j=i+1;j<10;j++)
{
if(m[j]<m[min])
{
min=j;
}
t=m[i];
m[i]=m[min];
m[min]=t;
}
System.out.println(“The number are arranged in ascending order as:”);
for(i=0;i<10;i++)
System.out.println(m[i]);
}
}
Q.13. WAP to initialize a Double Dimension Array(DDA) of 3 rows and 4 columns
and display the sum of respective row elements and column elements.
Ans. import java.util.*;
public class Arr_Sum
{
public static void main(String args[])
{

Scanner in = new Scanner (System.in);


int i,j,sumr=0,sumc=0;
int m[][]=new int[3][4];

System.out.println(“Enter elements”);

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=in.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
sumr=sumr+m[i][j];
}
System.out.println(“The sum of row “+(i+1)+” = “+sumr);
sumr=0;
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
sumc=sumc+m[j][i];
}
System.out.println(“The sum of column “+(i+1)+” = “+sumc);
sumc=0;
}
}
}
Q.14. WAP to initialize a Double Dimension Array(DDA) of 3 rows and 3 columns
and display the diagonal elements.
Ans. import java.util.*;
public class Diagonal
{
public static void main(String args[])
{

Scanner in = new Scanner (System.in);


int i,j;
int m[][]=new int[3][3];
System.out.println(“Enter elements”);

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m[i][j]=in.nextInt();
}
}
System.out.println(“The array is:”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(m[i][j]+” “);
} System.out.println();
}

System.out.println(“The left diagonal elements are:”);


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
System.out.println(m[i][j]);
}
}
System.out.println(“The right diagonal elements are:”);
for(i=0;i<3;i++)
{
System.out.println(m[i][2-i]);
}
}}

Q.15. Write a program in Java to store 20 numbers in a Single Dimensional Array


(SDA). Display the numbers which are prime.
Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]

45 65 77 71 90 67 ... 82 19 31 52

Sample Output: 71, 67, 19, 31


import java.util.Scanner;

public class SDAPrimeNumbers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {

int c = 0;
for (int j = 1; j <= arr[i]; j++) {
if (arr[i] % j == 0) {
c++;
}
}

if (c == 2)
System.out.print(arr[i] + ", ");
}
}
}

Q.16. Write a program to accept name and total marks of N number of students in
two single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]

import java.util.Scanner;

public class SDAMarks


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();

String name[] = new String[n];


int totalmarks[] = new int[n];
int grandTotal = 0;

for (int i = 0; i < n; i++) {


in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}

double avg = grandTotal / (double)n;


System.out.println("Average = " + avg);

for (int i = 0; i < n; i++) {


System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}

Q.17. The annual examination result of 50 students in a class is tabulated in a


Single Dimensional Array (SDA) is as follows:

Roll No. Subject A Subject B Subject C

....... ....... ....... .......

....... ....... ....... .......

....... ....... ....... .......

Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is
above. 80.
(c) Print the roll number and the average marks of the students whose average is
below 40.

import java.util.Scanner;

public class Q14


{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


int sA[] = new int[TOTAL_STUDENTS];
int sB[] = new int[TOTAL_STUDENTS];
int sC[] = new int[TOTAL_STUDENTS];
double avg[] = new double[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}

System.out.println("\nRoll No\tAverage Marks");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average above 80:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average below 40:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}
}

You might also like