Discussion
What will be the output of the program?
import java.util.*;
public class Sketch92
{
public static void main(String[] args)
{
List<String> l = new ArrayList<String>(
Arrays.asList("b", "a", "c"));
String[] arr = l.toArray(new String[0]);
Arrays.sort(arr);
System.out.println(arr[0] + " " + l.get(0));
}
}
Answer: D
toArray builds a new array holding the same references, so sorting it rearranges the array only and the list keeps its original order. The array starts with a while the list still starts with b. Nothing here is a view; the array and the list are independent containers.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development