Discussion
What will be the output of the program?
import java.util.*;
public class Sample94
{
public static void main(String[] args)
{
Set<String> seats = new TreeSet<String>();
Collections.addAll(seats, "A1", "B2");
boolean again = seats.add("A1");
boolean gone = seats.remove("C3");
System.out.println(
again + " " + gone + " " + seats.size() + " " + seats.first());
}
}
Answer: B
add returns false because A1 is already present and the set is unchanged, and remove returns false because C3 was never there. Neither throws. The size stays at 2, and first reports the lowest key under the natural ordering, A1. These methods report a CHANGE, not success.
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