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

Java.lang Class - Finding the output Questions and Answers

61.

What will be the output of the program?

public class Listing61
{
    Listing61()
    {
        StringBuffer buf = new StringBuffer("log");
        buf.append(":").append(1);
        String s = buf.toString();
        System.out.println(s + " " + s.length());
    }

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

Answer: A

StringBuffer offers the same mutating API as StringBuilder, differing only in being synchronised, so the appends give log:1. That is five characters, and the int 1 is converted to its digit rather than added to anything. StringBuilder is preferred when no thread safety is needed.

Please log in or register to report this question.

62.

What will be the output of the program?

public class Fragment62
{
    public static void main(String[] args) throws Exception
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 3; i > 0; i--) sb.append(i);
        System.out.println(sb + " " + sb.length());
    }
}

Answer: B

The loop counts down from 3, so the digits are appended in the order 3, 2, 1 and the content is 321. Appending an int writes its decimal digits, so the length is 3 rather than the sum of the values. Nothing reverses the builder here.

Please log in or register to report this question.

63.

What will be the output of the program?

public class Example63
{
    private static void report()
    {
        String s = "a-b-c";
        String[] two = s.split("-", 2);
        System.out.println(two.length + " " + two[1]);
    }

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

Answer: C

A positive limit caps how many pieces split produces, so with 2 the string is cut once and the remainder is left whole. That gives a and b-c, so the length is 2 and the second element still contains a hyphen. The default limit of zero would have given three pieces.

Please log in or register to report this question.

64.

What will be the output of the program?

public class Program64
{
    void execute()
    {
        boolean underscoreOk = Character.isLetterOrDigit('_');
        boolean tabIsSpace = Character.isWhitespace('\t');
        System.out.println(underscoreOk + " " + tabIsSpace);
    }

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

Answer: D

An underscore is neither a letter nor a digit as far as Character is concerned, despite being legal in identifiers, so the first test is false. A tab is whitespace, so the second is true. Identifier rules and character classification are separate questions.

Please log in or register to report this question.

65.

What will be the output of the program?

public class Trial65
{
    public static void main(String[] args)
    {
        String s = "one";
        switch (s)
        {
            case "one": System.out.print("1");
            case "two": System.out.print("2"); break;
            default: System.out.print("?");
        }
    }
}

Answer: A

Switching on a String matches by equals, so the first label is taken. There is no break after it, so control falls through into the next case and prints 2 before the break stops it. Fall-through applies to String switches exactly as it does to int ones.

Please log in or register to report this question.

66.

What will be the output of the program?

public class Case66
{
    static void show()
    {
        Integer count = 5;
        int doubled = count * 2;
        Integer boxedAgain = doubled;
        System.out.println(doubled + " " + boxedAgain.equals(10));
    }

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

Answer: B

The wrapper unboxes for the arithmetic, giving the int 10, and assigning that back to an Integer boxes it again. equals against the literal 10 boxes that too and compares values, so it is true. Autoboxing happens silently in both directions.

Please log in or register to report this question.

67.

What will be the output of the program?

public class Sketch67
{
    void run()
    {
        System.out.println(Boolean.compare(true, false) + " " + Boolean.logicalXor(true, true));
    }

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

Answer: C

compare orders false below true, so comparing true with false gives a positive result, specified as 1. logicalXor is true only when the operands differ, and here they are both true, so it is false. Neither method short-circuits, since both arguments are already evaluated.

Please log in or register to report this question.

68.

What will be the output of the program?

public class Item68
{
    public static void main(String... args)
    {
        Object a = new Object();
        Object b = a;
        System.out.println((a.equals(b)) + " " + (System.identityHashCode(a) == System.identityHashCode(b)));
    }
}

Answer: D

Object's equals is identity, and both variables reference the same object, so it is true. identityHashCode returns the original hash for a given object regardless of any override, and for one object it is the same value both times. Two distinct objects would likely differ.

Please log in or register to report this question.

69.

What will be the output of the program?

public class Sample69
{
    Sample69()
    {
        double d = 1e3;
        float f = 1.5f;
        System.out.println(d + " " + (d + f));
    }

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

Answer: A

A double in this range prints in plain decimal form with one fractional digit, so 1e3 shows as 1000.0. Adding a float promotes it to double, and the sum prints as 1001.5. The literal notation does not survive into the output.

Please log in or register to report this question.

70.

What will be the output of the program?

public class Probe70
{
    public static void main(String[] args) throws Exception
    {
        String s = "\u0041BC";
        System.out.println(s + " " + s.length());
    }
}

Answer: B

A Unicode escape is processed by the compiler before the program is even parsed, so the source holds the single character A followed by BC. The string is therefore ABC with a length of 3. The escape never survives as text.

Please log in or register to report this question.

Loading…

Topics

    No topic!
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