Array&Stringprograms
Array&Stringprograms
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]
5. The of the array is the address of the first element of the array.
a. index b. size c. name d. length
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
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}
Page 2 of 7
What will be the output of the above code snippet?
a. 133 b. 134 c. 135 d.136
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
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
32. search algorithm uses the middle value of the array for search
operation.
a. Binary b. Bubble c. Selection d. Linear
Q4. Output:
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) ;
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.
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 ) ;
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.
Page 7 of 7
Array Programs
Page 1 of 15
Array Programs
Page 2 of 15
Array Programs
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
{ {
{ {
Scanner sc=new
if(n[j].compareTo(n[j+1])>0) Scanner([Link]);
Page 5 of 15
Array Programs
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].*;
{ {
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
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
for(i=0;i<3;i++) {
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
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 }
Page 1 of 12
Strings Progarms
Page 2 of 12
Strings Progarms
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
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
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
Page 9 of 12
Strings Progarms
}
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
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
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