Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class Sample13
{
    Sample13()
    {
        StringBuilder path = new StringBuilder("/usr/local/bin");
        path.delete(4, 10);
        System.out.println(path);
    }

    public static void main(String[] args)
    {
        new Sample13();
    }
}

  • A.The program prints /usr/bin
  • B.The program prints /usr/local
  • C.The program prints /usrlocal/bin
  • D.The program prints /usr/l/bin

Answer: A

delete removes from the start index inclusive to the end index exclusive, so positions 4 through 9 go and position 10 stays. That takes out /local and leaves /usr/bin. The half-open range is the same convention substring uses, and forgetting it removes one character too many or too few.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.