9.3. Using the super Keyword

 
[Page 292 ( continued )]

Review Questions

Section 8.2 The String Class

8.1 Suppose that s1 , s2 , s3 , and s4 are four strings, given as follows :
 String s1 =   "Welcome to Java"   ; String s2 = s1; String s3 = new String(   "Welcome to Java"   ); String s4 = s3.intern(); 

What are the results of the following expressions?

 (1) s1 == s2 (3) s1.equals(s2) (2) s1 == s3 (4) s2.equals(s3) 

[Page 293]
 (5) s1.compareTo(s2) (15) s1.substring(   5   ,   11   ) (6) s2.compareTo(s3) (16) s1.startsWith(   "Wel"   ) (7) s1 == s4 (17) s1.endsWith(   "Java"   ) (8) s1.charAt(     ) (18) s1.toLowerCase() (9) s1.indexOf(   'j'   ) (19) s1.toUpperCase() (10) s1.indexOf(   "to"   ) (20)   " Welcome "   .trim() (11) s1.lastIndexOf(   'a'   ) (21) s1.replace(   'o'   ,   'T'   ) (12) s1.lastIndexOf(   "o"   ,   15   ) (22) s1.replaceAll(   "o"   ,   "T"   ) (13) s1.length() (23) s1.replaceFirst(   "o"   ,   "T"   ) (14) s1.substring(   5   ) (24) s1.toCharArray() 

8.2 Suppose that s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
 String s =   new   String(   "new string"   ); String s3 = s1 + s2; String s3 = s1 - s2; s1 == s2; s1 >= s2; s1.compareTo(s2); int i = s1.length(); char c = s1(     ); char c = s1.charAt(s1.length()); 

8.3 What is the printout of the following code?
 String s1 =   "Welcome to Java"   ; String s2 =   "Welcome to Java"   ; System.out.println(   "s1 == s2 is "   + s1 == s2); System.out.println(   "s1 == s2 is "   + (s1 == s2)); 

8.4 How do you compare whether two strings are equal without considering cases? How do you convert all the letters in a string to uppercase? How do you convert all the letters in a string to lowercase? Do the conversion methods ( toLowerCase , toUpperCase , trim , replace ) change the contents of the string that invokes them?
8.5 Suppose string s is created using new String() ; what is s.length() ?
8.6 How do you convert a char , an array of characters , or a number to a string?
8.7 Why does the following code cause a NullPointerException ?
 1   public class   Test { 2   private   String text; 3 4   public   Test(String s) { 5 String text = s; 6 } 7 8   public static void   main(String[] args) { 9 Test test =   new   Test(   "ABC"   ); 10 System.out.println(test.text.toLowerCase()); 11 } 12 } 

8.8 What is wrong in the following program?
 1   public class   Test { 2 String text; 3 

[Page 294]
 4   public void   Test(String s) { 5 this.text = s; 6 } 7 8   public static void   main(String[] args) { 9 Test test =   new   Test(   "ABC"   ); 10 System.out.println(test); 11 } 12 } 

Section 8.3 The Character Class

8.9 How do you determine whether a character is in lowercase or uppercase?
8.10 How do you determine whether a character is alphanumeric ?

Section 8.4 The StringBuilder / StringBuffer Class

8.11 What is the difference between StringBuilder and StringBuffer ?
8.12 How do you create a string buffer for a string? How do you get the string from a string buffer?
8.13 Write three statements to reverse a string s using the reverse method in the StringBuffer class.
8.14 Write three statements to delete a substring from a string s of twenty characters, starting at index 4 and ending with index 10. Use the delete method in the StringBuffer class.
8.15 What is the internal structure of a string and a string buffer?
8.16 Suppose that s1 and s2 are given as follows:
 StringBuffer s1 =   new   StringBuffer(   "Java"   ); StringBuffer s2 =   new   StringBuffer(   "HTML"   ); 

Show the results of the following expressions of s1 after each statement. Assume that the expressions are independent.

 (1) s1.append(   " is fun"   ); (7) s1.deleteCharAt(   3   ); (2) s1.append(s2); (8) s1.delete(   1   ,   3   ); (3) s1.insert(   2   ,   "is fun"   ); (9) s1.reverse(); (4) s1.insert(   1   , s2); (10) s1.replace(   1   ,   3   ,   "Computer"   ); (5) s1.charAt(   2   ); (11) s1.substring(   1   ,   3   ); (6) s1.length(); (12) s1.substring(   2   ); 

8.17 Show the output of the following program:
   public class   Test {   public static void   main(String[] args) { String s =   "Java"   ; StringBuffer buffer =   new   StringBuffer(s); change(s, buffer); System.out.println(s); System.out.println(buffer); }   private static void   change(String s, StringBuffer buffer) { s = s +   " and HTML"   ; buffer.append(   " and HTML"   ); } } 


[Page 295]

Section 8.5 Command-Line Arguments

8.18 This book declares the main method as
   public static void   main(String[] args) 

Can it be replaced by one of the following lines?

   public static void   main(String args[])   public static void   main(String[] x)   public static void   main(String x[])   static void   main(String x[]) 

8.19 Show the output of the following program when invoked using
  1. java Test I have a dream

  2. java Test "1 2 3"

  3. java Test

  4. java Test "*"

  5. java Test*

   public class   Test {   public static void   main(String[] args) { System.out.println(   "Number of strings is "   + args.length);   for   (   int   i =     ; i < args.length; i++) System.out.println(args[i]); } } 

Section 8.6 Regular Expressions

8.20 Describe whitespace characters, word characters, quantifiers, and grouping patterns in regular expressions.
8.21 Show the output of the following statements:
 System.out.println(   "abc"   .matches(   "a[\\w]c"   )); System.out.println(   "12Java"   .matches(   "[\\d]{2}[\\w]{4}"   )); System.out.println(   "12Java"   .matches(   "[\\w]*"   )); System.out.println(   "12Java"   .matches(   "[\\W]*"   )); System.out.println(   "12Java"   .matches(   "[\\d]*"   )); System.out.println(   "12Java"   .matches(   "[\\d]{2}[\\w]"   )); System.out.println(   "12Java"   .matches(   "[\\d]{2}[\\W]*"   )); System.out.println(   "12Java"   .matches(   "[\\D]{2}[\\w]+"   )); System.out.println(   "12Java"   .matches(   "[\\d]{1}[\\w]"   )); System.out.println(   "12Java"   .matches(   "[\\d]{1}[\\w]*"   )); System.out.println(   "12_Java"   .matches(   "[\\d]{2}[\\w]{5}"   )); System.out.println(   "12Java"   .matches(   "[\\d]{2}[\\w]{1,15}"   )); System.out.println(   "12#Java"   .matches(   "[\\d]{2}[\\w]{1,15}"   )); 

8.22 Show the output of the following statements:
 System.out.println(   "Java"   .replaceAll(   "[av]"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "av"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "[a][v]"   ,   "RX"   )); System.out.println(   "Java"   .replaceFirst(   "\\w"   ,   "RX"   )); System.out.println(   "Java"   .replaceFirst(   "\\w*"   ,   "RX"   )); System.out.println(   "Java12"   .replaceAll(   "\\d"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "\\d"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "\\W"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "\\D"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "\\w*"   ,   "RX"   )); 

[Page 296]
 System.out.println(   "Java"   .replaceAll(   "\\w+"   ,   "RX"   )); System.out.println(   "Java"   .replaceAll(   "\\w+?"   ,   "RX"   )); System.out.println(   "Java"   .replaceFirst(   "\\w+?"   ,   "RX"   )); 

8.23 The following methods split a string into substrings. Show the substrings for each method.
   "Java"   .split(   "[a]"   )   "Java"   .split(   "[av]"   )   "Java#HTML#PHP"   .split(   "#"   )   "JavaTOHTMLToPHP"   .split(   "TOTo"   )   "JavaTOHTMLToPHP"   .split(   "TH"   )   "JavaTOHTMLToPHP"   .split(   "TH"   )   "JavaTOHTMLToPHP"   .split(   "J"   ) 

8.24 How do you check whether a string contains all numeric digits?

Section 8.7 The File Class

8.25 What is wrong about creating a File object using the following statement?
   new   File(   "c:\book\test.dat"   ); 

8.26 How do you check whether a file already exists? How do you delete a file? How do you rename a file? Can you find the file size (the number of bytes) using the File class?
8.27 Can you use the File class for I/O?

Section 8.8 Text I/O

8.28 How do you create a PrintWriter to write data to a file? What is the reason to declare throws Exception in the main method in Listing 8.6, WriteData.java. What would happen if the close() method is not invoked in Listing 8.6.
8.29 Show the contents of the file temp.txt after the following program is executed:
   public class   Test {   public static void   main(String[] args)   throws   Exception { java.io.PrintWriter output =   new   java.io.PrintWriter(   "temp.txt"   ); output.printf(   "amount is %f %e\r\n"   ,   32.32   ,   32.32   ); output.printf(   "amount is %5.4f %5.4e\r\n"   ,   32.32   ,   32.32   ); output.printf(   "%6b\r\n"   , (   1   >   2   )); output.printf(   "%6s\r\n"   ,   "Java"   ); output.close(); } } 

8.30 How do you create a Scanner to read data from a file? What is the reason to declare throws Exception in the main method in Listing 8.7, ReadData.java. What would happen if the close() method is not invoked in Listing 8.7.
8.31 What will happen if you attempt to create a Scanner for a nonexistent file? What will happen if you attempt to create a PrintWriter for an existing file?
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net