Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ques-Только тут иногда ошибки мне встретились, редко, но все же.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
240 Кб
Скачать
☆

96. Given this method in a class:

21. public String toString() {

22. StringBuffer buffer = new StringBuffer();

23. buffer.append('<');

24. buffer.append(this.name);

25. buffer.append('>');

26. return buffer.toString();

27. }

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 + ">";

97. Given:

1. public class MyLogger {

2. private StringBuilder logger = new StringBuuilder();

3. public void log(String message, String user) {

4. logger.append(message);

5. logger.append(user);

6. }

7. }

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

98. Given:

11. public String makinStrings() {

12. String s = "Fred";

13. s = s + "47";

14. s = s.substring(2, 5);

15. s = s.toUpperCase();

16. return s.toString();

17. }

How many String objects will be created when this method is invoked?

A. 1

B. 2

C. 3

D. 4

E. 5

F. 6

99. Given:

1. public class TestString3 {

2. Public static void main(String[] args) {

3. // insert code here

5. System.out.println(s);

6. }

7. }

Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.)

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");

100. Given:

11. public class Yikes {

13. public static void go(Long n) {System.out.println("Long ");}

14. public static void go(Short n) {System.out.println("Short ");}

15. public static void go(int n) {System.out.println("int ");}

16. public static void main(String [] args) {

17. short y= 6;

18. long z= 7;

19. go(y);

20. go(z);

21. }

22. }

What is the result?

A. int Long

B. Short Long

C. Compilation fails.

D. An exception is thrown at runtime.

101. Given:

12. public class Wow {

13. public static void go(short n) {System.out.println("short"); }

14. public static void go(Short n) {System.out.println("SHORT");}

15. public static void go(Long n) {System.out.println(" LONG"); }

16. public static void main(String [] args) {

17. Short y= 6;

18. int z=7;

19. go(y);

20. go(z);

21. }

22. }

What is the result?

A. short LONG

B. SHORT LONG

C. Compilation fails.

D. An exception is thrown at runtime.