Question
Consider the following pseudo-code: ```
function calculate(arr): sum = 0 for i from 0 to length(arr) - 1: if arr[i] % 2 == 0: sum = sum + arr[i] else: sum = sum - arr[i] return sum my_array = [1, 2, 3, 4, 5] result = calculate(my_array) print(result) ``` What will be the output of this code?Solution
Dry Run: `my_array = [1, 2, 3, 4, 5]` `sum = 0` `i = 0, arr[0] = 1` (odd): `sum = 0 - 1 = -1` `i = 1, arr[1] = 2` (even): `sum = -1 + 2 = 1` `i = 2, arr[2] = 3` (odd): `sum = 1 - 3 = -2` `i = 3, arr[3] = 4` (even): `sum = -2 + 4 = 2` `i = 4, arr[4] = 5` (odd): `sum = 2 - 5 = -3` Return `sum = -3`
100 litres of a mixture contains 40% milk and rest water. If ‘x’ litres of milk is added into the mixture then quantity of water in the final mixtur...
Vessel contains 40 liters wine. If x liters of wine is taken out and replaced with water, this process is repeated two more times and then quantity of ...
A 360-liter mixture consists of milk and water in a 5:4 ratio. If 7.5% of the mixture is removed and replaced with the same amount of water, what is the...
A container was initially filled to the brim with milk. After removing 52% of the milk, 348 ml remained in the container. Calculate the original volume ...
- In a 80 litres mixture of juice and soda, the ratio of juice to soda is 2:3. If 30 litres of this mixture is removed and replaced with 10 litres of soda, f...
A fuel blend composed of diesel and petrol is mixed in a ratio of 5:3, respectively. If one-fourth of this blend is removed and replaced with 24 liters ...
In a mixture, the quantity of sugar is 25% of the quantity of water. On removing 70 litres mixture and adding 10 litres each of sugar and water, the qua...
A recipe for a cake calls for 2.5 cups of milk and 3 cups of flour. With this recipe, a cake was baked using 14 cups of flour. How many cups of milk wer...
A trader mixes three types of fruit juices. Juice A costs $10 per liter, Juice B costs $15 per liter, and Juice C costs $20 per liter. The mixture is so...
- Out of a 200-litre solution of juice and soda, the ratio of juice to soda is 1:4. How many litres of juice should be added to make the new ratio 3:8?