单选题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

题目
单选题
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 + “>”;

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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

第2题:

执行下列程序后,输出结果为( )。 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()函数是求出字符序列的长度。

第3题:

( 20 )请阅读下面程序

public class ExampleStringBuffer{

public static void main ( String[] args ){

StringBuffer sb=new StringBuffer ( "test" ) ;

System.out.println ( "buffer= "+sb ) ;

System.out.println ( "length= "+sb.length ()) ; }}

程序运行结果中在 "length=" 后输出的值是

A ) 10

B ) 4

C ) 20

D ) 30


正确答案:B

第4题:

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

第5题:

下面是一段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。

第6题:

Given:Which code, inserted at line 15, allows the class Sprite to compile?()

A.Foo { public int bar() { return 1; }

B.new Foo { public int bar() { return 1; }

C.new Foo() { public int bar() { return 1; }

D.new class Foo { public int bar() { return 1; }


参考答案:C

第7题:

阅读以下说明和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()

第8题:

请阅读下面程序 public class ExampleStringBuffer{ public static void main(String []args){ StringBuffer sb=new StringBuffer("test"); System.out.println("buffer="+sB) ; System.out.println("length="+sb.length());} } 程序运行结果中在"length="后输出的值

A.10

B.4

C.20

D.30


正确答案:B
解析:本题考查Java中字符串的知识。Java中,可以用StringBuffer类表示字符串,
  StringBuffer用于处理长度可变字符串。StringBuffer类提供了三种构造方法:
  String strObj=new StringBuffer();
  String strObj=new StringBuffer(int length);
  String strObj=new StringBuffer(String str);
  本题程序中使用的是第三种构造方法来创建一个字符串对象。对StringBuffer(String str)构造方法,用str给出字符串的初始值,并分配16个字符的缓存。因此,字符串sb的初始值是“test”,并且包含16个字符的缓存。iength()方法用来获得字符串长度,不包含缓存。故程序运行结果中在“lensgth=”后输出的值应该是字符串sb的长度,即4。本题的正确答案是选项B。

第9题:

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

第10题:

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

更多相关问题