The right answer is the letter A, remember that String is immutable, the "concat" method inside the for each as it didn't receive a variable wasn't storing anything, so it didn't change. And then at the time of printing, it printed A B because nothing has changed.
NOTE: To concatenate a String variable, it would have to be -> | strs[idx] = strs[idx] + " element " + idx; | Instead of what's in the code.
Answer is A.
To test:
public static void main(String[] args) {
String[] strs = {"A", "B"};
int idx = 0;
for(String s : strs) {
strs[idx].concat(" element " + idx);
idx++;
}
for(idx = 0; idx < strs.length; idx++) {
System.out.println(strs[idx]);
}
}
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
iSnover
Highly Voted 10 months agoRajeevkuamr
Most Recent 6 months, 4 weeks agoakbiyik
8 months agocarloswork
9 months, 2 weeks ago