Given:
What is the result?
Given:
What is the result?
HOWDY <-- sb.append("HOWDY"); *HOWDY <-- sb.insert(0, "*"); *HOLLY <-- sb.replace(3, 5, "LL"); *HOLLYCOW <-- sb.insert(6, "COW"); *HOW <-- sb.delete(2, 7); 4 <-- sb.length();
'-' indicates blank space HOWDY _HOWDY _HOLLY _HOLLYCOW _HOW
4 is the length.
B - Correct Answer
B is correct
why 4 ? if final string is _HCOW and has 5 elements
anwers: delete(2,7) delete 5 elements 2,3,4,5,6 => so 9 - 5 = 4 lol!
public class Tester { public static void main(String[] args) { StringBuilder sb = new StringBuilder(5); sb.append("HOWDY"); sb.insert(0, ' '); sb.replace(3, 5, "LL"); sb.insert(6, "COW"); sb.delete(2, 7); System.out.println(sb.length()); } } Result: 4. --> Choose B.
B IS CORRECT
B- Correct, see below with capacity also. HOWDY :: sb.length():5, sb.capacity(): 5 _HOWDY :: sb.length():6, sb.capacity(): 12 _HOLLY :: sb.length():6, sb.capacity(): 12 _HOLLYcow :: sb.length():9, sb.capacity(): 12 _How :: sb.length():4, sb.capacity(): 12
5 is correct answer because HOWDY _HOWDY _HOLLDY _HOLLDcowY _HowY 5
used : sb.insert(0,' '); exit console: HOWDY HOWDY HOLLY HOLLYCOW HOLLYOW 8
D is correct: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 6, length 5 at java.base/java.lang.String.checkOffset(String.java:4572) at java.base/java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:1170) at java.base/java.lang.StringBuilder.insert(StringBuilder.java:336) at com.jgr.temporal.main.Principal.main(Principal.java:16)
B is correct: the capacity will be automatically extended