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`
In a scholarship test, 'P' achieved 28% of the total marks and fell short of passing by 45 marks. Meanwhile, 'Q' scored 44% of th...
- During a club election between members 'X' and 'Y', 10% of all votes were invalid. If 'X' got 52% of the valid votes and 'Y' secured 21,168 valid votes, de...
- The sum of two numbers, x and y, is 640. If x is reduced by 8% and y is raised by 12%, the two values become equal. Determine the value of y.
A girl found the answer for the question “subtract the sum 1/2 and 1/8 from unity and express the answer in decimals” as 1.5. The percentage...
- X is 40% less than Y and Z is 30% more than W. If W is 25% more than X, then which of the following is true?
A person spent 25% of his monthly income on food and 55% of the remaining on rent. If amount spent on rent is Rs 825, then find the amount spent on food.
The sum of the monthly incomes of ‘A’, ‘B’ and ‘C’ is Rs. 55000 which is 4 times the monthly income of ‘C’. If ‘A’ spends 60% of his...
In a given years, A sold 1850 caps while B sold 20% more number of caps than A and C sold 5% less number of caps than B. Find the ratio between the numb...
- Each year, the population of a village rises by 8%. If after 2 years the population will reach 34,992, what is the population now?
The population of a city is decreased by 12% in the first year and then increased by 25% in the second year. Find the population of the city at the end...