Which code fragment, inserted at line 24, outputs 123abc 123abc?

Given:

22. StringBuilder sb1 = new StringBuilder(“123”);
23. String s1 = “123”;
24. // insert code here
25. System.out.println(sb1 + ” ” + s1);

Which code fragment, inserted at line 24, outputs 123abc 123abc?

Given:

22. StringBuilder sb1 = new StringBuilder(“123”);
23. String s1 = “123”;
24. // insert code here
25. System.out.println(sb1 + ” ” + s1);

Which code fragment, inserted at line 24, outputs 123abc 123abc?

A.
sb1.append(“abc”); s1.append(“abc”);

B.
sb1.append(“abc”); s1.concat(“abc”);

C.
sb1.concat(“abc”); s1.append(“abc”);

D.
sb1.concat(“abc”); s1.concat(“abc”);

E.
sb1.append(“abc”); s1 = s1.concat(“abc”);

F.
sb1.concat(“abc”); s1 = s1.concat(“abc”);

G.
sb1.append(“abc”); s1 = s1 + s1.concat(“abc”);

H.
sb1.concat(“abc”); s1 = s1 + s1.concat(“abc”);

Explanation:
A:
Main.java:24: cannot find symbol
symbol : method append(java.lang.String)
location: class java.lang.String
sb1.append(“abc”); s1.append(“abc”);
^
1 error

B: 123abc 123

C:
Main.java:24: cannot find symbol
symbol : method concat(java.lang.String)
location: class java.lang.StringBuilder
sb1.concat(“abc”); s1.append(“abc”);
^
Main.java:24: cannot find symbol
symbol : method append(java.lang.String)
location: class java.lang.String
sb1.concat(“abc”); s1.append(“abc”);
^
2 errors

D:
Main.java:5: cannot find symbol
symbol : method concat(java.lang.String)
location: class java.lang.StringBuilder
sb1.concat(“abc”); s1.concat(“abc”);
^
1 error

E: 123abc 123abc

F:
Main.java:24: cannot find symbol
symbol : method concat(java.lang.String)
location: class java.lang.StringBuilder
sb1.concat(“abc”); s1 = s1.concat(“abc”);
^
1 error

G: 123abc 123123abc

H:
Main.java:24: cannot find symbol
symbol : method concat(java.lang.String)
location: class java.lang.StringBuilder
sb1.concat(“abc”); s1 = s1 + s1.concat(“abc”);
^
1 error



Leave a Reply 0

Your email address will not be published. Required fields are marked *