网友您好, 请在下方输入框内输入要搜索的题目:
题目内容
(请给出正确答案)
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 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 methodB、 replace StringBuilder with StringBufferC、 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” 相关考题
考题
阅读以下说明和java代码,将应填入(n)处的字句写在对应栏内。[说明]本程序中预设了若干个用户名和口令。用户输入正确的用户名后,可以查找对应的口令,一旦输入结束标记“end”,程序结束。[Java代码]import java. io. *:public class User {public String user;public Siring pass;public User() { }public User( String u,String p) {user=u;pass=p;}public String (1) () { return this. user; }public String (2) () { return this. pass; }public static void main(String[] args) {User ua[]={new User("Li","123456"), new User("wang","654321"),new User("Song","666666")};while(true) {InputStreamReader reader = new InputStreamReader(System. in);BufferedReader inpul = new BnfferedReader(reader);System. out. print("Enter your name:");String name = null;try { name = input. readLine();}catch (IOException ex) {}if((3)) break;int i;for (i=0;i<3;i++) {if (name. equals(ua[i]. getUser())){System. out. println("密码:"+ua[i].getPass());(4);}}if ((5)) System. out. println("该用户不存在!");}}}
考题
A programmer needs to create a logging method that can accept an arbitrary number of arguments.Forexample,it may be called in these ways:logIt(log message 1);logIt(log message 2”,”log message 3);logIt(log message 4,log message 5,log message 6);Which declaration satisfies this requirement()?A.public void logIt(String*msgs)B.public void logIt(String[]msgs)C.public void logIt(String...msgs)D.public voidl ogIt(Stringmsg1,Stringmsg2,Stringmsg3)
考题
publicclassMyLogger{privateStringBuilderlogger=newStringBuuilder();publicvoidlog(Stringmessage,Stringuser){logger.append(message);logger.append(user);}}TheprogrammermustguaranteethatasingleMyLoggerobjectworksproperlyforamulti-threadedsystem.Howmustthiscodebechangedtobethread-safe?()A.synchronizethelogmethodB.replaceStringBuilderwithStringBufferC.Nochangeisnecessary,thecurrentMyLoggercodeisalreadythread-safe.D.replaceStringBuilderwithjustaStringobjectandusethestringconcatenation(+=)withinthelogmethod
考题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。[说明]本程序的功能是给公司的员工Tom,Jack,Green增加薪水。三人的职位分别是programmer, Manager,CEO。程序由6个类组成:WorkerTest是主类,programmer,Manager,CEO三个类,薪水增加的规则是 programmer的涨幅是5%;Manager的是10%;CEO也是Manager,但是它除了有Manager的涨幅,还有1000元的bonus。接口SalaryRaise提供了一个增加薪水的方法raise()。[java程序]public class WorkerTest {public WorkerTest( ) {}public static void main( String[] args) {Programmer programmer = new Programmer( "Tom" ,3000);Manager manager = new Manager( "Jack" ,4000);CEO ceo = new CEO( "Green" ,4000);Worker [] worker = new Worker[3];programmer, raise( );manager, raise( );ceo. raise( );worker[0] = programmer;worker [1] = manager;worker[2] = ceo;for ( int i = 0 ;i < worker, length; i + + ) {System. out. prinfln (" Name:" + worker [i]. getName ( ) +" \ tSalary:" + worker [i]. getSalary ());public interface SalaryRaise { void raise( ); }public class Worker {public String name;public double (1);public Worker( ) {}public String getName( ) {return name;}public void setName( String name) {this. name = name;}public double getSalary( ) {return salary;}public void setSalary(double salary) { this. salary = salary; }}public class Programmer extends Worker implements (2) {public Programmer( ) {}public void raise( ) {double pets=0.05;double sala = this. getSalary( ) * (1 + pers);this. setSalary (sala);public Programmer( Siring name, double salary) tthis. name = name;this. salary = salary;public class Manager extends (3) implements SalaryRaise {public Manager( ) { }public Manager(String name, double salary) {this. name = name;this. salary = salary;}public void raise( ) {double pets = 0.1;double sala = this. getSalary() * (1 + pers);this. setSalary(sala);}}public class CEO extends Manager implements SalaryRaise {public CEO() {}public CEO( String name,double salary) {this. name = name;this. salary = salary;}public void raise( ) {double bonus = 1000;(4);double sala = this. getSalary( );(5);this. setSalary(sala);}}
考题
interface Playable {void play();}interface Bounceable {void play();}interface Rollable extends Playable, Bounceable {Ball ball = new Ball("PingPang");}class Ball implements Rollable {private String name;public String getName() {return name;}public Ball(String name) {this.name = name;}public void play() {ball = new Ball("Football");System.out.println(ball.getName());}}这个错误不容易发现。
考题
阅读以下说明和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);
考题
阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 以下C++代码实现一个简单的聊天室系统(ChatRoomSystem),多个用户(User)可以向聊天室(ChatRoom)发送消息,聊天室将消息展示给所有用户。类图如图6-1所表示。图6-1 类图【C++代码】 includeiostream include string using namespace std; class User { private: string name; public: User(string name){ (1) =name; } ~User(){} void setName(string name) { this-name=name; } string getName(){ return name; } void sendMessage(string message); }; class ChatRoom { . public: static void showMessage(User* user, string message) { cout[user-getName()] : messageendl; } }; void User::sendMessage(string message) { (2) (this,message); } class ChatRoomSystem{ public: . . void startup() { User* zhang = new User(John); User* li = new User(Leo); zhang-sendMessage(Hi! Leo!); li_sendMessage(Hi! John!); } void join(User* user) { (3) (HeIIo Everyone! l am+user-getName()); } . }; int main(){ ChatRoomSystem*crs= (4) ; crs-startup(); crs-join( (5) (Wayne)); delete crs; } /* 程序运行结果: [John]:Hi! Leo! [Leo]:Hi! John! [Wayne]:Hello Everyone! I am Wayne /*
考题
试题六(共 15 分)阅读下列说明和 C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。【说明】以下 C++代码实现一个简单的聊天室系统(ChatRoomSystem),多个用户 (User)可以向聊天室(ChatRoom)发送消息,聊天室将消息展示给所有用户。 类图如图 6-1 所表示。
【C++代码】#include#include using namespace std; class User {private:string name; public:User(string name){ (1) =name;}~User(){}
void setName(string name) {this->name=name; } string getName(){return name;}void sendMessage(string message); }; class ChatRoom { . public:static void showMessage(User* user, string message) { coutgetName()"] : " zhang->sendMessage("Hi! Leo!");li_>sendMessage("Hi! John!"); }void join(User* user) { (3) ("HeIIoEveryone!l am"+user->getName()); . ;} . };int main(){ChatRoomSystem*crs= (4) ; crs->startup();crs->join( (5) ("Wayne")); delete crs; }/* 程序运行结果: [John]:Hi! Leol [Leo]:Hi! John![Wayne】:Hello Everyone!Iam Wayne/*
考题
试题五(共 15 分)阅读以下说明和 Java 程序,填补代码中的空缺,将解答填入答题纸的对应 栏内。【说明】以下 Java 代码实现一个简单的聊天室系统(ChatRoomSystem),多个用 户(User)可以向聊天室( ChatRoom)发送消息,聊天室将消息展示给所有用户。 类图如图 5-1 所示。
【Java 代码】 class ChatRoom {
public static void showMessage(User user, Strmg message) {System.out.println("[" + user.getName() + "] : " + message);} }classUser{private String name; public String getName() { return name;}public void setName(String name) { this.name = name;}public User(String name) { (1) =name;}public void sendMessage(String message) { (2) (this, message);}}public class Chat:RoomSystem { public void startup() {
User zhang= new User("John");User li =new User("Leo"); zhang.sendMessage("Hi! Leo! "); 1i.sendMessage("Hi! John!"); } public void join(User user) { (3) ("Hello Everyone! I am" + user.getName()); }public static void main(String[] args) { ChatRoomSystem crs= (4) ; Crs.startup();Crs.join( (5) )(“Wayne”));}}/*程序运行结果: [John]:Hi! Leol [Leo]:Hi! John![Wayne】:Hello Everyone!Iam Wayne*/
考题
public class Pet{ private String name; public Pet(String name){ this.name = name; } public void speak(){ System.out.print(name); } } public class Dog extends Pet{ public Dog(String name){ super(name); } public void speak(){ super.speak(); System.out.print(“ Dog ”); } } 执行代码 Pet pet = new Dog(“京巴”); pet.speak(); 后输出的内容是哪项?() A、 京巴B、 京巴 DogC、 nullD、 Dog京巴
考题
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,SALESB、 null,SALESC、 smith,nullD、 null,nullE、 编译错误
考题
A programmer needs to create a logging method that can accept an arbitrary number of arguments.Forexample,it may be called in these ways: logIt("log message 1"); logIt("log message 2”,”log message 3"); logIt("log message 4","log message 5","log message 6"); Which declaration satisfies this requirement()?A、public void logIt(String*msgs)B、public void logIt(String[]msgs)C、public void logIt(String...msgs)D、public voidl ogIt(Stringmsg1,Stringmsg2,Stringmsg3)
考题
Which two allow the class Thing to be instantiated using new Thing()?A、 public class Thing { }B、 public class Thing { public Thing() {} }C、 public class Thing { public Thing(void) {} }D、 public class Thing { public Thing(String s) {} }E、 public class Thing { public void Thing() {} public Thing(String s) {} }
考题
public class X { public static void main(String [] args) { try { badMethod(); System.out.print(“A”); } catch (Exception ex) { System.out.print(“C”); } finally { System.out.print(“B”); } System.out.print(“D”); } public static void badMethod() { throw new Error(); } } What is the result?() A、 ABCDB、 Compilation fails.C、 C is printed before exiting with an error message.D、 BC is printed before exiting with an error message.E、 BCD is printed before exiting with an error message.
考题
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、 smithB、 nullC、 SALESD、 编译错误
考题
public class Item { private String desc; public String getDescription() { return desc; } public void setDescription(String d) { desc = d; } public static void modifyDesc(Item item, String desc) { item = new Item(); item.setDescription(desc); } public static void main(String[] args) { Item it = new Item(); it.setDescription(”Gobstopper”); Item it2 = new Item(); it2.setDescription(”Fizzylifting”); modifyDesc(it, “Scrumdiddlyumptious”); System.out.println(it.getDescription()); System.out.println(it2.getDescription()); } } What is the outcome of the code? ()A、 Compilation fails.B、 Gobstopper FizzyliftingC、 Gobstopper ScrumdiddlyumptiousD、 Scrumdiddlyumptious FizzylifltngE、 Scrumdiddlyumptious Scrumdiddlyumptious
考题
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,SALESB、 null,SALESC、 smith,nullD、 null,null
考题
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(); }
考题
A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, it may be called in these ways: logIt(”log message 1 “); logIt(”log message2”,”log message3”); logIt(”log message4”, “log message5”, “log message6); Which declaration satisfies this requirement?()A、 public void logIt(String * msgs)B、 public void logIt(String [] msgs)C、 public void logIt(String... msgs)D、 public void logIt(String msg1, String msg2, String msg3)
考题
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 3B、 line 6C、 line 7D、 line 8E、 line 10
考题
public class Car { private int wheelCount; private String vin; public Car(String vin) { this.vin = vin; this.wheelCount = 4; } public String drive() { return “zoom-zoom”; } public String getInfo() { return “VIN: “+ vin + “wheels: “+ wheelCount; } } And: public class MeGo extends Car { public MeGo(String vin) { this.wheelCount = 3; } } What two must the programmer do to correct the compilation errors?()A、 insert a call to this() in the Car constructorB、 insert a call to this() in the MeGo constructorC、 insert a call to super() in the MeGo constructorD、 insert a call to super(vin) in the MeGo constructorE、 change the wheelCount variable in Car to protectedF、 change line 3 in the MeGo class to super.wheelCount = 3;
考题
单选题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,SALESB
null,SALESC
smith,nullD
null,nullE
编译错误
考题
单选题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 3B
line 6C
line 7D
line 8E
line 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 methodB
replace StringBuilder with StringBufferC
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 programmer needs to create a logging method that can accept an arbitrary number of arguments.Forexample,it may be called in these ways: logIt("log message 1"); logIt("log message 2”,”log message 3"); logIt("log message 4","log message 5","log message 6"); Which declaration satisfies this requirement()?A
public void logIt(String*msgs)B
public void logIt(String[]msgs)C
public void logIt(String...msgs)D
public voidl ogIt(Stringmsg1,Stringmsg2,Stringmsg3)
考题
单选题A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, it may be called in these ways: logIt(”log message 1 “); logIt(”log message2”,”log message3”); logIt(”log message4”, “log message5”, “log message6); Which declaration satisfies this requirement?()A
public void logIt(String * msgs)B
public void logIt(String [] msgs)C
public void logIt(String... msgs)D
public void logIt(String msg1, String msg2, String msg3)
考题
多选题Which two allow the class Thing to be instantiated using new Thing()?Apublic class Thing { }Bpublic class Thing { public Thing() {} }Cpublic class Thing { public Thing(void) {} }Dpublic class Thing { public Thing(String s) {} }Epublic class Thing { public void Thing() {} public Thing(String s) {} }
热门标签
最新试卷