Strings


  1. What is the output of this program?

    public class output
    {
    public static void main(String args[])
    {
    String str1 = "Hello i love interview mania";
    String str2 = new String(str1);
    System.out.println((str1 == str2) + " " + str1.equals(str2));
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The == operator compares two object references to see whether they refer to the same instance, where as equals() compares the content of the two objects.


  1. What is the output of this program?

    public class Result
    {
    public static void main(String args[])
    {
    String ch = "hello i love interview mania";
    boolean var0;
    var0 = ch.startsWith("hello");
    System.out.println(var0);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence true is stored in var0.



  1. Which of these data type value is returned by equals() method of String class?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.


  1. What is the value returned by function compareTo() if the invoking string is less than the string compared?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.



  1. Which of these methods of class String is used to check whether a given object starts with a particular string literal?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is a specialized form of method regionMatches().