0% found this document useful (0 votes)
27 views9 pages

Array Programs

The document provides examples of array programs and questions to write programs for various array operations like printing elements, finding duplicates, maximum/minimum, sorting, merging etc. A total of 36 questions are given with examples for writing programs on basic and advanced array concepts in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

Array Programs

The document provides examples of array programs and questions to write programs for various array operations like printing elements, finding duplicates, maximum/minimum, sorting, merging etc. A total of 36 questions are given with examples for writing programs on basic and advanced array concepts in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ARRAY PROGRAMS

1.Write a program to print the 2nd and 4th elements in the given array?
output should be 3.
int[] intArray = new int[]{1,3,2,6,5,4};

2.Write a program to print the 2nd repeated element in the given array.
output should be 3.
int[] intArray = new int[]{1,1,3,1,2,6,3,5,4};

3.Write a program to check


1) Arrays have same elements at each position.
2) Arrays have the same elements but position of elements are different.
3) Arrays have the same unique elements.
Given Arrays:
Integer[] a1 = {1,2,3,2,1};
Integer[] a2 = {1,2,3};
Integer[] a3 = {1,2,3,4};
Integer[] a4 = {1,2,3};
Integer[] a5 = {1,3,2};

4.Choose the correct way of initializing a multidimensional array below.

a)int[2][]x={(1,2),(3,4,5)};
b)int[][]x={(1,2),(3,4,5)};
c)int[][]x={1,2.3,4,5};

1
ARRAY PROGRAMS
d)All

5.What is the output the following code?

public class Test{


public static void main(String[] args)
{
char[] charArr = new char[5];
for(int i=0;i<charArr.length;++i)
{
charArr[i]='i';
System.out.print(charArr[i]+" ");
}
}
}

a)i i i i i
b)0 1 2 3 4
c)1 2 3 4 5
d)CE:Variable must provide either dimension expressings of an array initialization.

6.write a program to find Mean of odd index numbers.


ex:Array with more than 3 elements eg:{2,4,25,26,78,42}

2
ARRAY PROGRAMS
7.Write a program to find common elements between two arrays.
input:['g','y','p','q','r'],['q','t','i','p']
output:q,p

8.Sorting of an array.Write a program on it?

9.Second largest element in an array?

10.Combine two unsorted arrays and sort them.

11.Print odd and even positions in an array?

12.Write a program to print swap two numbers without using third variable.

13.Write a program to print duplicate elements in a given array

14.Write a program to merge two arrays.

15.Write a program to print even index and odd index elements in an array.

16.What is the output of this program?


class evaluate{
public static void main(String args[])
{

3
ARRAY PROGRAMS
intarr[] = new int[]{0,1,2,3,4,5,6,7,8,9}
int n=6;
n=arr[arr[n]/2];
System.out.println(arr[n]/2);
}
}

a)3
b)0
c)6
d)1

17.Which of the following is valid declaration for array?


1.char[] startsWith[];
int[] score;
char[8] dist;
school students[];
school students[5];
a)1,2,4
b)2,4,5
c)2,3,4
d)All are correct.
1.W.A.P to remove duplicate number in an array.

4
ARRAY PROGRAMS
18.W.A.P to find duplicate elements in the given array and print their sum.
Array = {5,3,4,6,7,5,3,2,1}
Duplicate elements : 5,3
sum of duplicate elements :5+3=8.

19.W.A.P to find the 2nd highest value from array.


I/P : [5,8,6,11,4,6,9]
o/p : 9.

20.class Metti{
public static void main(String[] args)
{
int a[] =(10,20,3);
System.out.println(a.length);
}
}

21.class Metti{
public static void main(String[] args)
{
float f1=10.20f;
float f2=10.20f;
System.out.println(f1==f2);
}

5
ARRAY PROGRAMS
}
22.#include<stdio.h>
int main()
{
int arr[] = {12,13,14,15,16};
printf("%d,%d,%d\n",sizeof(arr),sizeof(*arr),sizeof(arr[0]))
return 0;
}

a)10,2,4
b)20,4,4
c)16,2,2
d)20,2,2

23.#include<stdio.h>
int main()
{
Static char *s[] = {"black","White","pink","violet");
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s",**p+1);
return 0;
}

6
ARRAY PROGRAMS

24.Given 2 arrays,find which numbers from first array is not present in second
array,Eg[1,4,2,5,6] and [3,6,2,7,9] ans-1,4,5.

25.Write a program where the same array is find in the array?

26.Write a program to find largest and smallest numbers in an given unsorted array.

27.How to get the matching elements in an integer array?

28.Write a program to find the smallest even number in a given array.


For ex:
If the input array [10,40,50,6,3,100,5,1,200] then the output should be 6 is smallest
even number in the array.

29.Write a program to find out if there are any consecutive numbers in a given array,
and print the sequence
example- i/p:{7,4,6,9,1,2,3,4,30,40,32}
o/p:{1,2,3,4}
30.Write a program to read the age of a few persons.and then based on their age,print
count of childern and adults?
example- i/p:{10,20,30,40,15,8,50,60}
o/p: Children count:3
Adult count: 5

7
ARRAY PROGRAMS
31.Write a program to reverse a given array by swapping the elements within it,
without using a second array and without printing it from last to first
example-if user enters the i/p array A as{10,20,30,40,50,60,70}
when you print it o/p should be {70,60,50,40,30,20,10}

32.Program
public class Program12 {
public static void main(String[] args) {
int [] x= {10,20,70,60,40};
int highest=x[0];
int lowest=x[0];

for(int i:x) {
if(i>highest) {
highest = i;
}
if(i<lowest) {
lowest=i;
}
}
System.out.println(highest + " is highest number ");
System.out.println(lowest + " is lowest number ");
}
}

8
ARRAY PROGRAMS
33.Write a program for the below requirements.
Input :{-3,-2,-1,3,1};
Output:{-3,-2,-1,1,3};
Without changing the place of negative number we need to change the positions of
the positive number.

34.sort the array without changing -ve number Index


Test case1:
I/P a[] = {11,16,-2,-4,7,8};
o/p a[] = {7,8,-2,-4,11,16};

Test case2:
I/p a[] = {-6,4,2,-1,9};
o/p a[] = {-6,2,4,-1,9};
1.Write a program to print 2nd largest element in an array.

35.Sorting of an array in descending order.

36.Insert an element in an array and remove the element in an array?

You might also like