0% found this document useful (0 votes)
20 views

Arrays

The document discusses various array programs and questions related to arrays like linear search, binary search, sorting, etc. It contains 15 questions related to arrays where programs are to be written to perform operations like sorting, searching, calculating average, minimum, maximum etc on arrays.

Uploaded by

qwertyuiopsa0011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Arrays

The document discusses various array programs and questions related to arrays like linear search, binary search, sorting, etc. It contains 15 questions related to arrays where programs are to be written to perform operations like sorting, searching, calculating average, minimum, maximum etc on arrays.

Uploaded by

qwertyuiopsa0011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

What is the difference between the linear


search and the binary search technique?
(i) String x[] = {"Artificial intelligence",
"IOT", "Machine Learning", "Big data"};
2. Give the output of the following
statements: (i) System.out.println(x[3]); (ii)
System.out.println(x.length); Output:
(i) Big data
(ii) 4
3. What is the difference between the linear
search and the binary search technique? 4.
Differentiate between searching and sorting.
5. 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));
Output:
false
JAI
6. String x[] = {"SAMSUNG", "NOKIA",
"SONY", "MICROMAX", "BLACKBERRY"};
Give the output of the following statements:
(i) System.out.println(x[1]);
(ii) System.out.println(x[3].length());
Output:
(i) NOKIA
(ii) 8
7. 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]
OUTPUT:
(i) p=6
(ii) q=7+10*3 = 7+30 = 37
8. What will this code print? int arr[] =
new int[5]; System.out.println(arr);
(i) 0 (ii) value stored in arr[0] (iii) 0000 (iv)
garbage value Ans:
(iii) garbage value
9. Find the errors in the given program
segment and re-write the statements
correctly to assign values to an integer
array.
int a = new int(5);
for(int i = 0; i <= 5; i++)
a[i] = i;
Ans:
int a[]=new int[5];
for(int i=0;i<5;i++)
a[i]=i;
10. If int n[] = {1, 2, 3, 5, 7, 9, 13, 16}; 11.
What are the values of x and y?
x = Math.pow(n[4], n[2]);
y = Math.sqrt(n[5] + n[7]);
12. What is an array? Write a statement to
declare an integer array of 10 elements. 13.
Name the search or sort algorithm that: (i)
Makes several passes through the array,
selecting the 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 the key value of the middle
element of the array.
Ans:
(ii) Binary search
14. State the total size in bytes, of the
arrays a[4] of char data type and
p[4] of float data type.
Ans:
8 bytes
16 bytes
15. Write one difference between Linear Search
and Binary Search 16. Name the keyword that:
(i) is used for allocating memory to an
array.
Ans: new
17. Given that int x[][] = {{2, 4, 6}, {3, 5, 7}};
18. What will be the value of x[1][0] and
x[0][2]?
19. If array[] = {1, 9, 8, 5, 2}; (i)
What is array.length?
(ii) What is array[2]?
20. Differentiate between binary
search and linear search.
SECTION-B

QUESTION-1

Write a program to input 15 integer elements in an array and sort them in ascending order
using the bubble sort technique.

QUESTION-2

Write a program to accept name and total marks of N number of students in two single
subscripts array 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]
QUESTION-3

Write a program to input integer elements into an array of size 20 and perform the following
operations:
Display the largest number from the array.
Display the smallest number from the array.
Display sum of all the elements of the array.

Small=a[0]; High=a[0];
for(i=0;i<20;++i)
{ if(a[i]>High)
High=a[i];
if(a[i]<Small)
Small=a[i];
Sum=Sum+a[i];
}
QUESTION-4

Write a program to input and store roll numbers, names and marks in 3 subjects of n number
of students in five single-dimensional arrays and display the remark based on average marks
as given below:
Average marks = Total Marks ÷ 3
Average Marks Remark
85 - 100 EXCELLENT
75 - 84 DISTINCTION
60 - 74 FIRST CLASS
40 - 59 PASS
Less than 40 POOR
The maximum marks in the subject are 100.

QUESTION-5

Write a program to input twenty names in an array. Arrange these names in descending order
of alphabets, using the bubble sort technique.
QUESTION-6

Write a program to accept the year of graduation from school as an integer value from the user.
Using the linear search technique on the sorted array of integers given below, output the
message “Record exists” if the value input is located in the array. If not, output the message
“Record does not exist”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}

int year[]={1982, 1987, 1993, 1996, 1999,


2003, 2006, 2007, 2009, 2010}; int n,f=0;

for(i=0;i<year.length;++i)
{
if(year[i]==n)
{ f=1;
break;
}}
if(f==
1)
System.out.println(“Record exists at the
position”+(i+1)); else
System.out.println(“Record does not exist”);
QUESTION-7

Write a program to input 10 integer elements in an array and sort them in descending
order using bubble sort technique.

QUESTION-8
Write a program to accept the names of 10 cities in a single dimension array and their STD
(Subscriber Trunk Dialing) codes in another single dimension integer array. Search for a name
of a city input by the user in the list. If found, display “Search successful” and print the name
of the city along with its STD code, or else display the message “Search unsuccessful, no such
city in the list”.

QUESTION-9

Write a program to input and sort the weight of ten people. Sort and display them in descending
order using the selection sort technique.

QUESTION-10

Write a program to perform binary search on a list of integers given below, to search for an
element input by the user. If it is found, display the element along with its position,
otherwise display the message “Search element not found”. 5, 7, 9, 11, 15, 20, 30, 45, 89, 97.

L=0;
N=
F=0;
U=a.length – 1; while(L<=U)
{
M=(L+U)/2;
if(a[M]==N)
{
F=1;
break; }
else if(a[M]<N) L=M+1;
else
U=M-1;
} if(F==1)
System.out.println(“Search success and the
element”+N+”at the position”+(i+1)); else
System.out.println(“Search element not found”);
QUESTION-11

Shasha Travels Pvt. Ltd. gives the following discount to its customers:
Ticket Amount Discount
Above Rs. 70000 18%
Rs. 55001 to Rs. 70000 16%
Rs. 35001 to Rs. 55000 12%
Rs. 25001 to Rs. 35000 10%
Less than Rs. 25001 2%
Write a program to input the name and ticket amount for the customer and calculate the
discount amount and net amount to be paid. Display the output in the following format for
each customer:
SNo. Name Ticket charges Discount Net amount
____ ______ _______________ _________ ____________
Assume that there are 15 customers, first customer is given the serial number 1, next customer
2… and so on.

QUESTION-12

Write a program to store six elements in an array P, and four elements in an array Q and
produce a third array R, containing all elements of arrays P and Q. Display the resultant
array.
Example:
P[] = {4, 6, 1, 2, 3, 10}
Q[] = {19, 23, 7, 8}
R[] = {4, 6, 1, 2, 3, 10, 19, 23, 7, 8}

QUESTION-13

The annual examination results of 50 students in a class is tabulated 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 average marks of the students whose average mark is above 80.
c) Print the roll number and average marks of the students whose average mark is below 40.

QUESTION-14

Define a class and store the given city names in a single dimensional array. Sort these
names in alphabetical order using the bubble sort technique only.
INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta OUTPUT: Agra, Bangalore,
Calcutta, Delhi, Mumbai
QUESTION-15

Write a program to initialize the given data in an array and find the minimum and maximum
values along with the sum of the given elements. Numbers: 2, 5, 4, 1, 3 Output:
Minimum value: 1
Maximum value: 5
Sum of the elements: 15

You might also like