Open In App
Related Articles

Output of Java Programs | Set 52 (Strings Class)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program
class demo1 {
    public static void main(String args[])
    {
        String str1 = "java";
        char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p'
        'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' };
        String str2 = new String(arr);
        System.out.println(str1);
        System.out.println(str2);
    }
}

                    
Output:
java
java programming
2. What is the Output Of the following Program
class demo2 {
    public static void main(String args[])
    {
        String str1 = "";
        char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p'
        'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' };
        String str2 = new String(arr);
        String str3 = new String(str2);
  
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}

                    
Output:

java programming
java programming
3. What is the Output Of the following Program
class demo3 {
    public static void main(String args[])
    {
        byte[] arr = { 97, 98, 99, 100, 101 };
        String str2 = new String(arr);
  
        System.out.println(str2);
    }
}

                    
Output:
abcde
Explanation: The constructor String( byte[] ) converts the bytes to corresponding characters i.e. 97 converted to character ‘a’ 4. What is the Output Of the following Program
class demo4 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        char ch = str.charAt(2);
        System.out.println(ch);
    }
}

                    
Output:
v
Explanation: charAt() function returns the character at the specified index. 5. What is the Output Of the following Program
class demo5 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        char arr[] = new char[10];
        str.getChars(0, 4, arr, 0);
        System.out.println(arr);
    }
}

                    
Output:
Java
Explanation: The syntax of the method is: getChars(startindex, numOfCharacters, arrayName, startindexOfArrat). So from the string, starting from 0th index, first four characters are taken. 6. What is the Output Of the following Program
class demo6 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        char arr[] = new char[20];
        arr = str.toCharArray();
        System.out.println(arr);
    }
}

                    
Output:
Java Programming
Explanation: toCharArray() method converts the string into a character array. 7. What is the Output Of the following Program
class demo7 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        String str1 = "Java Programming";
  
        String str2 = str1;
        if (str.equals(str1))
            System.out.println("Equal Case 1");
        if (str == str1)
            System.out.println("Equal Case 2");
    }
}

                    
Output:
Equal Case 1
Equal Case 2
Explanation: The equals() method compares the characters inside a String object.Thus str.equals(str1) comes out to be true. The == operator compares two object references to see whether they refer to the same instance. Now str points to “Java Programming” and then str1 also points to “Java Programming”, hence str == str1 is also true. 8. What is the Output Of the following Program
class demo8 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        String str1 = "Programminggggg";
  
        if (str.regionMatches(5, str1, 0, 11))
            System.out.println("Same");
    }
}

                    
Output:
Same
Explanation: The syntax of the function is: regionMatches( startIndex, stringS, stringS’s_startIndex, numOfCharacters) so starting from index 5, string str1 is compared from index 0 till 11 characters only. Hence output is ‘Same’ 9. What is the Output Of the following Program
class demo9 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        String str1 = "Java";
        if (str.startsWith("J"))
            System.out.println("Start Same");
  
        if (str.endsWith("T"))
            System.out.println("End Same");
    }
}

                    
Output:
Start Same
Explanation: The startsWith() method determines whether a given String begins with a specified string. The endsWith() determines whether the String in question ends with a specified string. 10. What is the Output Of the following Program
class demo10 {
    public static void main(String args[])
    {
        String str = "JavaProgramming";
        String str1 = "Java";
  
        System.out.println(str.compareTo(str1));
        System.out.println(str1.compareTo(str));
    }
}

                    
Output:
11
-11
Explanation: The String method compareTo( ) serves the purpose of comparing two strings. Whether one string is less than, greater than or equal to the second string. In case 1, comparing JavaProgramming with Java implies JavaProgramming is greater than Java by 11 characters. In case 2, comparing Java with JavaProgramming implies Java is lesser than JavaProgramming by 11 characters (-11). 11. What is the Output Of the following Program
class demo11 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        String str1 = "Java";
  
        System.out.println(str.indexOf("a"));
        System.out.println(str.indexOf("m"));
  
        System.out.println(str.lastIndexOf("a"));
        System.out.println(str.lastIndexOf("m"));
    }
}

                    
Output:
1
11
10
12
Explanation: indexOf( ) Searches for the first occurrence of a character or substring. lastIndexOf( ) Searches for the last occurrence of a character or substring. 12. What is the Output Of the following Program
class demo12 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        String str1 = "Java";
        String str2 = str.substring(5);
        System.out.println(str2);
        System.out.println(str.substring(5, 9));
    }
}

                    
Output:
Programming
Prog
Explanation: constructor substring(int startIndex) takes the substring starting from the specified index till end of the string constructor substring(int startIndex, int endIndex) takes the substring from startIndex to endIndex-1 13. What is the Output Of the following Program
class demo13 {
    public static void main(String args[])
    {
        String str = "Java Programming";
  
        System.out.println(str.replace('a', '9'));
    }
}

                    
Output:
J9v9 Progr9mming
Explanation: The replace( ) method replaces all occurrences of one character in the invoking string with another character. Hence ‘a’ replaced with a ‘9’. 14. What is the Output Of the following Program
class demo14 {
    public static void main(String args[])
    {
        String str = "Java";
        String str1 = "Programming";
  
        System.out.println(str.concat(str1));
    }
}

                    
Output:
JavaProgramming
Explanation: concat() method simply concatenates one string to the end of the other. 15. What is the Output Of the following Program
class demo15 {
    public static void main(String args[])
    {
        String str = "    Java          Programming                  ";
  
        System.out.println(str.trim());
    }
}

                    
Output:
Java          Programming
Explanation: The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. But it does not remove the spaces present between two words.

Last Updated : 18 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads