Java Programming - Java.lang Class - Discussion
Discussion Forum : Java.lang Class - Finding the output (Q.No. 19)
19.
What will be the output of the program?
String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);
Answer: Option
Explanation:
Both substring() and charAt() methods are indexed with a zero-base, and substring() returns a String of length arg2 - arg1.
Discussion:
15 comments Page 1 of 2.
Dakota Hobson said:
2 years ago
The output of this code would be "ape", because:
The variable `a` is initialized with the string "newspaper".
The `substring()` method is used to extract a new string from `a` starting at index 5 (inclusive) and ending at index 7 (exclusive). This results in the string "ap".
The 'charAt()' method is used to retrieve the character at index 1 of the string "ap", which is the character 'p'.
The variable 'b' is assigned the value of 'p'.
The string concatenation operator `+` is used to concatenate the string "ap" and the character 'p', resulting in the string "ape".
Finally, the 'println()' method is used to print the value of the variable `a`, which is now "ape".
The variable `a` is initialized with the string "newspaper".
The `substring()` method is used to extract a new string from `a` starting at index 5 (inclusive) and ending at index 7 (exclusive). This results in the string "ap".
The 'charAt()' method is used to retrieve the character at index 1 of the string "ap", which is the character 'p'.
The variable 'b' is assigned the value of 'p'.
The string concatenation operator `+` is used to concatenate the string "ap" and the character 'p', resulting in the string "ape".
Finally, the 'println()' method is used to print the value of the variable `a`, which is now "ape".
Arvin said:
9 years ago
How is a.substring(5,7) same as a.substring(5,6)?
Execute it and see, following is the right answer, everything else is wrong.
a = a.substring(5,7) = n(0)e(1)w(2)s(3)p(4)a(5)p(6)e(7)r(8) = ape
It will take from 5th, 6th, and 7th position.
Then next,
a.charAt(1) = e
Final result = apep
That's it.
Execute it and see, following is the right answer, everything else is wrong.
a = a.substring(5,7) = n(0)e(1)w(2)s(3)p(4)a(5)p(6)e(7)r(8) = ape
It will take from 5th, 6th, and 7th position.
Then next,
a.charAt(1) = e
Final result = apep
That's it.
Veena said:
1 decade ago
In string the position is starts from 0.
So count letters in "newspapers" as of a.substring(5,7); will count from 0 and its start taking letters after 5 till (7-1).
i.e till 6th letter.
So a.substring(5,7); will contains "ap"
So count letters in "newspapers" as of a.substring(5,7); will count from 0 and its start taking letters after 5 till (7-1).
i.e till 6th letter.
So a.substring(5,7); will contains "ap"
Shraddha said:
1 decade ago
Second argument of the Substring() method is the LENGTH from first argument.
a.Substring(5,7);//Returns:"aper"
a.charAt(1); // Returns: "p"
a = a + b; // a + b = ("aper" + "p") = "aperp"
a.Substring(5,7);//Returns:"aper"
a.charAt(1); // Returns: "p"
a = a + b; // a + b = ("aper" + "p") = "aperp"
Sundar said:
1 decade ago
a.substring(5,7); // Returns: "ap"
a.charAt(1); // Returns: "p"
a = a + b; // a + b = ("ap" + "p") = "app" stored in a.
Therefore the string "app" is the correct answer.
a.charAt(1); // Returns: "p"
a = a + b; // a + b = ("ap" + "p") = "app" stored in a.
Therefore the string "app" is the correct answer.
Amit Agrawalla said:
1 decade ago
@Ahmed : as a.substring(5,7); // Returns: "ap"
So now a has "ap" that is a is in zero place and p in the 1 place.
So charAt(1) implies 1 place of a. that is p.
So now a has "ap" that is a is in zero place and p in the 1 place.
So charAt(1) implies 1 place of a. that is p.
Muthuramu said:
1 decade ago
a.Substring(5,8);//Returns:"ape"(means 5,6,7)???
a.Substring(5,9);//Returns:"Aper"??? only 0 to 8 is available in string??
a.Substring(5,9);//Returns:"Aper"??? only 0 to 8 is available in string??
Mukesh said:
1 decade ago
a.substring(5,7) will return ape.
So the final output should be apep...
Please explain why the first line is returning ap only and not ape.
So the final output should be apep...
Please explain why the first line is returning ap only and not ape.
Abhi said:
10 years ago
a.substring (5, 7) will return ape.
And charAt (1) will be p. So answer will be apep.
How app anyone please explain?
And charAt (1) will be p. So answer will be apep.
How app anyone please explain?
Zen said:
9 years ago
substring(beginIndex, endIndex)
beginIndex - inclusive.
endIndex - exclusive.
That's why a.substring(5,7) = ap.
beginIndex - inclusive.
endIndex - exclusive.
That's why a.substring(5,7) = ap.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers