String Handlng
String Handlng
CHAPTER A T A GLANC
E
.
String Class Melbods
'
_trim ....:.. ,' 1
__.:..1> . 1ot :p pe ~ ( ) IJ1Cha,Aq l / I ,~,,,,,.or /' I ,- ,,,.,. i/ I oom~-" To~I
' --eq_,-'_u_als-'-'-1:..,_ -
-_re_Cas
_e__,/
j concat / gn_o
-"'
indc.~or ( )
I __ _
,
101,o\\erCa..~ ( ) length ( )
T IO N S
SOLVED QUES Sy st em .O ut . pr in t( x.
eq ua ls( t) );
(b ) false
\ \ 1111111 (a ) tr ue
~ M ul ti pl e C ho ic e Q
ue st io ns
(c ) E rr or
(d ) 1
of be lo w code: A ns . (a )
J. W ri te th e ou tp ut t of be lo w co de:
R eu se Re cy cl e" :
e 3. W ri te th e o ut pu
S tri ng t = "R ed uc Re us e Recycle " :
S tri ng x = ne w St
rin g (t ) ; St rin g t = "R e uu ce
= = t) ; ing (t);
Sy st em .o u t.p ri nt (x St rin g x = n ew S tr
(b ) fa lse . r( x. eq ua ls lg no re C··•-··
(I ll
·
(a ) tr ue Sys te m .o ut .p rm
(d ) I (b ) (al s•·
(c ) E rr or (a ) tr ue
A n s. (b ) (d )
of be lo w code: (c ) E rr or
2.
W ri te th e o ut pu t
= "R ed uc e R e us e R ec yc le " ; An s. (a )
Si ri ng t
S ir in g x = ne w St ring( r);
,., ;,, ,,, : I/C S E ) -
10
l'hl' nu•lhod rnm1mrfl,, I)
J- • . ' return .
1,,,1 strin~s 11re l'llUnl : s - - if 1I. Which of the followlnit Is lht• rnrrccl output
lflll'
l'' ) (I>) of IIH· lll'low slnlcnll'nfs'/
(,·) ll (cl) False String sir = new String() :
l .) Sys1c111.n111.prin1("Thc new si ring is" + sir):
111'· t
· \\'hill is the output for the ht•lo . ( 11 ) The new sl ring i:,
~- . • " w Sltt1,•mcn1 ·~
s~stt'[Link]( Saw ckctrkit . · (Ii) The new siring is null
nd
·. • ·01" 1\ 'b') + I )·,
•,11,kX Y'' W!lll'r". (c) Exccplion
(d) The new string i~ ~Ir
(11) _1 (I>) 0
Ans. (a)
(d I (d) - 1
12. _ _ _ _ C'l nss Is used to ronvcrt a 1,rimillvl'
(/!)
\\\)• d11t11 ty1>e to Its l'Orrcspondinit ohjcl'I. 120221
Gin' the output of the folio Wtn
. g Sll'ing
(). . (11) St ring (/,) Wrapper
methods: "MI SS ISSIPPl".indexOf('S') +
(r) System (ti) Math
"MISSISSIPPl".lastlndex('I')
12022]
Ans. (/J)
(a) to (b) 12
(cl 20 (cl) 11
W- (/,)
l;lA Short Answer Questions \\-
7. Whal is lhe output for: I. Give the output of the following:
[Link]("Malayalam" . replace ('a'. Siring n="Computcr Knowledge.";
'z') . substring( 3.6 )): String m= "Computcr Applicntions";
(a) Mzlzyzlzm (I>) Mzlayalarn [Link]([Link](0,8).concat(m.
(c) cyz (d) zyzl
substring(9) ));
IDS. (c) [Link] .print In ([Link]("e")) ;.
8. The java class S tring is in _ _ package.
Ans. CompulcrApplications
(a) [Link] ring
ll'UC
(b) [Link]
2. Write the output of the following:
(c) [Link]
(i) [Link] ([Link] Uppcr
(d) All of the above
Casc('R'));
Ans. (b)
(ii) [Link]([Link] ppcr
9. Siring s = new String(): will create an object
Case('J '));
of String with
(a) no characters
Ans. (i) true (ii) J
String Handling -
R ,1dcr ([Link] ));
. . . "l>l l lll"· (11pu1 S1rcanl c,
1\ ns. (a) S11111i,: 1.·11y- · • • ,,(new
1
11 tl 1•1cdl<,ll' l
(/•) B11fk11.•dl h-111k1 oh Ill'\'' 11 11ddl.c;. ~,·")
'
;
''l'I . l•111c1 yo111
sv~11.· 111.11111.p11111 I11 1 cn~1. • . • . in the address")·
. ( 1. O
f ·haractcr s ,
S11 ini,: 11.ld11·~, 11h , c.1111 nw • 11 mhcr c
• ) -l "j, 1hc II
(1') s , ,11·11111111 p1111thl(H1hh r s~.ll' 111,t th (
. .,,i(·uldrcsi-):
(,/) Sttlll!,! p1,~[Link]\dd1c~~ [Link](, ' . tO)·
. . .. • 1hstnn1,t(O, '
\I') St11ni,: l'\t1.1ctnl = 1111~talAdl Iicss.~I. · I. ,th() - 1);
. . (Address. cng
0st
l/) dm1 d1 - p11~[Link](P •'
ld h1h1ka11 h,1111 =rity.l.'quals("Ghazialrnd"):
(/,) int 1 =rit~.intk1,Of('B'):
(i) int pl1~ =[Link]~t lmkxOl'("DEL"):
lil St ring s I= "my mother is very loving";
Strine. s2=s [Link]('m'.'M');
~ c ("KARAM ARORA");
\I.) boolean bln=[Link] ase
(/) int rnmparison=[Link](str2);
(m) int pos=orig_str.indexOf(srch_str);
(11) int pos t =orig_str.indexOf(srch_str,10);
(o) String str2=[Link](4,11);
/" It extracts the sub-string from 4th position, here 4 specifies the beginning index and 11 Sjlc{',:
the end index, i.e., the stopping position. This 11th position character is not included in sub-sin:.
(p) String str_replaced= [Link](chl ,ch2);
(q) StringBuffer s="SUN MI CROSYSTEMS"·
'
[Link]( ); /*The reversed string gets stored in same objects*/
(r) String s=[Link]( );
(s) String s=[Link]();
(t) String s=[Link]();
(u} String name= "SMITH JOHN"·
'
String nam = [Link]( );
if( [Link]('Z'))
[Link] ("Hello Boss");
else if( [Link]('a'))
[Link]("Good Day Bo ")·
. , ss '
(v) S~n~g y=[Link](56.78);
(w) iJ ([Link]("34")- _
--true)
System
' o t · ("
· u .prmt Yahoooo !!")
else
[Link]("l'm sad");
-I