0% found this document useful (0 votes)
5 views34 pages

Array&Stringprograms

The document contains a series of multiple choice questions related to arrays in programming, covering topics such as array declaration, initialization, access, and manipulation. It includes questions on both one-dimensional and two-dimensional arrays, as well as sorting algorithms and error handling in code snippets. Additionally, there are sections for coding exercises and assertions regarding array properties.

Uploaded by

shouryaroy90
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)
5 views34 pages

Array&Stringprograms

The document contains a series of multiple choice questions related to arrays in programming, covering topics such as array declaration, initialization, access, and manipulation. It includes questions on both one-dimensional and two-dimensional arrays, as well as sorting algorithms and error handling in code snippets. Additionally, there are sections for coding exercises and assertions regarding array properties.

Uploaded by

shouryaroy90
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

Array

Q1. Multiple choice questions:

1. The statement to declare an array of 15 characters is


a. char [ ] N=new char[15]; b. char N[ ]=new char [14]
c. char [ ] N=new char [16]; d. char N[ ]= new char [ ];

2. The statement to initialize an integer array with 5 elements is


a. int a[ ] ={ 10,20,30,40,50,60}; b. int a[ ]={ 10,20,30};
c. int a[ ]={10,20,30,40,50} ; d. int a[ ]={ 10,20,60,70,30,80}

3. In the array Num[10], the 5th element is accessed using, the name of the
array as the address of the first element of the array
a. Num[5] b. Num[4] c.4=Num[4] d. Num[6]

4. The length of an array Num[ ] can be calculated using


a. [Link] b. [Link]() c. [Link] [Link]()

5. The of the array is the address of the first element of the array.
a. index b. size c. name d. length

6. The valid 2 – D declaration are


a) int m[ ] [ ]=new int [3][2] b. float [ ] [ ]m= new float[5][4]
c. x[ ] [ ] =new int [ ] [ ]{{1,2,3},{4,5,6},{7,8,9}}; d. All of these

7. Array are passed using call by method by default.


a. value b. reference c. primitive d. if( )

8. The mandatory condition for binary search is the requirements of


arrays.
a. sorted b. unsorted c. random d. method
9. Bubble, Selection, exchange are techniques of arrays.
a. Comparing b. Searching c. Sorting d. Unsorted

10. Given the following array : int m[ ]={ 2,24,35,50,5};


How many bytes it will occupies?
a. 20 bytes b.10 bytes c. 30 bytes d. 40 bytes.

11. Arrange the following java statements in the correct order of execution to
the values stored in the array a[ ] :
i. [Link](a [i] +”\t”);
ii. [Link](“ The elements int the array are: “);
iii. int i ;
iv. for( i=0; i<10; ++i)

Page 1 of 7
a) i, ii, iii, iv
b) ii, iii, iv, i
c) iv, iii, ii, i
d) iii, i, iv, ii

12) In the bubble sort technique, during each iteration of the inner loop, two
adjacent elements are and .
i. compared ii. swapped iii. selected iv. deleted
a) i and ii
b) ii and iii
c) iii and iv
d) ii and iv

13) The sum of a[1] and a[3] in the array int a[ ] ={20, 40, 60, 80,100} is:
a) 80 b) 100 c) 120 d) 60

14. Choose the correct statement about arrays in Java:


a. Arrays are dynamically sized.
b. Array can only store primitive types.
c. The index of arrays starts from 0.
d. The size of an array is defined at runtime.

15. The array double a[ 2 ] [ 3 ] occupies


a) 48 bytes b) 16 bytes c) 24 bytes d) 40 bytes

16. Number of rows in a two dimensional array m[ ] [ ] is


a. [Link] b. [Link]( ) c. m[ 0 ] length d. m[ ].length

17. Number of columns in a two dimensional array m[ ] [ ] is


a. [Link] b. [Link]( ) c. m[ 0 ] length d. m[ ].length

18. Given an array a [ ] ={ 25, 12, 45, 92, 8}. What will be the contents of the
array after the first pass of bubble sort
a. { 12, 25, 45, 92, 8} b. { 12, 25, 45, 8, 92} c. { 12, 25, 8, 45, 92}
d. { 12, 8,25, 45, 92}

19. Given an array a [ ] ={ 25, 12, 45, 92, 8}. What will be the contents of the
array after the first pass of selection sort
a. { 12, 25, 45, 92, 8} b. { 12, 25, 45, 8, 92} c. { 8,12, 45, 92, 25}
d. { 12, 8,25, 45, 92}

20. int a[ ] ={ 2, 3, 4, 5, 6}, then a [ ].length will result in :


a. 6 b. 5 c. a[5] d. Syntax error

21. char ch[ ] ={‘ A’, ‘ B’, ‘ C’, ‘ D’ };


[Link](ch[ 1] + ch[ 3]);

Page 2 of 7
What will be the output of the above code snippet?
a. 133 b. 134 c. 135 d.136

22. Consider the following array declaration


int a[ ] ={ 60, 40, 30,20, 10};
What will be the output of the following java statement?
[Link](a[ 1] + a[ 2] +3 );
4
a.70. 75
b. 70.0
c. 70
d. logical error
23. Predict the output of the following statement :
String wd [ ]= {“INDIA”, “USA”, “NEPAL ”, “GERMANY”, “MEXICO”}
[Link](wd[2].substring(0,3)+wd[4].substring(4));
a. USANY [Link] c. NEPCO d. INDNY

24. boolean b[ ]={ true, false};


[Link](b[ 1 ] * 2);
What will be the output of the above code fragment?
a. 2 b. 0 c. true * false d. Syntax error

25. The second last element of an array of size 100, has index:
a. 97 b. 99 c. 98 d.100

26. Name the search algorithm which compares the sought key value with
the key value of middle element of the array at each stage.
a. Bubble Sort b. Linear Search c. Binary Search d. Selection Sort

27. A two dimensional array, also known as 2D array, is a collection of data


