Machine-translated by Google. Questions are authored in English.
Top

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());
    }
}

  • A.The program prints true false 3 A1
  • B.The program prints false false 2 A1
  • C.The program prints false true 2 B2
  • D.The program prints true true 3 B2

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.