Introduction to Java Programming, Sixth Edition, Y. Daniel Liang

Chapter 8 Strings and Text I/O


Section 8.2 The String Class
1  Suppose s is a string with the value "java". What will be assigned to x if you execute the following code?

char x = s.charAt(4);

A. 'a'
B. 'v'
C. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

2  Which of the following statements is preferred to create a string "Welcome to Java"?

A. String s = "Welcome to Java";
B. String s = new String("Welcome to Java");
C. String s; s = "Welcome to Java";
D. String s; s = new String("Welcome to Java");

3  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = "Welcome to Java!";
     String s2 = s1;

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

4  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = "Welcome to Java!";
     String s2 = "Welcome to Java!";

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

5  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = new String("Welcome to Java!");

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

6  Suppose s1 and s2 are two strings. What is the result of

     s1.equals(s2) == s2.equals(s1)

A. true
B. false

7  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = new String("Welcome to Java!");

     if (s1.equals(s2))
       System.out.println("s1 and s2 have the same contents");
     else
       System.out.println("s1 and s2 have different contents");
   }
}

A. s1 and s2 have the same contents
B. s1 and s2 have different contents

8  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java!");
     String s2 = s1.toUpperCase();

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else if (s1.equals(s2))
       System.out.println("s1 and s2 have the same contents");
     else
       System.out.println("s1 and s2 have different contents");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 have the same contents
C. s1 and s2 have different contents

9  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = new String("Welcome to Java");
     String s2 = s1;

     s1 += "and Welcome to HTML";

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

10  What is the output of the following code?

public class Test {
   public static void main(String[] args) {
     String s1 = "Welcome to Java!";
     String s2 = "Welcome to Java!";

     if (s1 == s2)
       System.out.println("s1 and s2 reference to the same String object");
     else
       System.out.println("s1 and s2 reference to different String objects");
   }
}

A. s1 and s2 reference to the same String object
B. s1 and s2 reference to different String objects

11  Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

A. String s = new String("new string");
B. String s3 = s1 + s2
C. s1 >= s2
D. int i = s1.length
E. s1.charAt(0) = '5'

12  Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect?

A. String s3 = s1 - s2;
B. boolean b = s1.compareTo(s2);
C. char c = s1[0];
D. char c = s1.charAt(s1.length());

13  "abc".compareTo("aba") returns ___________.

A. 1
B. 2
C. -1
D. -2
E. 0

14  "AbA".compareToIgnoreCase("abC") returns ___________.

A. 1
B. 2
C. -1
D. -2
E. 0

15  ____________________ returns true.

A. "peter".compareToIgnoreCase("Peter")
B. "peter".compareToIgnoreCase("peter")
C. "peter".equalsIgnoreCase("Peter")
D. "peter".equalsIgnoreCase("peter")
E. "peter".equals("peter")

16  What is the return value of "SELECT".substring(0, 5)?

A. "SELECT"
B. "SELEC"
C. "SELE"
D. "ELECT"

17  What is the return value of "SELECT".substring(4, 4)?

A. an empty string
B. C
C. T
D. E

18  Analyze the following code.

class Test {
   public static void main(String[] args) {
     String s;
     System.out.println("s is " + s);
   }
}

A. The program has a compilation error because s is not initialized, but it is referenced in the println statement.
B. The program has a runtime error because s is not initialized, but it is referenced in the println statement.
C. The program has a runtime error because s is null in the println statement.
D. The program compiles and runs fine.

19  To check if a string s contains the prefix "Java", you may write


A. if (s.startsWith("Java")) ...
B. if (s.indexOf("Java") == 0) ...
C. if (s.substring(0, 4).equals("Java")) ...
D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...

20  To check if a string s contains the suffix "Java", you may write


A. if (s.endsWith("Java")) ...
B. if (s.lastIndexOf("Java") >= 0) ...
C. if (s.substring(s.length() - 4).equals("Java")) ...
D. if (s.substring(s.length() - 5).equals("Java")) ...
E. if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...

