Ask Question
25 March, 10:46

Consider the following code segment: ArrayList bulbs = new ArrayList (); bulbs. add (new Light ()); bulbs. remove (0); bulbs. add (new Light ()); Light b = new Light (); bulbs. add (1, b); bulbs. add (new Light ()); bulbs. remove (0); bulbs. add (new Light ()); bulbs. remove (2); bulbs. add (new Light ()); bulbs. add (1, new Light ()); After running the code, what is the size of bulbs?

+1
Answers (1)
  1. 25 March, 10:52
    0
    To answer this, lets run through the code:

    ArrayList bulbs = new ArrayList (); / / create a new empty list

    bulbs. add (new Light ()); / / size of bulbs = 1

    bulbs. remove (0); / / remove item at index 0. size of bulbs = 0

    bulbs. add (new Light ()); / / size of bulbs = 1

    Light b = new Light ();

    bulbs. add (1, b); / / size of bulbs = 2

    bulbs. add (new Light ()); / / size of bulbs = 3

    bulbs. remove (0); / / remove bulb at index 0. Size of bulbs = 2

    bulbs. add (new Light ()); size of bulbs = 3

    bulbs. remove (2); / / remove bulb at index 2. size of bulbs = 2

    bulbs. add (new Light ()); / / size of bulbs = 3

    bulbs. add (1, new Light ()); / / inserted at position 1. size of bulbs = 4

    The final answer. Size of bulbs = 4.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the following code segment: ArrayList bulbs = new ArrayList (); bulbs. add (new Light ()); bulbs. remove (0); bulbs. add (new ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers