Consider the following method contract and client/** * Adds n to this. *... *@up
ID: 3802464 • Letter: C
Question
Consider the following method contract and client/** * Adds n to this. *... *@updates this * @restores n * @ensures this = #this + n */public void add(NaturalNumber n) {...} NaturalNumber num = new NaturalNumber2(2); num.add(num); What is the value of num after the method call? a) num = 0 b) num = 2 c) num = 4 d) Cannot tell from the information provided. What are the values of array and index after the call to bar? private static void bar(int[] a, int i) {a[i]++; i++;} int[] array = {1, 2, 3}; int index = 1; bar(array, index); a) array = {1, 2, 3}, index = 1 b) array = {1, 2, 3}, index = 2 c) array = {1, 3, 3}, index = 1 d) array = {1, 3, 3}, index = 2Explanation / Answer
1. Cannot tell from the information provided because only calling is given but no any definition is given. So, without having any information about definition we cant tell about its calling. So information provided is insufficient.
2. Right option is (c). When bar function is called, it will move to its definition as a[i] contains 1 as first element. Then a[i] means a[1] i.e 2. Its value is incremented and we get 3. Again it is called and array becomes {1,3,3} and index value is 1 because i is post increment. Firstly, value will be stored and then it will be incremented.