单选题Given this method in a class:  public String toString() {  StringBuffer buffer = new StringBuffer();  buffer.append(‟‟);  return buffer.toString();  }  Which is true?()AThis code is NOT thread-safe.BThe programmer can replace StringBuffer with StringBu

题目
单选题
Given this method in a class:  public String toString() {  StringBuffer buffer = new StringBuffer();  buffer.append(‟‟);  return buffer.toString();  }  Which is true?()
A

 This code is NOT thread-safe.

B

 The programmer can replace StringBuffer with StringBuilder with no other changes.

C

 This code will perform well and converting the code to use StringBuilder will not enhance the performance.

D

 This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”;


相似考题
更多“单选题Given this method in a class:  public String toString() {  StringBuffer buffer = new StringBuffer();  buffer.append(‟‟);  return buffer.toString();  }  Which is true?()A  This code is NOT thread-safe.B  The programmer can replace StringBuffer with Stri”相关问题
  • 第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


    正确答案:C
    解析:StringBuffer类的length()函数是求出字符序列的长度。

  • 第2题:

    下面是一段javabean程序,该程序的运行结果是( )。public class NullTest{public static void main(String[]?args){int M=0;String str=null;StringBuffer sb=new StringBuffer("=");sb.append(str);sb.append(M++);System.out.println(sb.toString( ));}}

    A.=null
    B.=null0
    C.=null1
    D.=nullM

    答案:B
    解析:
    本题考查学生对javabean程序的熟悉程度,尤其是数值类型数据和字符串类型数据的掌握情况。M是整型变量,其值为0,str是字符串,sb是字符串空间,其中存放字符“=”,append是字符串添加函数,M++为自增运算符,它的特点是先取M的值作为表达式的值,再进行自增运算。程序的运算过程是:先将null拼接到“=”的后面,得字符串“=null”,再将0作为字符拼接到“=null”的后面,得“=null0”,M自增为1,输出结果为:=null0。

  • 第3题:

    Whichtwo scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?()

    • A、When using versions of Java technology earlier than 5.0.
    • B、When sharing a StringBuffer among multiple threads.
    • C、When using the java.io class StringBufferInputStream.
    • D、When you plan to reuse the StringBuffer to build more than one string.
    • E、Enitiation of separate design processes to the separation of users

    正确答案:A,B

  • 第4题:

    String与StringBuffer的区别()。

    • A、String是不可变的对象,StringBuffer是可以再编辑的
    • B、String是常量,StringBuffer是变量
    • C、String是可变的对象,StringBuffer是不可以再编辑的
    • D、以上说法都不正确

    正确答案:A,B

  • 第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?() 

    • A、 This code is NOT thread-safe.
    • B、 The programmer can replace StringBuffer with StringBuilder with no other changes.
    • C、 This code will perform well and converting the code to use StringBuilder will not enhance the performance.
    • D、 This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”;

    正确答案:B

  • 第6题:

    Which methods from the String and StringBuffer classes modify the object on which they are called?()  

    • A、The charAt() method of the String class.
    • B、The toUpperCase() method of the String class.
    • C、The replace() method of the String class.
    • D、The reverse() method of the StringBuffer class.
    • E、The length() method of the StringBuffer class.

    正确答案:D

  • 第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? ()

    • A、 The program prints “ABBCAD”
    • B、 The program prints “CDDACB”
    • C、 The program prints “ADCBADBC”
    • D、 The output is a non-deterministic point because of a possible deadlock condition.
    • E、 The output is dependent on the threading model of the system the program is running on.

    正确答案:B,D

  • 第8题:

    Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?()

    • A、When using versions of Java technology earlier than 5.0.
    • B、When sharing a StringBuffer among multiple threads.
    • C、When using the java.io class StringBufferInputStream.
    • D、When you plan to reuse the StringBuffer to build more than one string.

    正确答案:A,B

  • 第9题:

    填空题
    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?()

    正确答案: JAVAJAVA
    解析: 暂无解析

  • 第10题:

    单选题
    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?()
    A

     synchronize the log method

    B

     replace StringBuilder with StringBuffer

    C

     No change is necessary, the current MyLogger code is already thread-safe.

    D

     replace StringBuilder with just a String object and use the string concatenation (+=) within the log method


    正确答案: A
    解析: 暂无解析

  • 第11题:

    单选题
    Which methods from the String and StringBuffer classes modify the object on which they are called?()
    A

    The charAt() method of the String class.

    B

    The toUpperCase() method of the String class.

    C

    The replace() method of the String class.

    D

    The reverse() method of the StringBuffer class.

    E

    The length() method of the StringBuffer class.


    正确答案: D
    解析: 暂无解析

  • 第12题:

    多选题
    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?()
    A

    Class a will not compile.

    B

    Line 46 can throw the unchecked exception TestException.

    C

    Line 45 can throw the unchecked exception TestException.

    D

    Line 46 will compile if the enclosing method throws a TestException.

    E

    Line 46 will compile if enclosed in a try block, where TestException is caught.


    正确答案: D,E
    解析: 暂无解析

  • 第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);


    正确答案:abstract Builder extends items.length construct()
    abstract Builder extends items.length construct()

  • 第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?()

    • A、 Class a will not compile.
    • B、 Line 46 can throw the unchecked exception TestException.
    • C、 Line 45 can throw the unchecked exception TestException.
    • D、 Line 46 will compile if the enclosing method throws a TestException.
    • E、 Line 46 will compile if enclosed in a try block, where TestException is caught.

    正确答案:D,E

  • 第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?()

    • A、 String s = “123456789”; s = (s-”123”).replace(1,3,”24”) - “89”;
    • B、 StringBuffer s = new StringBuffer(”123456789”); s.delete(0,3).replace( 1,3, “24”).delete(4,6);
    • C、 StringBuffer s = new StringBuffer(”123456789”); s.substring(3,6).delete( 1 ,3).insert(1, “24”);
    • D、 StringBuilder s = new StringBuilder(”123456789”); s.substring(3,6).delete( 1 ,2).insert( 1, “24”);
    • E、 StringBuilder s = new StringBuilder(”123456789”); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);

    正确答案:B,E

  • 第16题:

    关于String和StringBuffer,下面那些是正确的:()

    • A、常量字符串使用String,非常量字符串使用StringBuffer。
    • B、使用StringBuffer的时候设置初始容量。
    • C、尽量使用StringTokenizer代替indexOf()和substring()。
    • D、尽量不要使用StringBuffer,StringTokenizer类。

    正确答案:A,B,C

  • 第17题:

    public class Score implements Comparable {  private int wins, losses;  public Score(int w, int 1) { wins = w; losses = 1; }  public int getWins() { return wins; }  public int getLosses() { return losses; }  public String toString() {  return “<“ + wins + “,“ + losses + “>”; }  // insert code here  }  Which method will complete this class?() 

    • A、 public int compareTo(Object o) {/*mode code here*/}
    • B、 public int compareTo(Score other) {/*more code here*/}
    • C、 public int compare(Score s1,Score s2){/*more code here*/}
    • D、 public int compare(Object o1,Object o2){/*more code here*/}

    正确答案:B

  • 第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?()


    正确答案:javajavaC

  • 第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?() 

    • A、 synchronize the log method
    • B、 replace StringBuilder with StringBuffer
    • C、 No change is necessary, the current MyLogger code is already thread-safe.
    • D、 replace StringBuilder with just a String object and use the string concatenation (+=) within the log method

    正确答案:A

  • 第20题:

    单选题
    Given this method in a class:  public String toString() {  StringBuffer buffer = new StringBuffer();  buffer.append(‟‟);  return buffer.toString();  }  Which is true?()
    A

     This code is NOT thread-safe.

    B

     The programmer can replace StringBuffer with StringBuilder with no other changes.

    C

     This code will perform well and converting the code to use StringBuilder will not enhance the performance.

    D

     This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”;


    正确答案: B
    解析: 暂无解析

  • 第21题:

    多选题
    关于String和StringBuffer,下面那些是正确的:()
    A

    常量字符串使用String,非常量字符串使用StringBuffer。

    B

    使用StringBuffer的时候设置初始容量。

    C

    尽量使用StringTokenizer代替indexOf()和substring()。

    D

    尽量不要使用StringBuffer,StringTokenizer类。


    正确答案: C,A
    解析: 暂无解析

  • 第22题:

    单选题
    public class Score implements Comparable {  private int wins, losses;  public Score(int w, int 1) { wins = w; losses = 1; }  public int getWins() { return wins; }  public int getLosses() { return losses; }  public String toString() {  return “”; }  // insert code here  }  Which method will complete this class?()
    A

     public int compareTo(Object o) {/*mode code here*/}

    B

     public int compareTo(Score other) {/*more code here*/}

    C

     public int compare(Score s1,Score s2){/*more code here*/}

    D

     public int compare(Object o1,Object o2){/*more code here*/}


    正确答案: B
    解析: 暂无解析

  • 第23题:

    多选题
    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?()
    A

    String s = “123456789”; s = (s-”123”).replace(1,3,”24”) - “89”;

    B

    StringBuffer s = new StringBuffer(”123456789”); s.delete(0,3).replace( 1,3, “24”).delete(4,6);

    C

    StringBuffer s = new StringBuffer(”123456789”); s.substring(3,6).delete( 1 ,3).insert(1, “24”);

    D

    StringBuilder s = new StringBuilder(”123456789”); s.substring(3,6).delete( 1 ,2).insert( 1, “24”);

    E

    StringBuilder s = new StringBuilder(”123456789”); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);


    正确答案: D,A
    解析: 暂无解析

  • 第24题:

    多选题
    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? ()
    A

    The program prints “ABBCAD”

    B

    The program prints “CDDACB”

    C

    The program prints “ADCBADBC”

    D

    The output is a non-deterministic point because of a possible deadlock condition.

    E

    The output is dependent on the threading model of the system the program is running on.


    正确答案: E,D
    解析: 暂无解析