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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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
Java
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? Java class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) tru
3 min read
Output of Java Programs | Set 54 (Vectors)
Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java import java.util.*; class demo1 { public static void main(String[] args) { Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); } } Output: 20 0 Explanation: function - int
6 min read
Output of Java programs | Set 29
Question 1. What is the output of the following question? JAVA 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 pred
2 min read
Output of Java Programs | Set 47 (Arrays)
Prerequisite : Arrays in Java Question 1. What is the output of this question? JAVA 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 JAVA 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.ArrayInde
3 min read
Output of Java program | Set 27
Ques1. What is the output of the following? Java 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 whe
3 min read
Output of Java Programs | Set 30
QUE.1 What is the output of this program ? JAVA public class Prg { public static void main(String args[]) { System.out.print('A' + 'B'); } } OPTION a) AB b) 195 c) 131 d) Error Answer: c Explanation: Here, âAâ and âBâ are not strings they are characters. âAâ and âBâ will not concatenate. The ASCII o
2 min read
Output of Java Programs | Set 31
Prerequisite : Arrays in Java 1. What will be the output of the following program? Java public class Test { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(arr); } } Options: 1. 1 2. Compile-time error 3. 1 2 3 4 5 4. [I@Hashcode_in_Hexadecimal Output: The
3 min read
Output of Java Programs | Set 37 (If-else)
Prerequisite: if else, for loops 1. What will be the output for the following program? JAVA public class Test { public static void main(String[] args) { for (;;) System.out.println("GEEKS"); } } Options: 1.GEEKS 2.Compile time error 3.Run time Exception 4.GEEKS (Infinitely) The answer is o
3 min read
Output of Java Program | Set 3
Predict the output of the following Java Programs: Example1: Java // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Printin
3 min read