Output of Java Programs | Set 52 (Strings Class)

Prerequisite : Basics of Strings class in java

1. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:



java
java programming

2. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:


java programming
java programming

3. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

class demo3 {
    public static void main(String args[])
    {
        byte[] arr = { 97, 98, 99, 100, 101 };
        String str2 = new String(arr);
  
        System.out.println(str2);
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

class demo4 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        char ch = str.charAt(2);
        System.out.println(ch);
    }
}

chevron_right


Output:

v

Explanation:
charAt() function returns the character at the specified index.

5. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


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



filter_none

edit
close

play_arrow

link
brightness_4
code

class demo6 {
    public static void main(String args[])
    {
        String str = "Java Programming";
        char arr[] = new char[20];
        arr = str.toCharArray();
        System.out.println(arr);
    }
}

chevron_right


Output:

Java Programming

Explanation:
toCharArray() method converts the string into a character array.

7. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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"));
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

class demo13 {
    public static void main(String args[])
    {
        String str = "Java Programming";
  
        System.out.println(str.replace('a', '9'));
    }
}

chevron_right


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



filter_none

edit
close

play_arrow

link
brightness_4
code

class demo14 {
    public static void main(String args[])
    {
        String str = "Java";
        String str1 = "Programming";
  
        System.out.println(str.concat(str1));
    }
}

chevron_right


Output:

JavaProgramming

Explanation:
concat() method simply concatenates one string to the end of the other.

15. What is the Output Of the following Program

filter_none

edit
close

play_arrow

link
brightness_4
code

class demo15 {
    public static void main(String args[])
    {
        String str = "    Java          Programming                  ";
  
        System.out.println(str.trim());
    }
}

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubh276



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.