Private static void mystery 1(Natural Number[] arr) { arr[0] - new Natural Numbe
ID: 3802801 • Letter: P
Question
Private static void mystery 1(Natural Number[] arr) { arr[0] - new Natural Number lL(2); arr[l].multiply(arr[0]) private static void mystery2(NaturalNumber n1) {nl.increment(); n1 = new NaturalNuaber1 L(5); private static void mystery3(String s1) { s1 = s1. substring(2); s1 = s1 + s1; public static void main(String[] args) { Natural Number[] nums = new Natural Number[2]; nums[0] = new NaturalNuaberll(l); nums[l] = new NaturalNuaberlL(2); NaturalNumber a = new NaturalNumberll(3); NaturalNumber b = new NaturalNumberlL(4); String s - "Ohio"; b.add(b.divide(nums[l])); mysteryl(nums); mystery2(a); mystery 3 (s); } Refer to the code provided above. When the program ends, what will be the final value of the variable, b? b = 6 b = 4 b = 2 b = 8 Refer to the code provided above. When the program ends, what will be the final value of the elements in the nuns array? nums [0] = 1, nums [1] = 2 nums [0] = 2, nums [1] = 2 nums [0] = 1, nums [1] = 4 nums [0] = 2, nums [1] = 4 Refer to the code provided above. When the program ends, what will be the final value of the variable, a? a = 5 a = 4 a- 3 a = 6 Refer to the code provided above. When the program ends, what will be the final value of the variable, s? s = Ohio" s = "ioio" s = OhOh" s- "OhioOhio"Explanation / Answer
Answer:
When the program ends, the final value of the elements in nums array
are
d) nums[0] = 2, nums[1] = 4
Explanation:
As we know execution of a program starts from the left brace of the
main method and follows sequentially line by line and if any method
is called, the control completes execution of all the statements in that
method and returns back to the calling method.
So first statement
NaturalNumber[] nums = new NaturalNumber[2]; creates an array
which can hold two values of NaturalNumber type.
Then in the next statement
nums[0] = new NaturalNumber1L(1);
It allocates memory to the first array element and assigns a value 1
by invoking parameterized constructor and passing value 1
Then in the next statement
nums[1] = new NaturalNumber1L(2);
It allocates memory to the second array element and assigns a value 2
by invoking parameterized constructor and passing value 2
Then memory is allocated for the variables a ,b,s.
then the statement b.add..... is executed and it will not effect any value
in the array nums[] as it is just using the values only.
Then the method mystery1(nums) is called by passing nums array
to the method.
In the mystery1 method
nums array is copied to arr
Now value of arr[0] will be 2 as per the first statement.
and value of arr[1] will be 4, because already it contains the value 2
and it is multiplied with the recent value of arr[0] i.e. 2.
So 2 * 2 = 4
As the array is passed by reference values in nums[] will also be changed.
i.e. nums[0] = 2 and nums[1] =4
Then the control goes back to the main method .
There are two function calls which will not effect nums[] array.
So the values of nums[0] = 2 and nums[1] =4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.