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.
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.
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??
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"
Ahmed said:
1 decade ago
How a.charAt(1) returns "p"?
Vinod said:
1 decade ago
In obj a now "ape".
a.charAt(0) means "a".
a.charAt(1) means "p".
a.charAt(0) means "a".
a.charAt(1) means "p".
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.
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"
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.
Aaa said:
1 decade ago
7-5 = 2 means length is 2.
a = ap.
a.charAt(1) = p.
a = ap.
a.charAt(1) = p.
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers