0% found this document useful (0 votes)
52 views7 pages

Chapter 9 Strings and Text I/O: Answer: Correct

This document discusses string and text I/O in Java. It provides examples of using various string methods like equals(), compareTo(), indexOf(), substring(), toUpperCase(), replace(), split(), etc. It also discusses reading and writing to files using PrintWriter, File, Scanner classes and dealing with exceptions. Some key points covered are: - Strings are immutable in Java and string methods do not modify the original string. - Various methods to compare, extract substrings, convert case, replace characters etc in strings. - Using PrintWriter and File classes to write to files and close the writer to release resources. - Using Scanner and File to read from files and close the scanner after use.

Uploaded by

Ninh Lục Tốn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views7 pages

Chapter 9 Strings and Text I/O: Answer: Correct

This document discusses string and text I/O in Java. It provides examples of using various string methods like equals(), compareTo(), indexOf(), substring(), toUpperCase(), replace(), split(), etc. It also discusses reading and writing to files using PrintWriter, File, Scanner classes and dealing with exceptions. Some key points covered are: - Strings are immutable in Java and string methods do not modify the original string. - Various methods to compare, extract substrings, convert case, replace characters etc in strings. - Using PrintWriter and File classes to write to files and close the writer to release resources. - Using Scanner and File to read from files and close the scanner after use.

Uploaded by

Ninh Lục Tốn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 9 Strings and Text I/O

1.
s1 == s2 => true
s2 == s3 => false
s1.equals(s2) => true
s2.equals(s3) => true
s1.compareTo(s2) => 0
s2.compareTo(s3) => 0
s1 == s4 => true
s1.charAt(0) => W
s1.inde!f("#") => $1
s1.inde!f(%to%) => &
s1.last'nde!f("a") => 14
s1.last'nde!f(%o%( 1)) => *
s1.len+th() => 1,
s1.su-strin+()) => me to .a/a
s1.su-strin+()( 11) => me to
s1.startsWith(%Wel%) => true
s1.endsWith(%.a/a%) => true
s1.to0o1er2ase() => 1elcome to #a/a
s1.to3pper2ase()=> W402!54 T! .A6A
% Welcome %.trim() => Welcome
s1.replace("o"( "T") => WelcTme tT .a/a
s1.replaceAll(%o%( %T%) => WelcTme tT .a/a
s1.replace7irst(%o%( %T%) => WelcTme tT .a/a
s1.to2harArra8() returns an arra8 of characters consistin+ of W( e( l( c( o( m( e( ( t( o( ( .( a( /( a
(9ote that none of the operation causes the contents of a strin+ to chan+e)
2. String s = new String("new string");
Answer: Correct
String s3 = s1 + s2;
Answer: Correct
String s3 = s1 - s2;
Answer: Incorrect
s1 == s2
Answer: Correct
s1 >= s2
Answer: Incorrect
s1.compareTo(s2);
Answer: Correct
int i = s1.length();
Answer: Correct
char c = s1(0);
Answer: Incorrect
char c = s1.chart(s1.length());
Answer: Incorrect ! it"s o#t o$ %o#n&s' e(en i$ the prece&ing pro%lem is $i)e&.
3. The o#tp#t is
Welcome to .a/a
Welca-cme ta-c .a/a
*int! +o metho& in the String class can change the content o$ the string. String is
an imm#ta%le class.
,.
2hec: 1hether s1 is equal to s2 and assi+n the result
to a ;oolean /aria-le is4qual.
-oolean is4qual = s1.equals(s2)<
2hec: 1hether s1 is equal to s2 i+norin+ case and
assi+n the result to a ;oolean /aria-le is4qual.
-oolean is4qual = s1.equals'+nore2ase(s2)<
2ompare s1 1ith s2 and assi+n the result to an int
/aria-le .
int = s1.compareTo(s2)<
2ompare s1 1ith s2 i+norin+ case and assi+n the result
to an int /aria-le .
int = s1.compareTo'+nore2ase(s2)<
2hec: 1hether s1 has prefi %AAA% and assi+n the
result to a ;oolean /aria-le -.
-oolean - = s1.startsWith(%AAA%)<
2hec: 1hether s1 has suffi %AAA% and assi+n the
result to a ;oolean /aria-le -.
-oolean - = s1.endsWith(%AAA%)<
Assi+n the len+th of s1 to an int /aria-le .
int = s1.len+th()<
Assi+n the first character of s1 to a char /aria-le .
char = s1.charAt(0)<
2reate a ne1 strin+ s3 that com-ines s1 1ith s2.
=trin+ s3 = s1 > s2<
2reate a su-strin+ of s1 startin+ from inde 1.
=trin+ s3 = s1.su-strin+(1)<
2reate a su-strin+ of s1 from inde 1 to inde 4.
=trin+ s3 = s1.su-strin+(1( ))<
2reate a ne1 strin+ s3 that con/erts s1 to lo1ercase.
=trin+ s3 = s1.lo1ercase()<
2reate a ne1 strin+ s3 that con/erts s1 to uppercase.
=trin+ s3 = s1.uppercase()<
2reate a ne1 strin+ s3 that trims -lan: spaces on -oth
ends of s1.
=trin+ s3 = s1.trim()<
?eplace all occurrence of character e 1ith 4 in s1 and
assi+n the ne1 strin+ to s3.
=trin+ s3 = s1.replaceAll(@eA( @4A)<
=plit %Welcome to .a/a and BT50% into an arra8 to:ens
usin+ delimited -8 a space.
String[] tokens = "Welcome to Java and HTML".split( );
Assi+n the inde of the first occurrence of character e
in s1 to an int /aria-le .
int x = s1.indexOf(e);
Assi+n the inde of the last occurrence of strin+ a-c
in s1 to an int /aria-le .
int x = s1.lastIndexOf(abc);
-. +o.
.. 0.
/. 0se the o(erloa&e& static (al#e1$ metho& in the String class.
2. The te)t is &eclare& in 3ine 2 as a &ata $iel&' %#t re&eclare& in 3ine - as a local
(aria%le. The local (aria%le is assigne& 4ith the string passe& to the constr#ctor'
%#t the &ata $iel& is still n#ll. In 3ine 10' test.te)t is n#ll' 4hich ca#ses
+#ll5ointer6)ception 4hen in(o7ing the to3o4erCase() metho&.
8. The constr#ctor is &eclare& incorrectl9. It sho#l& not ha(e (oi&.
10. lo4ercase letter is %et4een :a; an& :<;. =o# can #se the static
is3o4erCase(char) metho& in the Character class to test i$ a character is in
lo4ercase. n #ppercase letter is %et4een :; an& :>;. =o# can #se the static
is0pperCase(char) metho& in the Character class to test i$ a character is in
#ppercase.
11. n alphan#meric character is %et4een :0; an& :8;' or :; an& :>;' or :a; an& :<;.
=o# can #se the static is3etter1r?igit(char ch) metho& in the Character class to
test i$ a character is a &igit or a letter.
12. The String@#il&er class' intro&#ce& in A?B 1.-' is similar to String@#$$er e)cept
that the #p&ate metho&s in String@#$$er are s9nchroni<e&.
13. 0se the String@#il&er;s constr#ctor to create a string %#$$er $or a string' an& #se
the toString metho& in String@#il&er class to ret#rn a string $rom a String@#il&er.
1,. String@#il&er s% = ne4 String@#il&er(s);
s%.re(erse();
s = s%.toString();
1-. String@#il&er s% = ne4 String@#il&er(s);
s%.&elete(,' 10);
s = s%.toString();
1.. @oth string an& string %#$$er #se arra9s to hol& characters. The arra9 in a string is
$i)e& once a string is create&. The arra9 in a string %#$$er ma9 change i$ the %#$$er
capacit9 is change&. To accommo&ate the change' a ne4 arra9 is create&.
1/.
(1) Aa(a is $#n
(2) Aa(a*TC3
(3) Aais $#n(a
(,) A*TC3a(a
(-) (
(.) ,
(/) Aa(
(2) Aa
(8) a(aA
(10) AComp#tera
(11) a(
(12) (a
12.
The o#tp#t is
Aa(a
Aa(a an& *TC3
+1T6!
Insi&e the metho&' the statement s = s + D an& *TC3D creates a ne4 String o%Eect
s' 4hich is &i$$erent $rom the original String o%Eect passe& to the change(s' %#$$er)
metho&. The original String o%Eect has not %een change&. There$ore' the printo#t
$rom the original string is Aa(a.
Insi&e the metho&' the content o$ the String@#il&er o%Eect is change& to Aa(a an&
*TC3. There$ore' the printo#t $rom %#$$er is Aa(a an& *TC3.
18.
pu-lic static /oid main(=trin+CD ar+s)
can -e replaced -8
pu-lic static /oid main(=trin+ ar+sCD)
pu-lic static /oid main(=trin+CD )
pu-lic static /oid main(=trin+ CD)
-ut not
static /oid main(=trin+ CD)
%eca#se it is not p#%lic.
20.
(1)
+#m%er o$ strings is ,
I
ha(e
a
&ream
(2)
+#m%er o$ strings is 1
1 2 3
(3)
+#m%er o$ strings is 0
(,)
+#m%er o$ strings is 1
F
(-)
+#m%er o$ strings is (the n#m%er o$ $iles an& &irector9 $rom 4here the comman& is
e)ec#te&)
?ispla9s all $iles an& &irector9 names in the &irector9 4here the comman& is
e)ec#te&.
21. The G is a special character. It sho#l& %e 4ritten as GG in Aa(a #sing the 6scape
seH#ence.
22. 0se e)ists() in the Iile class to chec7 4hether a $ile e)ists. 0se &elete() in the Iile
class to &elete this $ile. 0se renameTo(Iile) to rename the name $or this $ile. =o#
cannot $in& the $ile si<e #sing the Iile class.
23. +o. The Iile class can %e #se& to o%tain $ile properties an& manip#late $iles' %#t
cannot per$orm IJ1.
2,. To create a 5rintKriter $or a $ile' #se ne4 5rintKriter($ilename). This statement ma9
thro4 an e)ception. Aa(a $orces 9o# to 4rite the co&e to &eal 4ith e)ceptions. 1ne
4a9 to &eal 4ith it is to &eclare thro4s 6)ception in the metho& &eclaration. I$ the
close() metho& is not in(o7e&' the &ata ma9 not %e sa(e& properl9.
2-. The contents o$ the $ile temp.t)t is!
amount is 32.320000 3.232000e>01
amount is 32.3200 3.2320e>01
false
.a/a
2.. To create a Scanner $or a $ile' #se ne4 Scanner(ne4 Iile($ilename)). This statement
ma9 thro4 an e)ception. Aa(a $orces 9o# to 4rite the co&e to &eal 4ith e)ceptions.
1ne 4a9 to &eal 4ith it is to &eclare thro4s 6)ception in the metho& &eclaration.
I$ the close() metho& is not in(o7e&' the pro%lem 4ill r#n $ine. @#t it is a goo&
practice to close the $ile to release the reso#rce on the $ile.
2/. I$ 9o# attempt to create a Scanner $or a none)istent $ile' an e)ception 4ill occ#r. I$
9o# attempt to create a Iormatter $or an e)isting $ile' the contents o$ the e)isting
$ile 4ill %e gone.
2&. 9o. The line separator on Windo1s is ErEn.
28. int6alue contains 4). dou-le6alue contains )F.&( and
line contains " "( "F "( "& "( "*".
30. int6alue contains 4). dou-le6alue contains )F.&( and
line is empt8.

You might also like