Assignment CH 9
Assignment CH 9
Assignment CH 9
char x = s.charAt(4);
A. 'a'
B. 'v'
C. Nothing will be assigned to x, because the execution causes the runtime error
StringIndexOutofBoundsException.
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");
}
}
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");
}
}
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");
}
}
6 Suppose s1 and s2 are two strings. What is the result of the following code?
s1.equals(s2) == s2.equals(s1)
A. true
B. false
if (s1.equals(s2))
System.out.println("s1 and s2 have the same contents");
else
System.out.println("s1 and s2 have different contents");
}
}
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");
}
}
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");
}
}
10 Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
B. String s3 = s1 + s2
C. s1 >= s2
D. int i = s1.length
E. s1.charAt(0) = '5'
11 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());
A. 1
B. 2
C. -1
D. -2
E. 0
A. 1
B. 2
C. -1
D. -2
E. 0
A. "peter".compareToIgnoreCase("Peter")
B. "peter".compareToIgnoreCase("peter")
C. "peter".equalsIgnoreCase("Peter")
D. "peter".equalsIgnoreCase("peter")
E. "peter".equals("peter")
String s = "University";
s.replace("i", "ABC");
System.out.println(s);
A. UnABCversity
B. UnABCversABCty
C. UniversABCty
D. University
A. "SELECT"
B. "SELEC"
C. "SELE"
D. "ELECT"
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.
A. if (s.startsWith("Java")) ...
B. if (s.indexOf("Java") == 0) ...
D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...
A. if (s.endsWith("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()
A. s.toLowerCase(s)
B. s.toLowerCase()
C. s.replace('A', 'a')
D. s.replace('a', 'A')
E. s.replace("ABCABC", "aBCaBC")
A. toChars(s)
B. s.toCharArray()
C. String.toChars()
D. String.toCharArray()
E. s.toChars()
A. String.valueOf(123)
B. String.valueOf(12.53)
C. String.valueOf(false)
A. Java
C. and HTML
D. nothing is displayed
A. JavaAAAneat
B. JavaAAA neat
D. Java AAAneat
A. Welcome to Java
B. Welc me to Java
C. Welc me t Java
D. Welcome t Java
A. false fasle
B. true fasle
C. true true
D. false true
A. A B C A#B#C
B. A#B#C A#B#C
C. A,B;C A#B#C
D. A B C A B C
32 What is displayed by the following code?
A. A,B;C;D
B. A B C D
C. A B C;D
D. A B;C;D
A. isLetterOrDigit(char)
B. isLetter(char)
C. isDigit()
D. toLowerCase(char)
E. toUpperCase()
A. x.equals(new Character('a'))
B. x.compareToIgnoreCase('A')
C. x.equalsIgnoreCase('A')
D. x.equals('a')
E. x.equals("a")
class Test {
public static void main(String[] args) {
StringBuilder strBuf = new StringBuilder(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 StringBuilder
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.
A. strBuf.charAt(strBuf.length() - 1)
B. strBuf.charAt(strBuf.capacity() - 1)
C. StringBuilder.charAt(strBuf.length() - 1)
D. StringBuilder.charAt(strBuf.capacity() - 1)
38 Assume StringBuilder 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)
A. strBuf.insert(1, "RRRR")
B. strBuf.insert(2, "RRRR")
C. strBuf.insert(3, "RRRR")
D. strBuf.insert(4, "RRRR")
40 Assume StringBuilder 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")
41 The StringBuilder 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
A. Java
C. and HTML
D. nothing is displayed
A. args[0]
B. args[1]
C. args[2]
D. args[3]
java Test 1 2 3
A. 3
B. 1
C. 1 2 3
D. 1 2
45 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?
C. String[5] a;
A. The program has a compile error because String argv[] is wrong and it should be replaced by String[] args.
B. The program has a compile 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.
B. To obtain the properties of the file such as whether the file can be read, written, or is hidden.
A. File.pathSeparator
B. File.pathSeparatorChar
C. File.separator
D. File.separatorChar
51 Which of the following statements creates an instance of File on Window for the file c:\temp.txt?
A. new File("c:\temp.txt")
B. new File("c:\\temp.txt")
C. new File("c:/temp.txt")
D. new File("c://temp.txt")
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.
A. File
B. PrintWriter
C. Scanner
D. System
A. File
B. PrintWriter
C. Scanner
D. System
A. File
B. PrintWriter
C. Scanner
D. System
A. close
B. print
C. exist
D. rename
57 Which method can be used to read a whole line from the file?
A. next
B. nextLine
C. nextInt
D. nextDouble
58 Which method can be used to create an input object for file temp.txt?
A. new Scanner("temp.txt")
B. new Scanner(temp.txt)
D. new Scanner(File("temp.txt"))
59 Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner input = new Scanner(System.in);
int v1 = input.nextInt();
int v2 = input.nextInt();
String line = input.nextLine();
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'.
60 Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner input = new Scanner(System.in);
double v1 = input.nextDouble();
double v2 = input.nextDouble();
String line = input.nextLine();
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'.
61 Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key. Analyze the following code.
Scanner input = new Scanner(System.in);
double v1 = input.nextDouble();
double v2 = input.nextDouble();
String line = input.nextLine();
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 '.
62 Which method can be used to create an output object for file temp.txt?
A. new PrintWriter("temp.txt")
B. new PrintWriter(temp.txt)
D. new PrintWriter(File("temp.txt"))