For the questions below assume an int array candy stores the
For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. What does the following code do? Scanner scan = Scanner.create(System.in); int value1 = scan.nextInt( ); int value2 = scan.nextInt( ); bars[value1] += value2; 1. adds value1 to the number of bars sold by child value2 2. adds 1 to the number of bars sold by child value1 and child value2 3. adds 1 to the number of bars sold by child value1 4. inputs a new value for the number of bars sold by both child value1 and child value2 5. adds value2 to the number of bars sold by child value1
Solution
Answer: 5. adds value2 to the number of bars sold by child value1
Explanation:
According to the question candy[j] is the number of candy bars sold by child j
hence bars[value1]=number of candy bars sold by child value1
bars[value1]+=value2 is nothing but bars[value1]=bars[value1]+value2
hence now bars[value1]=old value of bars[value1] + value2
hence ,this adds value2 to the number of bars sold by child value1
Note:Please ask in case of any doubt,Thanks,God bless you.
![For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold](/WebImages/33/for-the-questions-below-assume-an-int-array-candy-stores-the-1095470-1761577584-0.webp)