Ask Question
4 February, 18:45

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?

+2
Answers (1)
  1. 4 February, 21:03
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers