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 + “>”;
您可能感兴趣的试卷
你可能感兴趣的试题
11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println(”It is “ + (j==i) + “that j==i.”);
16. }
17. }
What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?()
A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
public class TestString 1 {
public static void main(String[] args) {
String str = “420”;
str += 42;
System.out.print(str);
}
}
What is the output?()
A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.
F. An exception is thrown at runtime.
1. public class Boxer1 {
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x=i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?()
A. The value “4” is printed at the command line.
B. Compilation fails because of an error in line 5.
C. Compilation fails because of an error in line 9.
D. A NullPointerException occurs at runtime.
E. A NumberFormatException occurs at runtime.
F. An IllegalStateException occurs at runtime.
10. public class ClassA {
11. public void count(int i) {
12. count(++i);
13. }
14. }
And:
20. ClassA a = new ClassA();
21. a.count(3);
Which exception or error should be thrown by the virtual machine?()
A. StackOverflowError
B. NullPointerException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionlnlnitializerError
public class Foo {
static int[] a;
static { a[0]=2; }
public static void main( String[] args) {}
}
Which exception or error will be thrown when a programmer attempts to run this code?()
A. java.lang. StackOverflowError
B. java.lang.IllegalStateException
C. java.lang.ExceptionlnlnitializerError
D. java.lang.ArraylndexOutOfBoundsException
10. public class ClassA {
11. public void methodA() {
12. ClassB classB = new ClassB();
13. classB.getValue();
14. }
15. }
And:
20. class ClassB {
21. public ClassC classC;
22.
23. public String getValue() {
24. return classC.getValue();
25. }
26. }
And:
30. class ClassC {
31. public String value;
32.
33. public String getValue() {
34. value = “ClassB”;
35. return value;
36. }
37. }
Given:
ClassA a = new ClassA();
a.methodA();
What is the result?()
A. Compilation fails.
B. ClassC is displayed.
C. The code runs with no output.
D. An exception is thrown at runtime.
11. public void someMethod(Object value) {
12. // check for null value ....
20. System.out.println(value.getClass());
21. }
What, inserted at line 12, is the appropriate way to handle a null value?()
A. assert value == null;
B. assert value !null, “value is null”;
C. if (value == null) { throw new AssertionException(”value is null”);
D. if (value == null) { throw new IllegalArgumentException(”value is null”);
static void test() throws RuntimeException {
try {
System.out.print(”test “);
throw new RuntimeException();
}
catch (Exception ex) { System.out.print(”exception “); }
}
public static void main(String[] args) {
try { test(); }
catch (RuntimeException ex) { System.out.print(”runtime “); }
System.out.print(”end “);
}
What is the result?()
A. test end
B. Compilation fails.
C. test runtime end
D. test exception end
E. A Throwable is thrown by main at runtime.
static void test() {
try {
String x=null;
System.out.print(x.toString() +“ “);
}
finally { System.out.print(“finally “); }
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print(”exception “); }
}
What is the result?()
A. null
B. finally
C. null finally
D. Compilation fails.
E. finally exception
static void test() throws Error {
if (true) throw new AssertionError();
System.out.print(”test “);
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print(”exception “); }
System.out.print(”elld “);
}
What is the result?()
A. end
B. Compilation fails.
C. exception end
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.
最新试题
A programmer must create a generic class MinMax and the type parameter of MinMax must implement Comparable. Which implementation of MinMax will compile?()
What is the result?()
Which two can be results?()
Given a class whose instances, when found in a collection of objects, are sorted by using the compareTo() method, which two statements are true?()
Which code, inserted at line 4, guarantees that this program will output [1, 2]?()
Which statement is true?()
What is the result?()
What is the result?()
Which two are possible results?()
Which three changes should be made to adapt this class to be used safely by multiple threads?()