In: Computer Science
Given the following Java source code fragment double price[]; price = new double[16]; What's the name of the array? How many elements are in the array? What's the index of the first element in the array? What's the index of the last element in the array? What's the value of the element at index 3 after the last statement executes?
1. Answer:
Name of the array is price.
2. Answer:
The size of array describes the number of elements in the array.
Here, the size is declared in [ ] square brackets & the size is 16. Hence, it can hold 16 elements.
3.Answer:
Index is the address of the elements stored in an array.
Indexing always starts from zero (0)
Hence, the index of the first element is 0. (zero).
4. Answer:
The last index is size of the array minus 1.(Since, indexing starts from zero).
Therefore, the index of last element in the array is 15 .(16-1=15).
5. Answer:
Initially, since no values are assigned to the array, the default values for the "double" data type is assigned. Hence upon trying to print the value at index 3, use the following java code :
System.out.println(price[3]); |
It will print 0.0 as output.
==============END OF THE ANSWER==============