niusouti.com

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

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


相似考题
更多“public class MyLogger {  private StringBuilder logger = new ”相关问题
  • 第1题:

    阅读下列说明和 Java 代码,将应填入(n)处的字句写在答题纸的对应栏内。
    【说明】
    生成器( Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图 6-1 所示为其类图。



    阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。

    【说明】

    ???? 生成器(Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图5-1所示为其类图。

    ?

    【C++代码】

    #include

    #include

    using namespace std;
    class Product {
    private:?
    string partA, partB;
    public:?
    Product() {?? }? ?
    void setPartA(const string& s) { PartA = s;}
    ???? void setPartB(const string& s) { PartB
    = s;}? ?
    //? 其余代码省略
    };
    class Builder {
    public:? ? ??
    (1)??
    ;?
    virtual void buildPartB()=0;? ? ?
    (2)??
    ;
    };
    class ConcreteBuilder1 : public Builder {
    private:?
    Product*?? product;
    public:
    ConcreteBuilder1() {product = new Product();???? }
    void buildPartA() {????? (3)???? ("Component
    A"); }?
    void buildPartB() {????? (4)???? ("Component
    B"); }??
    Product* getResult() { return product; }
    //? 其余代码省略
    };
    class ConcreteBuilder2 : public Builder {? ??? ? ? ?
    /*??? 代码省略??? */
    };
    class Director {
    private:? ??
    Builder* builder;
    public:??
    Director(Builder* pBuilder) { builder= pBuilder;}? ??
    void construct() {
    ????????????????? (5)???? ;
    ?????????????? //? 其余代码省略? ?
    }??
    //? 其余代码省略
    };
    int main() {? ? ??
    Director* director1 = new Director(new ConcreteBuilder1());? ?
    director1->construct();? ? ??
    delete director1;? ? ?
    return 0;
    【Java代码】
    import jav(6)A.util.*;
    class Product {? ? ? ?
    private String partA;? ? ? ?
    private String partB;? ? ? ??
    public Product() {}? ? ??
    public void setPartA(String s) { partA = s; }? ? ? ?
    public void setPartB(String s) { partB = s; }
    }
    interface Builder {? ?
    public?????? (1)???? ;? ??
    public void buildPartB();? ? ??
    public?????? (2)???? ;
    }
    class ConcreteBuilder1 implements Builder {? ? ? ?
    private Product product;? ? ? ?
    public ConcreteBuilder1() { product = new Product();?? }? ? ? ??
    public void buildPartA() {????????
    (3)??
    ("Component A"); }
    public void buildPartB() {???? ????(4)?? ("Component B"); }? ? ??
    public Product getResult() { return product;}
    }
    class ConcreteBuilder2 implements Builder {?? ? ? ? ?
    //? 代码省略
    }
    class Director {? ? ? ?
    private Builder builder;? ? ? ?
    public Director(Builder builder) {this.builder = builder; }
    public void construct() {
    ? ? ? ? ? ? ? ? ? (5)???? ;
    ? ? ? ? ? ? ? //? 代码省略? ? ??
    }
    }
    class Test {? ? ??
    public static void main(String[] args) {
    ???????????????? Director director1 = new
    Director(new ConcreteBuilder1());
    ???????????????? director1.construct();? ? ? ??
    }


    答案:
    解析:
    (1)void buildPart A()
    (2) Product getResult()
    (3)product.setPartA
    (4)product.setPartB
    (5)builder.buildPartA();
    builder.buildPartB();
    Product p=builder.getResult();

  • 第2题:

    1. public class Target {  2. private int i = 0;  3. public int addOne() {  4. return ++i;  5. }  6. }  And:  1. public class Client {  2. public static void main(String[] args) {  3. System.out.println(new Target().addOne());  4. }  5. }  Which change can you make to Target without affecting Client?() 

    • A、 Line 4 of class Target can be changed to return i++;
    • B、 Line 2 of class Target can be changed to private int i = 1;
    • C、 Line 3 of class Target can be changed to private int addOne() {
    • D、 Line 2 of class Target can be changed to private Integer i = 0;

    正确答案:D

  • 第3题:

    Which three demonstrate an “is a” relationship?() 

    • A、 public class X {  }     public class Y extends X { }
    • B、 public interface Shape { }     public interface Rectangle extends Shape{ }
    • C、 public interface Color { }     public class Shape { private Color color; }
    • D、 public interface Species { }     public class Animal { private Species species; }
    • E、 public class Person { }    public class Employee {      public Employee(Person person) { }
    • F、 interface Component { }     class Container implements Component {   private Component[] children; }

    正确答案:A,B,F

  • 第4题:

    public class Employee{   private String name;   public Employee(String name){   this.name = name;  }   public void display(){   System.out.print(name);  }  }   public class Manager extends Employee{   private String department;   public Manager(String name,String department){   super(name);   this.department = department;  }   public void display(){   System.out.println( super.display()+”,”+department);  }   }   执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?() 

    • A、 smith,SALES
    • B、 null,SALES
    • C、 smith,null
    • D、 null,null

    正确答案:A

  • 第5题:

    Which the two demonstrate an “is a” relationship?()

    • A、 public interface Person {}  Public class Employee extends Person {}
    • B、 public interface Shape {}  public interface Rectangle extends Shape {}
    • C、 public interface Color {}  public class Shape { private Color color; }
    • D、 public class Species {}  public class Animal { private Species species; }
    • E、 interface Component {} Class Container implements Component {private Component [] children;

    正确答案:B,E

  • 第6题:

    Given the following code:     1) class Parent {     2) private String name;     3) public Parent(){}     4) }  5) public class Child extends Parent {     6) private String department;  7) public Child() {}  8) public String getValue(){ return name; }     9) public static void main(String arg[]) {     10) Parent p = new Parent();     11) }  12) }  Which line will cause error?()   

    • A、 line 3
    • B、 line 6
    • C、 line 7
    • D、 line 8
    • E、 line 10

    正确答案:D

  • 第7题:

    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()

    • A、 public class Circle implements Shape { private int radius; }
    • B、 public abstract class Circle extends Shape { private int radius; }
    • C、 public class Circle extends Shape { private int radius; public void draw(); }
    • D、 public abstract class Circle implements Shape { private int radius; public void draw(); }
    • E、 public class Circle extends Shape { private int radius;public void draw() {/* code here */} }
    • F、 public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }

    正确答案:B,E

  • 第8题:

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


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

  • 第9题:

    单选题
    public class Pet{     private String name;     public Pet(){  System.out.print(1);    }  public Pet(String name){        System.out.print(2);    } }  public class Dog extends Pet{     public Dog(String name){        System.out.print(3);    } }  执行new Dog(“棕熊”);后程序输出是哪项?()
    A

     23

    B

     13

    C

     123

    D

     321


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

  • 第10题:

    多选题
    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()
    A

    public class Circle implements Shape { private int radius; }

    B

    public abstract class Circle extends Shape { private int radius; }

    C

    public class Circle extends Shape { private int radius; public void draw(); }

    D

    public abstract class Circle implements Shape { private int radius; public void draw(); }

    E

    public class Circle extends Shape { private int radius;public void draw() {/* code here */} }

    F

    public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }


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

  • 第11题:

    多选题
    Which two demonstrate an “is a” relationship?()
    A

    public interface Person { }  public class Employee extends Person { }

    B

    public interface Shape { }  public class Employee extends Shape { }

    C

    public interface Color { }  public class Employee extends Color { }

    D

    public class Species { }  public class Animal (private Species species;)

    E

    interface Component { }  Class Container implements Component ( Private Component[ ] children;  )


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

  • 第12题:

    单选题
    Given the following code:     1) class Parent {     2) private String name;     3) public Parent(){}     4) }  5) public class Child extends Parent {     6) private String department;  7) public Child() {}  8) public String getValue(){ return name; }     9) public static void main(String arg[]) {     10) Parent p = new Parent();     11) }  12) }  Which line will cause error?()
    A

     line 3

    B

     line 6

    C

     line 7

    D

     line 8

    E

     line 10


    正确答案: E
    解析: 第8行的getValue()试图访问父类的私有变量,错误。

  • 第13题:

    public class Pet{     private String name;     public Pet(){  System.out.print(1);    }  public Pet(String name){        System.out.print(2);    } }  public class Dog extends Pet{     public Dog(String name){        System.out.print(3);    } }  执行new Dog(“棕熊”);后程序输出是哪项?() 

    • A、 23
    • B、 13
    • C、 123
    • D、 321

    正确答案:B

  • 第14题:

    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

  • 第15题:

    public class Employee{       private String name;  public Employee(String name){           this.name = name;      }  public String getName(){         return name;      } }  public class Manager extends Employee{       public Manager(String name){          System.out.println(getName());      } }  执行语句new Manager(“smith”)后程序的输出是哪项?() 

    • A、 smith
    • B、 null
    • C、 编译错误
    • D、 name

    正确答案:C

  • 第16题:

    public class X {  public X aMethod() { return this;}  }  public class Y extends X {  }  Which two methods can be added to the definition of class Y?()

    • A、 public void aMethod() {}
    • B、 private void aMethod() {}
    • C、 public void aMethod(String s) {}
    • D、 private Y aMethod() { return null; }
    • E、 public X aMethod() { return new Y(); }

    正确答案:C,E

  • 第17题:

    Which two demonstrate an “is a” relationship?()   

    • A、 public interface Person { }  public class Employee extends Person { }
    • B、 public interface Shape { }  public class Employee extends Shape { }
    • C、 public interface Color { }  public class Employee extends Color { }
    • D、 public class Species { }  public class Animal (private Species species;)
    • E、 interface Component { }  Class Container implements Component ( Private Component[ ] children;  )

    正确答案:D,E

  • 第18题:

    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

  • 第19题:

    单选题
    public class Employee{       private String name;  public Employee(String name){           this.name = name;      }  public String getName(){         return name;      } }  public class Manager extends Employee{       private String department;  public Manager(String name,String department){          this.department = department;          super(name);  System.out.println(getName());      }  }  执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
    A

     smith

    B

     null

    C

     SALES

    D

     编译错误


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

  • 第20题:

    多选题
    Which are syntactically valid statement at// point x?()     class Person {     private int a;  public int change(int m){  return m;  }     }  public class Teacher extends Person {     public int b;  public static void main(String arg[]){     Person p = new Person();     Teacher t = new Teacher();    int i;  // point x     }    }
    A

    i = m;

    B

    i = b;

    C

    i = p.a;

    D

    i = p.change(30);

    E

    i = t.b.


    正确答案: D,E
    解析: A:m没有被申明过,不能使用。 
    B:虽然b是类Teacher的public成员变量,但是在静态方法中不能使用类中的非静态成员。 
    C://a是类Person的private成员,在类外不能直接引用。 
    D://change(int m)方法是public方法,并且返回一个int型值,可以通过类的实例变量p引用并赋值给一个int型变量。 
    E://b是类Teacher的public成员变量,且是int型,可以通过类的实例变量t引用并赋值给一个int型变量

  • 第21题:

    多选题
    Which three demonstrate an “is a” relationship?()
    A

    public class X {  }     public class Y extends X { }

    B

    public interface Shape { }     public interface Rectangle extends Shape{ }

    C

    public interface Color { }     public class Shape { private Color color; }

    D

    public interface Species { }     public class Animal { private Species species; }

    E

    public class Person { }    public class Employee {      public Employee(Person person) { }

    F

    interface Component { }     class Container implements Component {   private Component[] children; }


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

  • 第22题:

    单选题
    public class Employee{   private String name;   public Employee(String name){   this.name = name;  }   public String getName(){   return name;  }  }   public class Manager extends Employee{   public Manager(String name){   System.out.println(getName());  }  }   执行语句new Manager(“smith”)后程序的输出是哪项?()
    A

     smith

    B

     null

    C

     编译错误

    D

     name


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

  • 第23题:

    多选题
    Which the two demonstrate an “is a” relationship?()
    A

    public interface Person {}  Public class Employee extends Person {}

    B

    public interface Shape {}  public interface Rectangle extends Shape {}

    C

    public interface Color {}  public class Shape { private Color color; }

    D

    public class Species {}  public class Animal { private Species species; }

    E

    interface Component {} Class Container implements Component {private Component [] children;


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

  • 第24题:

    单选题
    public class Employee{   private String name;   public Employee(String name){   this.name = name;  }   public void display(){   System.out.print(name);  }  }   public class Manager extends Employee{   private String department;   public Manager(String name,String department){   super(name);   this.department = department;  }   public void display(){   System.out.println( super.display()+”,”+department);  }   }   执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
    A

     smith,SALES

    B

     null,SALES

    C

     smith,null

    D

     null,null


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