elements arranged in a grid – like structure with rows and columns. Each
element in the array is referred to as a cell and can be accessed by its row and
column indices/ index. A 3 x 4 2D array typically stores 4 element in each of
the 3 rows.
How many elements will be stored in 2D array of size 5 x 3.
a. 8 elements b. 20 elements c. 15 elements d. 35 elements.

28. While declaring an array brackets are placed after the array
name.
a. { } b.( ) c. [ ] d. None

29. While referring the array element if we specify an index number that does
not exist, the program will raise and error indicating that the specified
index number does not exist.

Page 3 of 7
a. ArrayIndexOutOfScope b. ArrayIndexOutOfBound
c. ArrayIndexOutOfLimit d. ArrayElementNotExist

30. To sort an array having n elements, we require number of passes.


a. n b. n + 1 c. n – 1 d. 1

31. In search technique, the search value is compared one by one


with the elements of array, till the value is found in the array.
a. Binary b. Bubble c. Selection d. Linear

32. search algorithm uses the middle value of the array for search
operation.
a. Binary b. Bubble c. Selection d. Linear

Q2. Write the size of the following arrays in number of bytes:

i) int d[ ] =new int[20] ;


ii) long n[3] = new long[3] ;
iii) float x [ ] [ ] =new float[2][3] ;
iv) char y[ ] [ ]=new char[5][4] ;

Q3. Find error(s), if any in the following code snippets :

i. int x [ ] [ ]={ {2,3},{3,4}} ;


x[1 , 1]
ii. int b [ ] = new int [10 ]
for (int i=0; I<=[Link](); ++ i)

Q4. Output:

i) Given the following code : int x[ ]={ 11, 22,33,44};


[Link] value of x[-1] is:
b. What will be the contents of of x[2] after execution of the following code:
for(int i=0; i< [Link]; i++)
x[i]+=2;
ii. Amal wants to display only the alphabets stored in a character array. While
compiling the code an error was displayed. Rewrite the correct code and predict
the output.
char arr= { 4,@, a, w, d};
int i ;

Page 4 of 7
for(i=0; i<[Link]; ++i)
{
if([Link](arr[i]))
[Link](arr[i]);
}
iii. Consider the following program which calculate the Norm of a matrix. Norm
is square root of sum of squares of all elements.
Fill in the blanks(a) and (b) with appropriate java statements:
double norm( )
{
int x[ ] [ ] ={{1,5,6},{4,2,9},{6,1,3}} ;
int r,c,sum=0;
for(r=0;r<3;r++)
{
for(c=0; c<3;c++)
{
sum=sum + (a) ;
}

(b) ;

iv. int a[ ] ={ 23, 51,26,90,100};


a) What will be the output of a[3] +a[4] + a[1];
b) Write the size of this array in bytes.
v. Consider the array int [ ] arr ={3,5,1,9,12}; , Write a code snippet to find the
maximum value in the array.
vi. Consider the given array and answer the questions given below:
int x[ ]={4,7,9,10,30,40,2};
a. What is the length of the array?
b. What is the value of x[4]?
vii. Predict the output of the following code
int a[ ] ={12,10,5,8,7};
[Link]([Link] +a[[Link] -1]) ;
[Link](a[0]++ + - -a[2] *a[4]) ;
for(int i=0; i< [Link] ; ++i)
[Link](a[i ] +” “);
viii. What will be the output of the code
char x[ ] = { 65,66,32,68,67} ;
[Link](x[ 0 ] + “ “ + x[3]);
[Link](++x[1]);

Page 5 of 7
for(int i=0;i<[Link]; i++)
{
[Link]((char)x[i]);
}

ix. What will be the final value stored in array a[ ] after execution of the above
code.
int a[ ] = {2, 4,6,8,10} ;
for( int i=0; i< [Link] – 1; i++)
{
a[i] = ++ a[ i + 1];
}
x. Given that d[ ] [ ] = { { 4,5,6}, {1,2,3}};
What will be the output of the value of d[0][2] and d[1][0];

xi. Consider a two dimensional array m[5] [2] of float type. Write how many
elements can be stored in the array m. Also write the number of bytes required
to store this array.

x. What will be the output of following code?


int [ ] a={ 2, 5, 3, 7, 1, 4, ,6, ,9, 8, 5 } ;

a. [Link](a[ 3 + 4] ) ;
b. [Link](a[ 1 + 3] ) ;
c. [Link](a[ 5 – 3 ] ) ;
d. [Link](a[ 3] + 4 ) ;
e. [Link](a[ 3 ] * 2 ) ;

xi. What will be the output of following code ?


int [ ] a={ 2, 5, 3, 7, 1, 4, ,6, ,9, 8, 5 } ;

a. State the start and end indices of a[ ].


b. State the element stored at 3rd and 7th index.
c. Find and print the middle index and the element present.
d. State whether sum from 0 to 4 index are more than sum of element from 5
to 9 index or not.

xii. Given that int x [ ] [ ]={ { 2,4,6},{3,5,7}} ; what will be the value of x[ 1 ][ 0]
and x [0][2];

xii. State the total size in bytes of the array a[ 4] of char data type and p[4] of
float data types.

Page 6 of 7
Q5. Assertion and Reason
1) Assertion (A): An array variable is also known as subscript variable.
Reason(R) : In arrays, every element is represented with the help of its
individual index.
a) Both Assertion ( A ) and Reason ( R ) are true and Reason ( R ) is a correct
explanation of Assertion ( A ).
b. Both Assertion( A ) and Reason ( R) are true and Reason ( R ) is not a correct
explanation of Assertion ( A ).
c. Assertion ( A ) is true and Reason ( R) is false.
d. Assertion ( A ) is false and Reason ( R) is true.

2. Assertion ( A): When an array is initialized, its size is not specified.


Reason ( R ): The size of an array is equal to the number of elements in the
list.
a) Both Assertion ( A ) and Reason ( R ) are true and Reason ( R ) is a correct
explanation of Assertion ( A ).
b. Both Assertion( A ) and Reason ( R) are true and Reason ( R ) is not a correct
explanation of Assertion ( A ).
c. Assertion ( A ) is true and Reason ( R) is false.
d. Assertion ( A ) is false and Reason ( R) is true.

