Output of Java Programs | Set 52 (Strings Class)
Last Updated :
18 Sep, 2018
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.
Similar Reads
Output of Java Programs | Set 53 (String Comparison)
Prerequisite : String Comparison in Java 1. What should be the output of this program? class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) true b)
3 min read
Output of Java Programs | Set 42 (Arrays)
Prerequisite : Java Arrays Question 1. What is the output of this question? class Test1 { public static void main(String[] args) { int arr[] = new int[5]; int arr2[] = new int['a']; byte bt = 10; int arr3[] = new int[bt]; System.out.println(arr.length); System.out.println(arr2.length); System.out.pr
3 min read
Output of Java programs | Set 29
Question 1. What is the output of the following question? class Test1 { public static void main(String[] args) { int String = 65; int Runnable = 97; System.out.print(String + " : " + Runnable); } } Option A) Error B) A : a C) 65 : 97 D) None Output: C Explanation : We can use all predefine
2 min read
Output of Java Programs | Set 50
Q 1. What is the output of this program? [GFGTABS] Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } [/GFGTABS]Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run
3 min read
Output of Java Programs | Set 32
Prerequisite : Arrays in Java Question 1. What is the output of following program? class ArrayDemo { public static void main(String[] args) { int arr1[] = { 1, 2, 3, 4, 5 }; int arr2[5] = { 1, 2, 3, 4, 5 }; for (int i = 0; i < 5; i++) System.out.print(arr1[i] + " "); System.out.println(
3 min read
Output of Java Programs | Set 47 (Arrays)
Prerequisite : Arrays in Java Question 1. What is the output of this question? class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); int arr2[] = new int[3]; arr2[] =
2 min read
Output of Java Programs | Set 38 (Arrays)
Prerequisite : Arrays in Java Question 1. What is the output of this question class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; System.out.print(arr[-2]); } } Option A) 11 33 B) Error C) exception D) 11 -33 Output: C Explanation : We will get java.lang.ArrayIndexOutO
3 min read
Output of Java program | Set 27
Ques1. What is the output of the following? import java.util.*; public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } Options : A. The program has a compile error because the size of the array wasn't specified when dec
3 min read
Output of Java program | Set 26
Ques1: What is the output of this program? [GFGTABS] Java class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.
4 min read
Output of Java Programs | Set 21 (Type Conversions)
Prerequisite - Type Conversions in Java with Examples 1) What is the output of the following program? public class Test { public static void main(String[] args) { int value = 554; String var = (String)value; //line 1 String temp = "123"; int data = (int)temp; //line 2 System.out.println(da
3 min read