108k views
0 votes
Which of the following will create a String different from the other three?

a. String r = ""123456""
b. int i = 123;
int j = 456;
String r = String.valueOf(j) + String.valueOf(i);
c. int i = 123;
int j = 456;
String r = String.valueOf(i) + String.valueOf(j);
d. int i = 123;
int j = 456;
String r = i + j;"

1 Answer

1 vote

Final answer:

Option b will create a String "456123", which is different from the other options where option c creates "123456" and option d results in the sum "579".

Step-by-step explanation:

The question asks which of the following options will create a String different from the other three. Upon examining each option:

Option a has a syntax error with double quotes and does not compile.

Option b creates a String by concatenating the value of j and then the value of i, resulting in "456123".

Option c creates a String by concatenating the value of i and then the value of j, resulting in "123456".

Option d also creates a String in a similar way to option c, because when you add two integers in Java and concatenate them with an empty string, it will concatenate their sum (579) into a single String.

Therefore, the option that creates a unique String compared to the others is option b, as it concatenates the numbers in the reverse order compared to option c and is different from the sum in option d.

User PVilaca
by
7.7k points