Which of the following creates the string of the numbers from 1 to 1000 most efficiently?

a. String s;
for (int i = 1; i <= 1000; i++)
s += i;
b. StringBuilder sb = new StringBuilder(10);
for (int i = 1; i <= 1000; i++)
sb.append(i);
String s = new String(sb);
c. StringBuilder sb = new StringBuilder(3000);
for (int i = 1; i <= 1000; i++)
sb.append(i);
String s = new String(sb);
d. All are equivalently efficient.

c. StringBuilder sb = new StringBuilder(3000);
for (int i = 1; i <= 1000; i++)
sb.append(i);
String s = new String(sb);

Computer Science & Information Technology

You might also like to view...

C# has only one operator, ________ , for integer division and floating point division.

a) asterisk ( * ) b) backslash ( \ ) c) forward slash ( / ) d) None of the above.

Computer Science & Information Technology

How would one assign the value of num to the id component of variable emp2?

a. emprec.id = num; b. emp2.num = id; c. emp2.id = num; d. &emp2.id = num; e. emprec.emp1.id = num;

Computer Science & Information Technology