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

Java.lang Class - Finding the output Questions and Answers

51.

What will be the output of the program?

public class Example51
{
    void run()
    {
        String out = String.format("%05.2f", 3.14159);
        System.out.println(out + " " + out.length());
    }

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

Answer: C

The format asks for a fixed-point number two places after the decimal point, in a field five characters wide, zero padded. That gives 03.14, which is five characters including the point. Without the 0 flag the padding would have been spaces.

Please log in or register to report this question.

52.

What will be the output of the program?

public class Program52
{
    public static void main(String... args)
    {
        System.out.println(Math.sqrt(-1) + " " + Math.pow(2, 10));
    }
}

Answer: D

Correct answer: NaN 1024.0

The square root of a negative number is not a domain error in floating point - Math.sqrt returns NaN rather than throwing, since the IEEE 754 arithmetic Java uses defines that result. Math.pow returns a double whatever its arguments, so 2 to the 10 prints as 1024.0 and not 1024. Both details matter to the exact output.

Please log in or register to report this question.

53.

What will be the output of the program?

public class Trial53
{
    Trial53()
    {
        int dividend = -7, divisor = 2;
        int floored = Math.floorDiv(dividend, divisor);
        int wrappedMod = Math.floorMod(dividend, divisor);
        System.out.println(floored + " and " + wrappedMod);
    }

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

Answer: A

floorDiv rounds toward negative infinity, giving -4 where ordinary division truncates to -3, and floorMod returns a result with the sign of the divisor, giving 1 rather than -1. The pair exists precisely because the built-in operators behave the other way.

Please log in or register to report this question.

54.

What will be the output of the program?

public class Case54
{
    public static void main(String[] args) throws Exception
    {
        int flags = 20;
        int top = Integer.highestOneBit(flags);
        int bottom = Integer.lowestOneBit(flags);
        System.out.println("top=" + top);
        System.out.println("bottom=" + bottom);
    }
}

Answer: B

Correct answer: top=16 then bottom=4

Twenty in binary is 10100. highestOneBit returns the value of the most significant set bit, which is 16, and lowestOneBit the value of the least significant set bit, which is 4. Both return the *value* of the bit rather than its position, which is why the answer is 16 and 4 rather than 4 and 2.

Please log in or register to report this question.

55.

What will be the output of the program?

public class Sketch55
{
    private static void report()
    {
        byte b = (byte) 200;
        short s = (short) 70000;
        System.out.println(b + " " + s);
    }

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

Answer: C

Both casts discard the high bits rather than clamping to the maximum. 200 does not fit in a signed byte and wraps to -56, and 70000 wraps to 4464 in a short. Narrowing never saturates in Java, which is why an explicit cast is required.

Please log in or register to report this question.

56.

What will be the output of the program?

public class Item56
{
    void execute()
    {
        System.out.println(Double.compare(0.0, -0.0) + " " + (0.0 == -0.0));
    }

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

Answer: D

The primitive comparison treats the two zeros as equal, so it is true. Double.compare deliberately does not: it orders negative zero below positive zero so that sorting and collections behave consistently, and returns 1. Equal but not identical.

Please log in or register to report this question.

57.

What will be the output of the program?

public class Sample57
{
    public static void main(String[] args)
    {
        Integer a = new Integer(5);
        Integer b = new Integer(5);
        System.out.println((a == b) + " " + a.equals(b) + " " + (a.intValue() == b.intValue()));
    }
}

Answer: A

Using the constructor always creates a new object, so the identity test is false whatever the value and the cache never applies. equals compares the wrapped values and is true, as is comparing the unboxed ints. This is why valueOf is preferred over the constructor.

Please log in or register to report this question.

58.

What will be the output of the program?

public class Probe58
{
    static void show()
    {
        System.out.println(Integer.decode("0x1f") + " " + Integer.parseInt("1f", 16));
    }

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

Answer: B

decode understands the 0x prefix and reads the rest as hexadecimal, giving 31. parseInt has no prefix handling but accepts an explicit radix, so 1f base 16 is also 31. Passing 0x1f to parseInt without a radix would throw NumberFormatException.

Please log in or register to report this question.

59.

What will be the output of the program?

public class Demo59
{
    void run()
    {
        String phrase = "hello world";
        boolean allLower = phrase.matches("[a-z ]+");
        String masked = phrase.replaceAll("o", "0");
        System.out.println(allLower);
        System.out.println(masked);
    }

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

Answer: C

The pattern allows lower case letters and spaces and must match the whole string, which it does, so matches is true. replaceAll substitutes every o, giving hell0 w0rld. Note matches anchors implicitly, unlike find in a Matcher.

Please log in or register to report this question.

60.

What will be the output of the program?

public class Snippet60
{
    public static void main(String... args)
    {
        String literal = "report";
        StringBuilder builder = new StringBuilder("rep");
        String pooled = builder.append("ort").toString().intern();
        System.out.println(literal == pooled);
    }
}

Answer: D

The builder produces a brand new String at runtime, which would fail an identity test against the literal. intern is what rescues it: the method returns the pooled instance for that text, and the pool already holds the literal. So both variables end up referencing the same object and the test is true. Dropping the intern call makes it false.

Please log in or register to report this question.

Loading…

Other Subjects

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