Q6. Write Java statement to perform the following :


1. To declare a single subscripted variable of 20 integers.
2. To declare subscripted variable of 10 real numbers.
3. To initialize first 10 even numbers in a single dimensional array.
4. To access and print the sum of first and the last element of an integer array
m.
5. Consider an array a [ ] has m elements and array b[ ] has n elements. Write
to create memory space for an array x [ ] to combine the elements of a[ ] and
b[ ].
6. To create memory space for an array of size 3 x 3.
7. To access the last 2nd element of an array a of size 4 x 4.

Page 7 of 7
Array Programs

Q1: /** WAP a program to assign Display the new array.


the following numbers 1, 2,3,4,5
Sample input: 10 20 30 5 25 60 45
1. Find the sum of all the numbers. 50 90 75
2. Find the largest number. Enter a number to be deleted:
5
3. Find the smallest number.*/
Sample output: 10 20 30 25 60 45
import [Link].*;
50 90 75
class ArraySumLGSM
*/
{
import [Link].*;
public static void main()
class Deleting1
{
{
int a[]={1,2,3,4,5};
public static void main()
int i,max=0,min=0,sm=0;
{
for(i=0;i<[Link];i++)
Scanner sc=new
{ Scanner([Link]);
sm=sm+a[i]; int n[]=new int [10];
if(a[i]>max)
int m[]=new int [10];
max=a[i];
if(min==0) int i,nd,k=0;
min=a[i];
[Link]("Enter any
else if(a[i]<min)
ten numbers");
min=a[i];
for(i=0;i<10;++i)
}
[Link]("Sum="+sm); {
[Link](" Largest
number ="+max); n[i]=[Link]();
[Link](" Smallest }
number="+min);
[Link]("Enter
} number to be deleted");
} nd=[Link]();
Q 2:
for(i=0;i<10;i++)
/**WAP to input 10 numbers in a
single dimensional array. {

Enter a number to be deleted if if(n[i]!=nd)


found and shift the elements. {

Page 1 of 15
Array Programs

m[k]=n[i]; int R[]=new int[10];


k++; int i,j,k=0;
} [Link]("Enter any
6 numbers");
}
for(i=0;i<6;++i)
[Link](" New
Array:"); {
for(i=0;i<k;i++) P[i]=[Link]();
{ }
[Link](m[i]+"\t"); [Link]("Enter any
4 numbers");
}
for(j=0;j<4;j++)
}
{
}
Q 3: /** WAP to store 6 elements Q[j]=[Link]();
in an array P and 4 elements in an
}
array Q. Now produce a third array
R, containing all the elements of
array P and Q. Display the
for(i=0;i<6;i++)
resultant array.
Sample input: Enter any six {
numbers: 10 20 30 40 50 60 R[k]=P[i];
Enter any four numbers: 70 80 90
100 k++;
Sample output: Merged array: 10
20 30 40 50 60 70 80 90 100
}
*/
for(j=0;j<4;j++)
import [Link].*;
{
class Merging
R[k]=Q[j];
{
k++;
public static void main()
{
Scanner sc=new }
Scanner([Link]); [Link]("Merged
int P[]=new int[6]; Array");

int Q[]=new int[4]; for(k=0;k<10;k++)

Page 2 of 15
Array Programs

{ int m[]=new int [50];


[Link](R[k]); int i,s=0,avg=0,max=0;
} String nm=" ";
} [Link](" Enter
the name of 50 students:");
}
for(i=0;i<50;i++)
Q4. /** The marks obtained by 50
students in a subject are tabulated {
as follows:
n[i]=[Link]();
* Name Marks
}
* ........... ............
[Link](" Enter
* ........... ............ the marks of 50 students:");
* Write a program to input the for(i=0;i<50;i++)
names and marks of the students
{
in the subject.
m[i]=[Link]();
* Calculate and display:
}
* a) The subject average marks (
subject average
marks=subject/50).
[Link]("Name
* b) The highest marks in the Marks");
subject and the name of the
students. for(i=0;i<50;++i)
{
* ( The maximum marks in the
subject are 100)
[Link](n[i]+"\t\t"+m[i])
*/
;
import [Link].*;
s=s+m[i];
class Marks1
if(m[i]>max)
{
{
public static void main(String
max=m[i];
args[])
nm=n[i];
{
Scanner sc=new Scanner( }
[Link]); }
String n[]=new String [50]; avg=s/50;

Page 3 of 15
Array Programs

[Link]("Subject for(j=0;j<4-i;j++)
Average="+avg);
{
[Link]("Highest
if(n[j]>n[j+1])
marks="+max);
{
[Link]("Name of
temp=n[j];
student who score highest
marks="+nm); n[j]=n[j+1];
} n[j+1]=temp;
} }
Question 5: }
/** WAP to enter 5 numbers in a }
single dimensional array. sort it in
[Link]("Sorted
ascending order using bubble sort
technique. display the sorted records in Ascending order");
records for(i=0;i<5;i++)
*/ {
import [Link].*; [Link](n[i]);
class BubbleASC }
{ }
public static void main() }
**Note : In Descending just change
{
the sign of: if(n[j]>n[j+1]) to <
Scanner sc=new
Scanner([Link]); Question 6:
int n[]=new int[5]; /** WAP to enter 5 names in a
single dimensional array. sort it in
int i,j,temp=0;
ascending order using bubble sort
[Link]("Enter any technique. display the sorted
five numbers"); records
for(i=0;i<5;i++) */
{ import [Link].*;
n[i]=[Link](); class BubbleStrtingASC
} {
for(i=0;i<4;i++) public static void main()
{ {

Page 4 of 15
Array Programs

Scanner sc=new }
Scanner([Link]);
}
String n[]=new String[5];
NOTE: In DESCENDING just
int i,j; change the sign of:
if(n[j].compareTo(n[j+1])>0) to <
String temp=" ";
[Link]("Enter any
five Names"); Q8. /** WAP to enter 10 character
in a single dimensional array. Sort
for(i=0;i<5;++i)
it in ascending order using
{ Selection sort techinque.
n[i]=[Link](); Display sorted records */

} import [Link].*;
for(i=0;i<4;++i) class Selection_sort_char

{ {

for(j=0;j<4-i;j++) public static void main()

{ {
Scanner sc=new
if(n[j].compareTo(n[j+1])>0) Scanner([Link]);

{ char c[]=new char[10],temp='


';
temp=n[j];
int i,j,pos=1,fl=0;
n[j]=n[j+1];
[Link]("Enter 10
n[j+1]=temp;
characters");
} for(i=0;i<10;i++)
}
{
}
c[i]=[Link]().charAt(0);
[Link]("Sorted
}
records in Ascending order");
for(i=0;i<9;i++)
for(i=0;i<5;++i)
{
{
pos=i;
[Link](n[i]);
for(j=i+1;j<10;j++)
}
{

Page 5 of 15
Array Programs

if(c[j]<c[pos]) String s[]=new


String[10],temp=" ";
{
int i,j,pos=1,fl=0;
pos=j;
[Link]("Enter 10
}
Strings");
}
for(i=0;i<10;i++)
temp=c[pos];
{
c[pos]=c[i];
s[i]=[Link]();
c[i]=temp;
}
}
for(i=0;i<9;i++)
[Link]("Sorted
{
characters");
pos=i;
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
{
{
[Link](c[i]);
}
if(s[j].compareTo(s[pos])>0)
{
}
pos=j;
}
}
Q9. /** WAP to enter 10 Strings in
}
a single dimensional array. Sort it
in descending order using temp=s[pos];
Selection sort technique.
s[pos]=s[i];
Display sorted records */
s[i]=temp;
import [Link].*;
}
class Selection_sort_char
[Link]("Sorted
{ Strings");
public static void main() for(i=0;i<10;i++)
{ {
Scanner sc=new [Link](s[i]);
Scanner([Link]);
}

Page 6 of 15
Array Programs

}
} }
} temp=n[pos];
Q10. /** WAP to enter 10 numbers n[pos]=n[i];
in a single dimensional array. Sort
n[i]=temp;
it in ascending order using
Selection sort technique. }
Display sorted records */ [Link]("Sorted
numbers:");
import [Link].*;
for(i=0;i<10;i++)
class Selection_sort_char
{
{
[Link](n[i]);
public static void main()
}
{
Scanner sc=new
Scanner([Link]); }
int n[]=new int[10],temp=0; }
int i,j,pos=1,fl=0;
Q11: WAP to enter name and roll
[Link]("Enter 10
numnber of 5 studens in two
numbers");
differnt single dimensional array.
for(i=0;i<10;i++) Enter a name to be [Link]
using Linear search, if found
{
display its position,name and
n[i]=[Link](); roll number. If not found display
the message " Search
} Unsuccessful".
for(i=0;i<9;i++)
import [Link].*;
{
class LinearSearch
pos=i; {
for(j=i+1;j<10;j++)
public static void main()
{
{
if(n[j]<n[pos])
Scanner sc=new
{ Scanner([Link]);
int rl[ ]=new int[5];
pos=j; String nm[]=new String[5];

Page 7 of 15
Array Programs

int i,fl=0,p=0; }
String ns=" ";
else
[Link]("Enter 5
Names"); {
for(i=0;i<5;++i)
[Link]("Search
{ Unsuccessful");
nm[i]=[Link](); }
} }
[Link]("Enter 5 }
Roll Numbers"); Q12: /** WAP to enter 5
for(i=0;i<5;++i) names/string. Enter a
name/string to be search search
{
using Binary search. If found
rl[i]=[Link](); display message "Search
Successful".If not found display
}
Search Unsuccessful"
[Link]("Enter
*/
name to be search");
import [Link].*;
ns=[Link]();
class BString
for(i=0;i<5;++i)
{
{
public static void main()
if(nm[i].equalsIgnoreCase(ns)) {
{ Scanner sc=new
Scanner([Link]);
fl=1;
String n[]=new String[5],ns;
p=i+1;
int i,j,fl=0,up=0,lp=4,mp=0;
break;
[Link]("Enter any
}
five names");
}
for(i=0;i<5;i++)
if(fl==1)
{
{
n[i]=[Link]();
[Link]("Position="+p);
[Link]("Name = }
"+nm[i]);
[Link]("Roll
Number="+rl[i]);

Page 8 of 15
Array Programs

[Link]("*********Sorted }
Array**************");
}
for(i=0;i<5;i++)
Q13./** WAP to enter 5
{ numbers. Enter a number to be
[Link] using Binary
[Link](n[i]);
search. If found display message
} "Search Successful".If not found
display Search Unsuccessful"
*/
[Link]("Enter a
name to be search");
ns=[Link](); import [Link].*;

while(up<=lp) class BNumber

{ {

mp=(up+lp)/2; public static void main()

if([Link](n[mp])==0) {

{ Scanner sc=new
Scanner([Link]);
fl=1;
int n[]=new int[5];
break;
int
}
i,j,ns,fl=0,up=0,lp=4,mp=0;
else
[Link]("Enter any
if([Link](n[mp])>0)
five numbers");
up=mp+1;
for(i=0;i<5;i++)
else {
lp=mp-1; n[i]=[Link]();
} }
if(fl==1)
[Link]("**********Sorte
[Link]("Search d Array**************");
Successful"); for(i=0;i<5;i++)
else
{
[Link](n[i]);
[Link]("Search
Unsuccessful"); }

Page 9 of 15
Array Programs

* Arrange characters of new word


in ascending order.
[Link]("Enter a
number to be search"); */
ns=[Link](); import [Link].*;
while(up<=lp) class ArrayString
{ {
mp=(up+lp)/2; public static void main()
if(ns==n[mp]) {
{ Scanner sc=new
Scanner([Link]);
fl=1;
String n[]=new String[5];
break;
int i,ch;
}
char c;
else if(ns>n[mp])
String wd=" ",nwd="";
up=mp+1;
[Link]("Enter 5
else
names:");
lp=mp-1;
for(i=0;i<5;i++)
}
{
if(fl==1)
n[i]=[Link]();
[Link]("Search
Successful"); }
else for(i=0;i<5;i++)
[Link]("Search
{
Unsuccessful");
c=n[i].charAt(0);
}
wd=wd+c;
}
}
Q14. /** Write a program to enter
5 names in a single dimensional [Link]("New
arrray. word="+wd);
* Frame a new word by picking for(ch=65;ch<=90;ch++)
first character of each word.
{
* Display a new word.
for(i=0;i<[Link]();i++)

Page 10 of 15
Array Programs

{
[Link]("*******Original
c=[Link](i);
Array**********");
if(c==(char)ch||c==(char)(ch+32))
for(i=0;i<3;i++)
nwd=nwd+c;
{
}
for(j=0;j<3;j++)
}
[Link]("Ascending {
word="+nwd);
[Link](n[i][j]+"\t");
}
}
[Link]();
}
}
Q15. /** WAP to enter numbers in
for(i=0;i<3;i++)
3 x 3 array. Find the sum of each
row and column separately*/ {
import [Link].*; for(j=0;j<3;j++)
class DDSUM_ROW_COL {
{ sr=sr+n[i][j];
public static void main() scl=scl+n[j][i];
{ }
Scanner sc=new [Link]("Sum of row
Scanner([Link]); " + (i+1)+ ":"+sr);
int n[][]=new int[3][3]; [Link]("Sum of
Column " + (i+1)+ ":"+scl);
int sr=0,scl=0,i,j;
sr=0;
[Link]("Enter
numbers in 3x3 array"); scl=0;
for(i=0;i<3;i++) }
{ }
for(j=0;j<3;j++) }
{ Q16. /** WAP to enter number in 3
x 3 array. Find the largest and
n[i][j]=[Link]();
smallest number*/
}
import [Link].*;
}

Page 11 of 15
Array Programs

class DDMAX_MIN [Link]("Largest


number :"+max);
{
[Link]("Smallest
public static void main()
number :"+min);
{
}
Scanner sc=new
}
Scanner([Link]);
Q17. /** WAP to enter number in 3
int n[][]=new int[3][3]; x 3 array. Find the sum of left and
right diagonal separately.*/
int max=0,min=0,i,j;
import [Link].*;
[Link]("Enter
numbers in 3x3 array"); class LR_Diagonal

for(i=0;i<3;i++) {

{ public static void main()

for(j=0;j<3;j++) {

{ Scanner sc=new
Scanner([Link]);
n[i][j]=[Link]();
int n[][]=new int[3][3];
}
int sld=0,srd=0,i,j;
}
[Link]("Enter
for(i=0;i<3;i++)
numbers in 3x3 array");
{
for(i=0;i<3;i++)
for(j=0;j<3;j++) {
{
for(j=0;j<3;j++)
if(n[i][j]>max)
{
max=n[i][j]; n[i][j]=[Link]();
if(min==0) }
min=n[i][j]; }
else if(n[i][j]<min) for(i=0;i<3;i++)
min=n[i][j];
{
}
for(j=0;j<3;j++)
} {
if(i==j)

Page 12 of 15
Array Programs

sld=sld+n[i][j]; n[i][j]=[Link]();
if((i+j)==2) }
srd=srd+n[i][j]; }
} [Link]("**************Or
iginal array************");
}
for(i=0;i<3;i++)
[Link]("Sum of left
diagonal :"+sld); {
[Link]("Sum of right for(j=0;j<3;j++)
diagonal :"+srd);
{
}
[Link](n[i][j]+" ");
}
}
Q18. /** WAP to enter numbers in
3 * 3 array. [Link]();
*Check whether sum of each row }
equal to
for(i=0;i<3;i++)
*sum of each column or not.*/
{
for(j=0;j<3;j++)
import [Link].*;
{
class RowEqualsColumn
rs=rs+n[i][j];
{
cs=cs+n[j][i];
public static void main()
}
{
if(rs!=cs)
Scanner sc=new
{
Scanner([Link]);
fl=1;
int n[][]=new int[3][3];
break;
int i,j,rs=0,cs=0,fl=0;
}
[Link]("Enter
numbers in 3x3 array"); }
for(i=0;i<3;i++) if(fl==0)
{ [Link]("All are
for(j=0;j<3;j++) same");
else
{

Page 13 of 15
Array Programs

[Link]("All are not N=[Link]();


same");
int num[][]=new int[M][N];
}
[Link]("Enter the
} daily sale for "+M*N+" shop");
for(i=0;i<M;i++)
Q19. /**'A Mega Mart' has M floors {
and N different types of shops in
for(j=0;j<N;j++)
each floor.
{
* Write a program in java, which
keeps the daily sale of num[i][j]=[Link]();
each shop of every floor. The }
manager wants to know the sale of
}
a particular shop of a particular
[Link]("The daily
floor by entering the floor
sale for "+M*N+" shop");
number and the shop number.
for(i=0;i<M;i++)
The program displays the desired
{
result by using multidimensional
array.*/ for(j=0;j<N;j++)
{
import [Link].*;
[Link](num[i][j]+" ");
class MegaMart
}
{
[Link]();
public static void main(String
args[]) }
{ [Link]("For
floor:0 for ground floor and
Scanner sc=new
according to");
Scanner([Link]);
[Link]("Enter the
int i,j,r,c,M,N;
floor number(row) between 0 to
[Link]("Enter the "+(M-1));
number of floor-");
r=[Link]();
M=[Link]();
[Link]("For shop
[Link]("Enter the number :0 for 1st shop and
number of shops in each floor-"); according to");

Page 14 of 15
Array Programs

[Link]("Enter the
shop number(column) between 0 to
"+(N-1));
c=[Link]();
[Link]("The daily
sale of floor number "+(r+1)+" and
shop number "+(c+1)+" is
Rs."+num[r][c]);

}
}

Page 15 of 15
Strings Progarms

Strings Programs
if(c=='a'||c=='e'||c=='i'||c=='o'||c
\' Insert a single quote =='u')
\" Insert a double quote break;
\\ Inserts a backslash }

[Link](" It\'s a fairy ns=[Link](i)+[Link](0,i)+"


land"); ay";
--> It's a fairy land [Link]("Piglatin
[Link](" Rahul word="+ns);
said\"May God Bless You\""); }
-->Rahul said "May God Bless You" }
[Link](" The symbol of /**Q2. Write a program to enter a
backslash is\\""); sentence. Count and display the
--> The symbol for backslash is\ frequency of each letter of the
sentence
in alphabetical word.
Sample input: APPLE
/** Q1. Write a program to enter a Sample output:
word and display the same in Pig CHARACTER
Latin form. FREQUENCY
i. Enter a word A 1
ii. Shift all the letters which E 1
are available before the first vowel, L 1
towards the end of the word. P 2
iii. Add ‘ay’ at the end of the */
word sequence.
*/ import [Link].*;
import [Link].*; class Frequency
class Piglatin {
{ public static void main()
public static void main() {
{ Scanner sc=new
Scanner sc=new Scanner([Link]);
Scanner([Link]); [Link]("Enter a
String s,ns=" "; string");
int i,l; String s;
char c; int i,l,f=0;
[Link]("Enter a char c,ch='A';
string"); s=[Link]();
s=[Link](); s=[Link]();
s=[Link](); l=[Link]();
l=[Link](); [Link]("CHARACTER
for(i=0;i<l;i++) FREQUENCY");
{ for(ch='A';ch<='Z';ch++)
c=[Link](i); {
for(i=0;i<l;i++)
{

Page 1 of 12
Strings Progarms

c=[Link](i); /**Q4. Write a program to enter a


if(c==ch) word. Check whether it is
f++; palindrome word or not.
} Sample input: DAD
if(f!=0) Sample output: Palindrome word*/
[Link](ch import [Link].*;
+"\t\t\t"+f); class Palindrome
f=0; {
} public static void main()
} {
} Scanner sc=new
/** Q3. Write a program to enter a Scanner([Link]);
word. String w,rev=" ";
Print a word in alphabetical order int i,l;
of its letters. char c;
Sample input: Cat [Link]("Enter a
Sample output:aCt */ word");
import [Link].*; w=[Link]();
class Alphabetical_order w=[Link]();
{ l=[Link]();
public static void main() for(i=0;i<l;++i)
{ {
Scanner sc=new c=[Link](i);
Scanner([Link]); rev=c+rev;
[Link]("Enter a }
string"); if([Link](rev))
String s,ns=" "; {
int i,l,ch=0;
char c; [Link]("Palindrome
s=[Link](); word");
l=[Link](); }
for(ch=65;ch<=90;ch++) else
{ {
for(i=0;i<l;i++) [Link]("Not
{ a Palindrome word");
c=[Link](i); }
if(c==(char)ch }
||c==(char)(ch+32)) }
ns=ns+c; /**Q5. Write a program to enter a
} sentence.
Frame a new word by joining the
} first character of each word of the
[Link]("New string sentence.
in alphabetical order :"+ns); Sample input: Vital Information
} Resources Under Seizee
Sample output: VIRUS*/
} import [Link].*;
class Frame_Word

Page 2 of 12
Strings Progarms

{ with 15 and Republic with


public static void main() Independence. Display the new
{ string as shown below
Scanner sc=new “August 15 is celebrated as
Scanner([Link]); Independence Day of India”*/
String s,ns=""; import [Link].*;
int l,i; class Replace_String
char c; {
[Link]("Enter a public static void main()
string"); {
s=[Link](); String s="January 26 is
s=[Link](); clebarted as the Republic Day of
s=" "+s; India";
l=[Link]();
for(i=0;i<l;++i) s=[Link]("January","August");
{ s=[Link]("26","15");
c=[Link](i);
if(c==' ') s=[Link]("Republic","Independen
{ ce");
ns=ns+[Link](i+1); [Link]("New string
} ="+s);
} }
[Link](ns); }
}
} /**Q8. Write a program to accept a
Varibales Data Description word and convert it into lowercase,
types if it is in upper case. Display the
s String To store new word by replacing only the
sentence vowels with the letter following it.
ns String To store Sample input: computer
new string Sample ouput: cpmpvtfr*/
c char To pick the
character import [Link].*;
i int To initialize class Vowels_nxt
the starting {
value and public static void main()
to run a {
loop Scanner sc=new
l int To find the Scanner([Link]);
length. String w,ns="";
/**Q6. Consider the sentence as int i,l,k=0;
given below: char c;
“January 26 is celebrated as [Link]("Enter
Republic Day of India”, write a word");
program to assign the given w=[Link]();
sentence to string variable. Replace l=[Link]();
the word January with August, 26 for(i=0;i<l;++i)
{

Page 3 of 12
Strings Progarms

c=[Link](i);
if(c=='A'||c=='E'||c=='I'||c=='O'||
if(c=='a'||c=='e'||c=='i'||c=='o'||c c=='U')
=='u') f++;
{ }
k= (int)(c+1); [Link]("Total
c=(char)k; number of charactes :"+l);
} [Link](" No of
ns=ns+c; vowels :"+f);
}
[Link]("New string [Link]("Reverse
="+ns); :"+rev);
} }
} }
/**Q10. Write a program to
/**[Link] a program to input assign a full path and file name as
any given string to calculate the given below. Using library
total number of characters and functions, extract and output the
vowels present in the string and file path, file name and file
also reverse the string. extension separately as shown.
Sample input: WORD Sample input:
Sample output: C:\Users\admin\Pictures\flowers.
Total number of characters 4 jpg
Number of vowels 1 Sample output:
Reverse string :DROW Path :
*/ C:\Users\admin\Pictures\
File name: flower
import [Link].*; Extension: jpg */
class NoCha_NoVoe_Rev
{ import [Link].*;
public static void main() class FilePathExtension
{ {
Scanner sc=new public static void main()
Scanner([Link]); {
String w,rev=""; Scanner sc=new
int i,l,f=0; Scanner([Link]);
char c; String
[Link]("Enter a path="",name="",s="",file="",extns="
word"); ";
w=[Link](); int i;
w=[Link](); [Link]("Enter full file
l=[Link](); name");
for(i=0;i<l;++i) s=[Link]();
{ i=[Link]("\\");
c=[Link](i); path=[Link](0,i);
rev=c+rev; name=[Link](i+1);

file=[Link]((i+1),[Link]("."));

Page 4 of 12
Strings Progarms

/**Q12. Write a program to enter a


extns=[Link]([Link](".")+1); string and print the pair of vowels
[Link]("Path :"+path); in the string.
[Link]("File:"+file); Sample input: BEAUTIFUL
[Link]("Extension:"+ex Sample output: EA AU
tns); Total pair :2 */
}
} import [Link];
class PairOfVowelsFreq
/**Q11. Write a program to enter a {
sentence and display only those public static void main(String
words which args[])
begins and ends with same {
alphabet.*/ Scanner sc=new
import [Link].*; Scanner([Link]);
class BegEndSame String s;
{ char c,ch;
public static void main() int i,l,f=0;
{ [Link]("Enter
Scanner sc=new sentence");
Scanner([Link]); s=[Link]();
String s="",wd=""; s=[Link]();
int i,l; s=s+" ";
char c,ch; l=[Link]();
[Link]("Enter [Link]("Pair of
sentence"); Vowels in Sentence:");
s=[Link](); for(i=0;i<l-1;++i)
s=[Link](); {
s=s+" "; c=[Link](i);
l=[Link](); ch = [Link](i + 1);
for(i=0;i<l;i++)
{ if((c=='A'||c=='E'||c=='I'||c=='O'||
c=[Link](i); c=='U')
if(c!=' ') &&(ch=='A'||ch=='E'||ch=='I'||ch
wd=wd+c; =='O'||ch=='U'))
else {
{ [Link]("
c=[Link](0); "+c+ch);
f++;
ch=[Link]([Link]()-1); }
if(c==ch) }
[Link](wd); [Link]("Frequency
wd=""; : "+f);
} }
} }
} /**[Link] a program to input a
} sentence and display only those

Page 5 of 12
Strings Progarms

words which begin and end with word after removing all the
vowel.*/ duplicate letters
import [Link]; Sample input: malayalam
class BegEndVowels Sample input: maly
{ */
public static void main() import [Link].*;
{ public class Replace
Scanner sc=new {
Scanner([Link]); public static void main()
String s="",wd=""; {
int i,l; Scanner sc=new
char c,ch; Scanner([Link]);
[Link]("Enter String s,str=" ";
sentence"); [Link]("Enter
s=[Link](); string");
s=[Link](); s=[Link]();
s=s+" "; s=[Link]();
l=[Link](); for(int i=0;i<[Link]();i++)
[Link]("Words {
which begins and end with char c=[Link](i);
vowels"); if([Link](c)==-1)
for(i=0;i<l;i++) str=str+c;
{ }
c=[Link](i); [Link](str);
if(c!=' ') }
wd=wd+c; }
else /** [Link] to enter any four
{ letter word and print all the
c=[Link](0); probable 4 letter combination. No
letter should be repeated*/
ch=[Link]([Link]()-1);
import [Link].*;
if((c=='A'||c=='E'||c=='I'||c=='O'|| class Combination
c=='U') {
&&(ch=='A'||ch=='E'||ch=='I'||ch public static void main()
=='O'||ch=='U')) {
{ Scanner sc = new
Scanner([Link]);
[Link](wd); String s;
} int i,l,j,k,x;
wd=""; [Link]("Enter a
} word:");
} s = [Link]();
} l = [Link]();
} if (l != 4)
/** [Link] to accept a word in {
lower case and display the new [Link]("Invalid
Input!");

Page 6 of 12
Strings Progarms

[Link]("Please public static void main()


enter a four letter word"); {
return; Scanner sc=new Scanner(
} [Link]);
for ( i = 0; i < l; i++) String s,ns="";
{ int i,l;
for ( j = 0; j < l; j++) char c;
{ [Link]("Enter your
for ( k = 0; k < l; k++) string");
{ s=[Link]();
for ( m = 0; m < l; s=" "+s;
m++) s=[Link]();
{ l=[Link]();
if (i != j && i != k for(i=0;i<l;++i)
&& i != m && j != k && j != m && {
k != m) c=[Link](i);
{ if(c==' ')
{
[Link]([Link](i)); c=[Link](i+1);
ns=ns+'
[Link]([Link](j)); '+[Link](c);
i++;
[Link]([Link](k)); }
else
[Link]([Link](m)); {
} ns=ns+c;
} }
} }
} [Link](" New
} string:"+ns);
} }
} }
/** /** [Link] a program in Java to
Q16. WAP to enter a sentence in accept two strings.
lowercase. Convert first characters Display the new string by taking
of each word of the sentence each character of the
in [Link] the new first string from left to right
sentence. and of the second string from right
Sample input: discipline to left.
dedication and determination are The letters should be taken
key to success alternatively from each string.
Sample output:Discipline Assume that the length of both the
Dedication And Determination Are strings are same.
Key To Success Sample Input:
*/ String 1: DOG
import [Link].*; String 2: CAT
class Change
{ Sample Output:

Page 7 of 12
Strings Progarms

DTOAGC */ s=[Link]();
s=[Link]();
import [Link].*; s=" "+s;
i=[Link](' ');
public class Left_Right p=[Link](' ',(i+1));
{ l=[Link](' ');
public static void main() [Link]("New
{ string="+[Link](i+1)+
Scanner sc = new [Link](p+1)+ [Link](l));
Scanner([Link]); }
String s1,s2,ns=" "; }
int i,l,ls;
char c,ch;
[Link]("Enter first /**Q19. Write a program to enter a
String: "); [Link] in alphabetical order
s1 = [Link](); of its letter.
[Link]("Enter Sample input: Computer
second String: "); Sample output: Cemoprtu*/
s2 = [Link](); import [Link].*;
l = [Link](); class Alphabetical
ls=[Link](); {
for ( i = 0; i < l; i++) public static void main()
{ {
c = [Link](i); Scanner sc=new
ch = [Link](ls - 1 - i); Scanner([Link]);
ns = ns + c + ch; String s;
} int i,l,ch=65;
[Link](" New char c;
string:"+ns); [Link]("Enter
} string");
} s=[Link]();
/** [Link] to enter a name l=[Link]();
containing 3 words and display the for(ch=65;ch<=90;ch++)
initials with the surname {
Sample intput: Saurav Thapa Allay for(i=0;i<l;++i)
Sample output: S T Allay */ {
import [Link].*; c=[Link](i);
class Initials
{ if(c==(char)ch||c==(char)(ch+32))
public static void main() [Link](c);
{ }
Scanner sc=new Scanner( }
[Link]); }
String s; }
int i,p,l; /**Q20. WAP to accept string and
char c,ch; display:
[Link]("Enter name a. The number of uppercase
containing 3 words"); letter

Page 8 of 12
Strings Progarms

b. The number of lowercase letter }


c. The number of digits /* Q21Write a program to accept a
d. The number of blanks word and form a new word by
e. The number of special joining all the letters at even
character*/ position followed by odd position
import [Link].*;
letter.
class Digit_Fre
{
Input: COMPUTER
public static void main()
{ Output: OPTRCMUE*/
Scanner sc=new Scanner(
[Link]); import [Link];
String s;
int public class Even_Odd_char
i,l,up=0,lp=0,dg=0,sp=0,ws=0;
char c; {
[Link]("Enter your public static void main(String
string"); args[])
s=[Link](); {
l=[Link]();
Scannerin=new
for(i=0;i<l;++i)
{ Scanner([Link]);
c=[Link](i); Stringstr,Estr="",Ostr="",newStr;
if(c>='A'&&c<='Z') [Link]("Enter a
up++; word:");
else if(c>='a'&&c<='z') str=[Link]();
lp++;
else if(c>='0'&&c<='9') str=[Link]();
dg++;
else for(int i=0;i<[Link]();i++)
if([Link](c))
ws++; {
else
sp++; if((i+1)%2==0)
}
Estr=Estr+[Link](i);
[Link]("****************
********************"); else
[Link]("Frequency of
capital letter :"+up); Ostr=Ostr+[Link](i);
[Link]("Frequency of
small letter :"+lp); }
[Link]("Frequency of
digit :"+dg); newStr= Estr+Ostr;
[Link]("Frequency of
blankspace :"+ws); [Link]("The
[Link]("Frequency of original string is: "+str);
special character :"+sp);
}

Page 9 of 12
Strings Progarms

[Link]("The final string }


is: "+newStr); [Link]("Enter the 2nd
Array");
}
for(int i=0;i<5;i++)
}
{
Q22. import [Link].*;
arr1[i]=[Link]();
/**
STRING ARRAY }

* write a program to input and [Link]("Input Array:");


check two string arrays of size 5
for(int i=0;i<5;i++)
are identical or not?
{
print the proper message after
checking. [Link](arr[i]+"\t");

}
Example:
Input: [Link]("******");
ram rom cv dvd BD(Array 1)
ram rom cv dvd BD (Array 2) for(int i=0;i<5;i++)
Output: Two arrays are identical {

*/ [Link](arr1[i]+"\t");
import [Link].*;
}
class String_arr
[Link](("******")
{ for(int i=0;i<5;i++)
if(arr[i].equals(arr1[i])==false)
public static void main(String {
args[]){
[Link]("Two arrays are
String arr[]=new String[5]; not identical");
String arr1[]=new String[5]; [Link](0);
Scanner sc=new }
Scanner([Link]);
}
[Link]("Enter the 1st [Link]("Two arrays
Array"); are identical");
for(int i=0;i<5;i++) }
{ }
Q23. /**WAP to input a word and
arr[i]=[Link]();
print the integrant of the word

Page 10 of 12
Strings Progarms

* where integrant of the word s=[Link]().trim();


means, if the length of the word
l=[Link]();
* is even then find its reverse and
if(l%2==0)
convert its first character
{
* to capital letter and remaining
letter should be in small letter. for(i=l-1;i>=0;i--)
* If the length of the word is odd ns=ns+[Link](i);
then join the first, middle

* and the last [Link]


the same. char
start=[Link](ns.c
* Sample input: CUTE harAt(0));
* Sample output:The integrant of
CUTE is Etuc remn=[Link](1).toLowerCase
();
*
[Link]("The
* Sample intput: Tiger
integrant of"+"\t" +s+"\t" +"is"+"\t"
* Sample output: The integrant of + start+remn);
Tiger is Tgr
}
*/import [Link].*;
else
public class Program
{
{
char first=[Link](0);
public static void main()
int m=l/2;
{
char mid=[Link](m);
Scanner sc=new
char last=[Link](l-1);
Scanner([Link]);
[Link]("The
String s,ns="",remn="";
integrant of"+"\t" +s+"\t" +"is"
int l,i; +"\t"+ first+mid+last);

char c; }

[Link]("Enter any }
word");

Page 11 of 12
Strings Progarms

} [Link]("The word
Q24. /*to accept a word and shift is : "+s2+s3);
the vowels to the left followed by
}
the consonants. Ignore other
characters.*/ }

import [Link].*;

class shift

public static void main()

Scanner sc=new
Scanner([Link]);
String s1,s2="",s3="";
int l,i;
[Link]("Enter a word :
");
s1=[Link]();
l=[Link]();
for(i=0;i<l;i++)

char ch1=[Link](i);

if([Link](ch1))

{
if("AEIOUaeiou".indexOf(ch1)>=0)

s2+=ch1;

else

s3+=ch1;

Page 12 of 12

You might also like