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

Java.lang Class - Finding the output Questions and Answers

1.

What will be the output of the program?

public class Sample01
{
    public static void main(String[] args)
    {
        String s = "java";
        s.concat(" lang");
        System.out.println(s);
    }
}

Answer: A

String is immutable, so concat returns a new String and leaves the original untouched. The returned value is discarded here because it is never assigned, so s still refers to java. Assigning the result back, s = s.concat(" lang");, is what the author intended.

Please log in or register to report this question.

2.

What will be the output of the program?

public class Probe02
{
    static void show()
    {
        String a = "hi";
        String b = "hi";
        System.out.println((a == b) + " " + a.equals(b));
    }

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

Answer: B

Both are string literals with the same characters, so the compiler puts a single interned String in the pool and both variables reference it. The identity test is therefore true, and equals compares contents and is also true. Only new String("hi") would separate them.

Please log in or register to report this question.

3.

What will be the output of the program?

public class Demo03
{
    void run()
    {
        String city = "delhi";
        String typed = new String("delhi");
        boolean sameObject = (city == typed);
        System.out.println(sameObject + " " + city.equalsIgnoreCase(typed));
    }

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

Answer: C

new String is required to create a distinct object, so the two variables reference different objects and the identity test is false. equalsIgnoreCase compares characters, not references, so it is true. This is why identity comparison of strings is a bug rather than a shortcut.

Please log in or register to report this question.

4.

What will be the output of the program?

public class Snippet04
{
    public static void main(String... args)
    {
        String word = "abcdef";
        String part = word.substring(2, 4);
        System.out.println(part);
    }
}

Answer: D

substring takes the start index inclusive and the end index exclusive, so characters at positions 2 and 3 are returned and position 4 is not. That gives cd, two characters, which is always end minus start. Expecting cde is the usual off-by-one.

Please log in or register to report this question.

5.

What will be the output of the program?

public class Listing05
{
    Listing05()
    {
        String raw = "  pad  ";
        String clean = raw.trim();
        System.out.println(clean.length());
    }

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

Answer: A

Correct answer: The program prints 3

trim() removes leading and trailing whitespace and returns a new string, so " pad " becomes "pad" with a length of 3. The original is unchanged, strings being immutable - which is why the result must be assigned. The 5 offered is the length of the untrimmed string.

Please log in or register to report this question.

6.

What will be the output of the program?

public class Fragment06
{
    public static void main(String[] args) throws Exception
    {
        String fruit = "banana";
        String masked = fruit.replace("an", "X");
        System.out.println(masked);
    }
}

Answer: B

replace substitutes every occurrence, not just the first. banana contains an twice, at positions 1 and 3, so both are replaced and four characters remain: b, X, X, a. replaceFirst is the method that stops after one match, which would have left bXana.

Please log in or register to report this question.

7.

What will be the output of the program?

public class Example07
{
    private static void report()
    {
        String greeting = "hello";
        int first = greeting.indexOf("l");
        int last = greeting.lastIndexOf("l");
        System.out.println(first + " " + last);
    }

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

Answer: C

indexOf scans from the start and finds the first l at position 2, while lastIndexOf scans from the end and finds one at position 3. Both indices are zero-based. A missing character would give -1 from either method rather than an exception.

Please log in or register to report this question.

8.

What will be the output of the program?

public class Program08
{
    void execute()
    {
        String upper = "Java";
        int diff = upper.compareTo("java");
        System.out.println(diff);
    }

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

Answer: D

compareTo is case sensitive and returns the difference between the first characters that differ. Here that is J against j, which are 74 and 106, so the result is -32. Only the sign is contractually meaningful; the magnitude is a String implementation detail.

Please log in or register to report this question.

9.

What will be the output of the program?

public class Trial09
{
    public static void main(String[] args)
    {
        String s = "a,b,,c";
        System.out.println(s.split(",").length);
    }
}

Answer: A

split discards trailing empty strings but keeps empty fields in the middle, so a, b, the empty field and c all survive, giving 4. Had the empty fields been at the end they would have been dropped. Passing a negative limit keeps every one of them.

Please log in or register to report this question.

10.

What will be the output of the program?

public class Case10
{
    static void show()
    {
        String small = "abc";
        boolean same = small.toUpperCase().equals("ABC");
        System.out.println(same);
    }

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

Answer: B

toUpperCase returns a new String holding ABC, and equals compares it with the literal ABC, so the result is true and println prints the boolean as true. The original string is unchanged, but nothing here depends on that.

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