This code is NOT thread-safe.
The programmer can replace StringBuffer with StringBuilder with no other changes.
This code will perform well and converting the code to use StringBuilder will not enhance the performance.
This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”;
第1题:
执行下列程序后,输出结果为( )。 public class Test { public static void main (String[] args) { StringBuffer sb = new StringBuffer("北京 2008" ); System. out. println ("length =" + sb. length ( ) ); } }
A.length = 8
B.length = 10
C.length = 6
D.length = 20
第2题:
第3题:
Whichtwo scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?()
第4题:
String与StringBuffer的区别()。
第5题:
Given this method in a class: public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(‟<‟); buffer.append(this.name); buffer.append(‟>‟); return buffer.toString(); } Which is true?()
第6题:
Which methods from the String and StringBuffer classes modify the object on which they are called?()
第7题:
public class SyncTest{ public static void main(String args) { final StringBuffer s1= new StringBuffer(); final StringBuffer s2= new StringBuffer(); new Thread () { public void run() { synchronized(s1) { s2.append(“A”); synchronized(s2) { s2.append(“B”); System.out.print(s1); System.out.print(s2); } } } }.start(); new Thread() { public void run() { synchronized(s2) { s2.append(“C”); synchronized(s1) { s1.append(“D”); System.out.print(s2); System.out.print(s1); } } } }.start(); } } Which two statements are true? ()
第8题:
Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?()
第9题:
第10题:
synchronize the log method
replace StringBuilder with StringBuffer
No change is necessary, the current MyLogger code is already thread-safe.
replace StringBuilder with just a String object and use the string concatenation (+=) within the log method
第11题:
The charAt() method of the String class.
The toUpperCase() method of the String class.
The replace() method of the String class.
The reverse() method of the StringBuffer class.
The length() method of the StringBuffer class.
第12题:
Class a will not compile.
Line 46 can throw the unchecked exception TestException.
Line 45 can throw the unchecked exception TestException.
Line 46 will compile if the enclosing method throws a TestException.
Line 46 will compile if enclosed in a try block, where TestException is caught.
第13题:
阅读以下说明和Java代码,回答问题
[说明]
在某些系统中,存在非常复杂的对象,可以采用循序渐进的方式进行组合将小对象组合,成复杂的对象。
以下实例展示了Builder(生成器)模式。该实例用来建立“文件”,文件内容包括:一个标题、一串字符以及一些有项目符号的项目。Builder类规定组成文件的方法,Director类利用这个方法产生一份具体的文件。图6-1显示了各个类间的关系。
以下是Java语言实现,能够正确编译通过。
[Java代码]
//Builder. java文件
public (1) class Builder {
public abstract void makeTitle(String title);
public abstract void makeString(String str);
public abstract void makeItems(String[] items);
public abstract Object getResult();
}
//Director. java文件
public class Director{
private (2) builder;
public Director(Builder builder){
this. builder = builder;
}
public Object construct(){
builder.makeTitle("Greeting");
builder.makeString("从早上到白天结束");
builder.makeItems(new String[]{"早安", "午安",});
builder.makeString("到了晚上");
builder.makeItems(new String[]("晚安", "好梦",});
return builder.getResult();
}
}
//TextBuilder.java文件
public class TextBuilder (3) Builder{
private StringBuffer buffer = new StringBuffer();
public void makeTitle(String title){
buffer.append("『" + title + "』"\n\n");
}
public void makeString(String str){
buffer.append('■' + str + "\n\n ");
}
public void makeItems(String[] items){
for(int i = 0; i< (4) ; i++){
buffer.append('·' + items[i] + "\n");
}
buffer.append("\n");
}
public Object getResult(){
return buffer.toString();
}
}
//Main.java文件
public class Main {
public static void main(String[] args) {
Director director = new Director(new TextBuilder());
String result = (String)director. (5) ;
System.out.println(result);
第14题:
Class TestException 1. public class TestException extends Exception { 2. } Class a: 1. public class a { 2. 3. public String sayHello(String name) throws TestException { 4. 5. if(name == null) { 6. throw new TestException(); 7. } 8. 9. return “Hello “+ name; 10. } 11. 12. } A programmer wants to use this code in an application: 45. A a=new A(); 46. System.out.println(a.sayHello(”John”)); Which two are true?()
第15题:
public class TestString3 { public static void main(String[] args) { // insert code here System.out.println(s); } } Which two code fragments, inserted independently at line 3, generate the output 4247?()
第16题:
关于String和StringBuffer,下面那些是正确的:()
第17题:
public class Score implements Comparable
第18题:
Public class test ( Public static void stringReplace (String text) ( Text = text.replace (‘j’ , ‘i’); ) public static void bufferReplace (StringBuffer text) ( text = text.append (“C”) ) public static void main (String args[]} ( String textString = new String (“java”); StringBuffer text BufferString = new StringBuffer (“java”); stringReplace (textString); bufferReplace (textBuffer); System.out.printLn (textString + textBuffer); ) ) What is the output?()
第19题:
public class MyLogger { private StringBuilder logger = new StringBuuilder(); public void log(String message, String user) { logger.append(message); logger.append(user); } } The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system. How must this code be changed to be thread-safe?()
第20题:
This code is NOT thread-safe.
The programmer can replace StringBuffer with StringBuilder with no other changes.
This code will perform well and converting the code to use StringBuilder will not enhance the performance.
This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”;
第21题:
常量字符串使用String,非常量字符串使用StringBuffer。
使用StringBuffer的时候设置初始容量。
尽量使用StringTokenizer代替indexOf()和substring()。
尽量不要使用StringBuffer,StringTokenizer类。
第22题:
public int compareTo(Object o) {/*mode code here*/}
public int compareTo(Score other) {/*more code here*/}
public int compare(Score s1,Score s2){/*more code here*/}
public int compare(Object o1,Object o2){/*more code here*/}
第23题:
String s = “123456789”; s = (s-”123”).replace(1,3,”24”) - “89”;
StringBuffer s = new StringBuffer(”123456789”); s.delete(0,3).replace( 1,3, “24”).delete(4,6);
StringBuffer s = new StringBuffer(”123456789”); s.substring(3,6).delete( 1 ,3).insert(1, “24”);
StringBuilder s = new StringBuilder(”123456789”); s.substring(3,6).delete( 1 ,2).insert( 1, “24”);
StringBuilder s = new StringBuilder(”123456789”); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);
第24题:
The program prints “ABBCAD”
The program prints “CDDACB”
The program prints “ADCBADBC”
The output is a non-deterministic point because of a possible deadlock condition.
The output is dependent on the threading model of the system the program is running on.