Output of Java Programs | Set 52 (Strings Class)

Prerequisite : Basics of Strings class in java

1. What is the Output Of the following Program

Output:

java
java programming

2. What is the Output Of the following Program

Output:


java programming
java programming

3. What is the Output Of the following Program

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

Output:

v

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

5. What is the Output Of the following Program

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

Output:

Java Programming

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

7. What is the Output Of the following Program

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

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

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

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

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

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

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

Output:

Java Programming

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

15. What is the Output Of the following Program

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

Recommended Posts:


    Article Tags :


    Be the First to upvote.


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