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`
There was a race of 2280 meters between P & Q on a circular track of 380 meters. First time they meet during the race after 4 minutes of starting of rac...
- The ratio of the age of R after 3 years to the age of S after 6 years is 3:2. The total age of S and T after 10 years is 84 years. T’s age 6 years ago wa...
- The ratio of present ages of Sneha to that of Akash is 6:8 while the ratio of present ages of Vikram to that of Akash is 5:8. If the age of Vikram, 3 years...
Present ages of P and Q are in ratio 5:4, respectively. Present ages of R and P are in ratio 3:4, respectively. If the difference between ages of Q and ...
Present age of ‘A’ is 40% more than that of ‘B’. If 14 years hence from now, ‘B’ will be 8 years younger than ‘A’, then find the sum of ...
At present, Peter is Seven years younger than Adam. Adam’s age twenty years hence will be equal to thrice that of peter Five years ago. What will ...
What is the value of 'x' such that, given their current ages are in a 3:7 ratio, 'x' years ago, the ratio of 'Adarsh' and 'Malik's ages was 1:3, and if ...
K is as much younger than M as he is older than O. If the sum of the ages of M and O is 100 years, what is definitely the difference between M and K’s...
Difference of ages of father and son is 24 years. Two years back age of father was twice the son's present age.
What is father's age now?
A’s age is 120% of what he was 4 years ago, But 60% of what it will be after 4 years. What is his present age?