Discussion
What will be the output of the program?
public class Probe14
{
public static void main(String[] args) throws Exception
{
StringBuilder sb = new StringBuilder("abcdef");
sb.setLength(3);
System.out.println(sb + " " + sb.length());
}
}
Answer: B
setLength truncates the builder when the new length is smaller, discarding everything from that index onward, so the content becomes abc and the length 3. Given a larger value it would pad with null characters instead of throwing.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development