21  Which of the following is the correct statement to return JAVA?


A. toUpperCase("Java")
B. "Java".toUpperCase("Java")
C. "Java".toUpperCase()
D. String.toUpperCase("Java")

22  Which of the following is the correct statement to return a string from an array a of characters?


A. toString(a)
B. new String(a)
C. convertToString(a)
D. String.toString(a)

23  Assume s is " abc ", the method __________ returns a new string "abc".

A. s.trim(s)
B. trim(s)
C. String.trim(s)
D. s.trim()

24  Assume s is "ABCABC", the method __________ returns a new string "aBCaBC".

A. s.toLowerCase(s)
B. s.toLowerCase()
C. s.replace('A', 'a')
D. s.replace('a', 'A')
E. s.replace("ABCABC", "aBCaBC")

25  Assume s is "ABCABC", the method __________ returns an array of characters.

A. toChars(s)
B. s.toCharArray()
C. String.toChars()
D. String.toCharArray()
E. s.toChars()

26  __________ returns a string.

A. String.valueOf(123)
B. String.valueOf(12.53)
C. String.valueOf(false)
D. String.valueOf(new char[]{'a', 'b', 'c'})
E. String.copyValueOf(new char[]{'a', 'b', 'c'})

27  The following program displays __________.

public class Test {
   public static void main(String[] args) {
     String s = "Java";
     StringBuffer buffer = new StringBuffer(s);
     change(s);
     System.out.println(s);
   }
  
   private static void change(String s) {
     s = s + " and HTML";
   }
}


A. Java
B. Java and HTML
C. and HTML
D. nothing is displayed

Section 8.3 The Character Class
28  Which of following is not a correct method in Character?

A. isLetterOrDigit(char)
B. isLetter(char)
C. isDigit()
D. toLowerCase(char)
E. toUpperCase()

29  Suppose Character x = new Character('a'), __________________ returns true.

A. x.equals(new Character('a'))
B. x.compareToIgnoreCase('A')
C. x.equalsIgnoreCase('A')
D. x.equals('a')
E. x.equals("a")

Section 8.4 The StringBuilder/StringBuffer Class
30  Analyze the following code.

class Test {
   public static void main(String[] args) {
     StringBuffer strBuf = new StringBuffer(4);
     strBuf.append("ABCDE");
     System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5));
   }
}

A. The program has a compilation error because you cannot specify initial capacity in the StringBuffer constructor.
B. The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are appended into the buffer.
C. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.
D. The program compiles and runs fine.

31  Which of the following is true?

A. You can add characters into a string buffer.
B. You can delete characters into a string buffer.
C. You can reverse the characters in a string buffer.
D. The capacity of a string buffer can be automatically adjusted.

32  _________ returns the last character in a StringBuffer variable named strBuf?

A. strBuf.charAt(strBuf.length() - 1)
B. strBuf.charAt(strBuf.capacity() - 1)
C. StringBuffer.charAt(strBuf.length() - 1)
D. StringBuffer.charAt(strBuf.capacity() - 1)

33  Assume StringBuffer strBuf is "ABCDEFG", after invoking _________, strBuf contains "AEFG".

A. strBuf.delete(0, 3)
B. strBuf.delete(1, 3)
C. strBuf.delete(1, 4)
D. strBuf.delete(2, 4)

34  Assume StringBuffer strBuf is "ABCDEFG", after invoking _________, strBuf contains "ABCRRRRDEFG".

A. strBuf.insert(1, "RRRR")
B. strBuf.insert(2, "RRRR")
C. strBuf.insert(3, "RRRR")
D. strBuf.insert(4, "RRRR")

35  Assume StringBuffer strBuf is "ABCCEFC", after invoking _________, strBuf contains "ABTTEFT".

A. strBuf.replace('C', 'T')
B. strBuf.replace("C", "T")
C. strBuf.replace("CC", "TT")
D. strBuf.replace('C', "TT")
E. strBuf.replace(2, 7, "TTEFT")

36  The StringBuffer methods _____________ not only change the contents of a string buffer, but also returns a reference to the string buffer.

A. delete
B. append
C. insert
D. reverse
E. replace

37  The StringBuffer method _____________ does not return a reference to the string buffer.

A. substring
B. setCharAt
C. setLength
D. length
E. toString

38  The following program displays __________.

public class Test {
   public static void main(String[] args) {
     String s = "Java";
     StringBuffer buffer = new StringBuffer(s);
     change(buffer);
     System.out.println(buffer);
   }
  
   private static void change(StringBuffer buffer) {
     buffer.append(" and HTML");
   }
}


A. Java
B. Java and HTML
C. and HTML
D. nothing is displayed

Section 8.5 Command-Line Arguments
39  How can you get the word "abc" in the main method from the following call?

java Test "+" 3 "abc" 2

A. args[0]
B. args[1]
C. args[2]
D. args[3]

40  Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

A. int count = args.length;
B. int count = args.length - 1;
C. int count = 0; while (args[count] != null) count ++;
D. int count=0; while (!(args[count].equals(""))) count ++;

41  Which correctly creates an array of five empty Strings?

A. String[] a = new String [5];
B. String[] a = {"", "", "", "", ""};
C. String[5] a;
D. String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

42  Identify the problems in the following code.
                
public class Test {
   public static void main(String argv[]) {
     System.out.println("argv.length is " + argv.length);
   }
}

A. The program has a syntax error because String argv[] is wrong and it should be replaced by String[] args.
B. The program has a syntax error because String args[] is wrong and it should be replaced by String args[].
C. If you run this program without passing any arguments, the program would have a runtime error because argv is null.
D. If you run this program without passing any arguments, the program would display argv.length is 0.

43  Which of the following is the correct header of the main method?

A. public static void main(String[] args)
B. public static void main(String args[])
C. public static void main(String[] x)
D. public static void main(String x[])
E. static void main(String[] args)

Section 8.6 (Optional) Regular Expressions
44  Which of the following will return true?

A. "Java is fun".equals("Java.*")
B. "Java is fun".matches("Java.*")
C. "Java is cool".equals("Java.*")
D. "Java is powerful".matches("Java.*")

45  Which of the following will return true?

A. "Java is neat".matches("\\*is\\*")
B. "Java is fun".matches(".*")
C. "Java is cool".matches(".+")
D. "Java is powerful".matches(".?is.?")

46  Which of the following will return true?

A. "Java is neat".matches("\\D*")
B. "Java is fun".matches("\\d*")
C. "Java is cool".matches("\\s*")
D. "Java is powerful".matches("\\S*")
E. "Java is powerful".matches("[\\w\\s]*")

47  Which of the following will return true?

A. "Java is neat".matches("\\D*")
B. "Java is fun".matches("\\d*")
C. "Java is cool".matches("\\s*")
D. "Java is powerful".matches("\\S*")
E. "Java is powerful".matches("[\\w\\s]*")

48  Which of the following will return true?

A. "Java is neat".matches("Java.{3}neat")
B. "Java is neat".matches("Java.{4}neat")
C. "Java is fun".matches("Java.{4,}fun")
D. "Java is fun".matches("Java.{5,}fun")
E. "Java is fun".matches("Java.{4,6}fun")

49  What is displayed by the following statement?
         System.out.println("Java is neat".replaceAll("is", "AAA"));

A. JavaAAAneat
B. JavaAAA neat
C. Java AAA neat
D. Java AAAneat

50  What is displayed by the following statement?
         System.out.println("Java is neat".replaceFirst("[\\s]", "BB"));

A. JavaBBis neat
B. JavaBBisBBneat
C. Java is neat
D. Javaisneat

51  What is displayed by the following code?
   public static void main(String[] args) throws Exception {
     String[] tokens = "Welcome to Java".split("o");
     for (int i = 0; i < tokens.length; i++) {
       System.out.print(tokens[i] + " ");
     }
   }

A. Welcome to Java
B. Welc me to Java
C. Welc me t Java
D. Welcome t Java

52  What is displayed by the following code?
   public static void main(String[] args) throws Exception {
     String[] tokens = "Welcome to Java".split("[o|a]");
     for (int i = 0; i < tokens.length; i++) {
       System.out.print(tokens[i] + " ");
     }
   }

A. Welc me t J v
B. Welc me to J va
C. Welc me t Jav
D. Welcome t ava

Section 8.7 The File Class
53  What are the reasons to create an instance of the File class?

A. To determine whether the file exist.
B. To obtain the properties of the file such as whether the file can be read, written, or is hidden.
C. To rename the file.
D. To delete the file.
E. To read/write data from/to a file

54  Which of the following returns the path separator character?

A. File.pathSeparator
B. File.pathSeparatorChar
C. File.separator
D. File.separatorChar
E. None of the above.

55  Which of the following statements creates an instance of File on Window for the file c:\t.txt?

A. new File("c:\txt.txt")
B. new File("c:\\txt.txt")
C. new File("c:/txt.txt")
D. new File("c://txt.txt")

56  Which of the following statements are true?

A. If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") returns null.
B. If a directory (e.g., c:\liang) does not exist, new File("c:\liang") returns null.
C. If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") creates a new file named c:\temp.txt.
D. If a directory (e.g., c:\liang) does not exist, new File("c:\liang") creates a new directory named c:\liang.
E. None of the above.

Section 8.8 Text I/O
57  Which class contains the method for checking whether a file exist?

A. File
B. PrintWriter
C. Scanner
D. System

58  Which class do you use to write data into a text file?

A. File
B. PrintWriter
C. Scanner
D. System

59  Which class do you use to read data into a text file?

A. File
B. PrintWriter
C. Scanner
D. System

60  Which method can be use to write data?

A. close
B. print
C. exist
D. rename

61  Which method can be use to read a whole line from the file?

A. next
B. nextLine
C. nextInt
D. nextDouble

62  Which method can be use to create an input object for file temp.txt?

A. new Scanner("temp.txt")
B. new Scanner(temp.txt)
C. new Scanner(new File("temp.txt"))
D. new Scanner(File("temp.txt"))

63  Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner scanner = new Scanner(System.in);
int intValue = scanner.nextInt();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();


A. After the last statement is executed, intValue is 34.
B. The program has a runtime error because 34.3 is not an integer.
C. After the last statement is executed, line contains characters '7 ', '8 ', '9?, '\n '.
D. After the last statement is executed, line contains characters '7 ', '8 ', '9?.

64  Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner scanner = new Scanner(System.in);
int value = scanner.nextDouble();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();


A. After the last statement is executed, intValue is 34.
B. The program has a runtime error because 34.3 is not an integer.
C. After the last statement is executed, line contains characters '7 ', '8 ', '9?, '\n '.
D. After the last statement is executed, line contains characters '7 ', '8 ', '9?.

65  Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key, 789, the ENTER key. Analyze the following code.
Scanner scanner = new Scanner(System.in);
int value = scanner.nextDouble();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();


A. After the last statement is executed, intValue is 34.
B. The program has a runtime error because 34.3 is not an integer.
C. After the last statement is executed, line contains characters '7 ', '8 ', '9?, '\n '.
D. After the last statement is executed, line contains characters '7 ', '8 ', '9?.
E. After the last statement is executed, line contains character '\n '.

66  Which method can be use to create an output object for file temp.txt?
A. new PrintWriter("temp.txt")
B. new PrintWriter(temp.txt)
C. new PrintWriter(new File("temp.txt"))
D. new PrintWriter(File("temp.txt"))