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

Java.lang Class - Finding the output Questions and Answers

11.

What will be the output of the program?

public class Sketch11
{
    void run()
    {
        StringBuilder sb = new StringBuilder("abc");
        sb.reverse();
        System.out.println(sb);
    }

    public static void main(String[] args)
    {
        new Sketch11().run();
    }
}

Answer: C

Unlike String, StringBuilder is mutable and reverse changes the object in place as well as returning it. The result is cba even though the return value was discarded. This is the difference that catches people who have just learned that String methods return new objects.

Please log in or register to report this question.

12.

What will be the output of the program?

public class Item12
{
    public static void main(String... args)
    {
        StringBuilder sb = new StringBuilder("hello");
        sb.insert(0, ">").append("<");
        System.out.println(sb);
    }
}

Answer: D

insert places the text before index 0, giving >hello, and each mutator returns the same builder so append then adds < at the end. Chaining works because the methods return this rather than a copy. The result is >hello<.

Please log in or register to report this question.

13.

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();
    }
}

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.

Please log in or register to report this question.

14.

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.

Please log in or register to report this question.

15.

What will be the output of the program?

public class Demo15
{
    private static void report()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(1).append(2 + 3).append("4");
        System.out.println(sb);
    }

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

Answer: C

The arithmetic inside the second append is evaluated before the call, so 2 + 3 becomes the int 5 rather than being concatenated as text. The three appends therefore add 1, 5 and 4 in turn, giving 154. Only string concatenation would have produced 23.

Please log in or register to report this question.

16.

What will be the output of the program?

public class Snippet16
{
    void execute()
    {
        String s = "ab";
        StringBuilder sb = new StringBuilder("ab");
        System.out.println(s.equals(sb.toString()) + " " + s.equals(sb));
    }

    public static void main(String[] args)
    {
        Snippet16 demo = new Snippet16();
        demo.execute();
    }
}

Answer: D

Converting the builder to a String gives a value equal to s, so the first test is true. The second passes the builder itself, and String.equals returns false for anything that is not a String, whatever it contains. StringBuilder does not override equals either.

Please log in or register to report this question.

17.

What will be the output of the program?

public class Listing17
{
    public static void main(String[] args)
    {
        long up = Math.round(2.5);
        long down = Math.round(-2.5);
        System.out.println(up + " " + down);
    }
}

Answer: A

Math.round adds one half and takes the floor, so a value exactly halfway always moves toward positive infinity. That sends 2.5 up to 3 and -2.5 up to -2. Expecting symmetry around zero is the trap; only the positive case matches the everyday rule.

Please log in or register to report this question.

18.

What will be the output of the program?

public class Fragment18
{
    static void show()
    {
        int smallest = Integer.MIN_VALUE;
        int magnitude = Math.abs(smallest);
        System.out.println(magnitude);
    }

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

Answer: B

Integer.MIN_VALUE has no positive counterpart in the int range, so Math.abs cannot represent the answer and returns the argument unchanged, still negative. This is documented behaviour rather than a defect. Math.abs on a long would be needed to get the positive value.

Please log in or register to report this question.

19.

What will be the output of the program?

public class Example19
{
    void run()
    {
        double value = -1.5;
        System.out.println(Math.floor(value) + " " + Math.ceil(value));
    }

    public static void main(String[] args)
    {
        new Example19().run();
    }
}

Answer: C

floor moves toward negative infinity and ceil toward positive infinity, so -1.5 becomes -2.0 and -1.0 respectively. Both return a double, which is why the output carries the decimal point. Thinking of them as round-down and round-up in magnitude reverses the answer for negatives.

Please log in or register to report this question.

20.

What will be the output of the program?

public class Program20
{
    public static void main(String... args)
    {
        double negZero = -0.0;
        double posZero = 0.0;
        System.out.println(Math.min(negZero, posZero));
    }
}

Answer: D

Math.min for doubles is specified to treat negative zero as strictly smaller than positive zero, so it returns -0.0 and println prints the sign. The primitive comparison -0.0 == 0.0 is true, which is why this looks surprising: the two are equal but not identical.

Please log in or register to report this question.

Loading…
Tags: Java.lang Class - Finding the output Questions and Answers || Java.lang Class - Finding the output MCQ Questions and Answers || Java.lang Class - Finding the output GK Questions and Answers || Java.lang Class - Finding the output GK MCQ Questions || Java.lang Class - Finding the output Multiple Choice Questions and Answers || Java.lang Class - Finding the output GK || GK on Java.lang Class - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming