Analyze the following code.

```
class Test {
public static void main(String[] args) {
StringBuilder strBuf = new StringBuilder(4);
strBuf.append("ABCDE");
System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5));
}
}
```
a. The program has a compile error because you cannot specify initial capacity in the StringBuilder constructor.
b. The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are appended into the buffer.
c. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.
d. The program compiles and runs fine.


c. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range.
The charAt method returns the character at a specific index in the string buffer. The first character of a string buffer is at index 0, the next at index 1, and so on. The index argument must be greater than or equal to 0, and less than the length of the string buffer.

Computer Science & Information Technology

You might also like to view...

To reduce the heat from a(n) ________, a cooling stand or heat-absorbing mat can be used to avoid burns

Fill in the blank(s) with correct word

Computer Science & Information Technology

To improve accuracy during user input, a designer can limit the number of characters a user can enter by adjusting the ________ from the properties screen

A) Length B) Details C) Field Size D) Data Type

Computer Science & Information Technology

If the condition in a do/while statement evaluates to false, the loop body is executed ____ time(s).

a. zero b. three c. two d. one

Computer Science & Information Technology

A technician is configuring a computer lab at a school. The computers need to be able to communicate with each other, but students using the computers should not be able to access the internet. Which of the following rules on the firewall should the technician configure for the lab computers?

A. Block all LAN to LAN traffic B. Block all LAN to WAN traffic C. Block all WAN to LAN traffic D. Block all WLAN to WAN traffic

Computer Science & Information Technology