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...

An initialization vector should be which of the following?

a. Unique and unpredictable b. Repeatable and unique c. Unique and predictable d. Repeatable and random

Computer Science & Information Technology

What is the function of the symbol & when used with a variable?

A. It makes a pointer to that variable. B. It allows main() to access the variable in memory. C. Gives the address of a variable in memory. D. You can’t use this operator alone.

Computer Science & Information Technology