93.3k views
1 vote
Consider the following Java code fragment: class Widget \{ public int value; \} Widget [] widgetList \( = \) new Widget [5]; How many widget objects exist after this code fragment executes? Answer:-----

User JJS
by
7.9k points

1 Answer

4 votes

Final answer:

The code fragment initializes an array to hold five Widget objects but does not create any Widget objects; zero widget objects exist after execution.

Step-by-step explanation:

After the given Java code fragment executes, zero widget objects exist. The code Widget [] widgetList = new Widget [5]; creates an array that can hold references to five Widget objects, but it does not actually create the Widget objects themselves. To have widget objects, you would need to initialize each element of the array with a new Widget, like this: widgetList[i] = new Widget(); for each index i in the array.

User Rhys Stephens
by
7.5k points