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

Java.lang Class - Finding the output Questions and Answers

71.

What will be the output of the program?

public class Demo71
{
    private static void report()
    {
        String s = "Mississippi";
        int count = 0;
        for (int i = 0; i < s.length(); i++) if (s.charAt(i) == 's') count++;
        System.out.println(count);
    }

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

Answer: C

Mississippi contains four lower case s characters, in two adjacent pairs. Counting with charAt compares chars directly, so no string allocation happens. The two i characters and the two p characters are not counted, which is where a quick guess of 2 comes from.

Please log in or register to report this question.

72.

What will be the output of the program?

public class Snippet72
{
    void execute()
    {
        String s = "abc";
        String t = s.replace('z', 'y');
        System.out.println((s == t) + " " + s.equals(t));
    }

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

Answer: D

replace returns the original object when there is nothing to change, so no new String is allocated and both variables reference the same one. Identity and equality are therefore both true. Replacing a character that does occur would have produced a new object.

Please log in or register to report this question.

73.

What will be the output of the program?

public class Listing73
{
    public static void main(String[] args)
    {
        Long a = 127L;
        Long b = 127L;
        System.out.println((a == b) + " " + a.equals(127L) + " " + a.equals(127));
    }
}

Answer: A

Long caches the same -128 to 127 range as Integer, so the identity test is true. equals against a long literal boxes it to Long and matches. equals against the int literal 127 boxes it to Integer, and a Long is never equal to an Integer whatever the value, so that one is false.

Please log in or register to report this question.

74.

What will be the output of the program?

public class Fragment74
{
    static void show()
    {
        System.out.println("10" + 20 + 30);
        System.out.println(10 + 20 + "30");
    }

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

Answer: B

Addition is left associative. The first line starts with a String, so every following value is joined as text, giving 102030. The second adds 10 and 20 as ints first and only then meets the String, giving 3030. Same operands, different order, different answer.

Please log in or register to report this question.

75.

What will be the output of the program?

public class Example75
{
    void run()
    {
        char[] letters = {'j', 'a', 'v', 'a'};
        System.out.println(letters.length + " " + String.valueOf(letters));
    }

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

Answer: C

String.valueOf has an overload taking a char array, which builds a String from its contents rather than calling toString on the array. So it prints java. Passing the array to println directly would have shown the type and hash instead.

Please log in or register to report this question.

76.

What will be the output of the program?

public class Program76
{
    public static void main(String... args)
    {
        Object[] items = new String[2];
        items[0] = "ok";
        System.out.println(items[0]);
        items[1] = Integer.valueOf(1);
    }
}

Answer: D

Array covariance lets a String array be referenced as an Object array, so the assignment compiles. The first store is a String and succeeds, and the print happens. The second stores an Integer into what is really a String array, which is checked at runtime and throws ArrayStoreException.

Please log in or register to report this question.

77.

What will be the output of the program?

public class Trial77
{
    Trial77()
    {
        String a = null;
        System.out.println(String.valueOf(a));
        System.out.println(a + "");
    }

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

Answer: A

String.valueOf checks for null and returns the text null rather than throwing, and concatenation applies the same conversion. So both lines print the four characters null. It is calling a method ON the null reference that fails, which neither line does.

Please log in or register to report this question.

78.

What will be the output of the program?

public class Case78
{
    public static void main(String[] args) throws Exception
    {
        int octal = 070;
        int hex = 0x1A;
        int plain = 70;
        System.out.println(octal + ", " + hex + ", " + plain);
    }
}

Answer: B

A leading zero marks an octal literal, so 070 is 7 times 8, which is 56, not 70. The 0x prefix marks hexadecimal, so 1A is 26. Without a prefix, 70 is just seventy, which is what makes the padded form so easy to misread. Accidental octal is a classic source of quiet arithmetic bugs.

Please log in or register to report this question.

79.

What will be the output of the program?

public class Sketch79
{
    private static void report()
    {
        System.out.println(Math.max(Integer.MIN_VALUE, Integer.MIN_VALUE + 1));
    }

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

Answer: C

Adding one to the smallest int stays inside the range and gives -2147483647, which is the larger of the two, so max returns it. No overflow occurs here because the addition moves toward zero. Only crossing an endpoint wraps.

Please log in or register to report this question.

80.

What will be the output of the program?

public class Item80
{
    void execute()
    {
        String s = "a";
        for (int i = 0; i < 3; i++) s += s;
        System.out.println(s + " " + s.length());
    }

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

Answer: D

Each pass doubles the string rather than adding one character, because s is appended to itself. Starting from one character, three doublings give eight. Each iteration also discards the previous String, which is why a builder is preferred in a loop.

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