need to change more than two or three lines in a function th

need to change more than two or three lines in a function, think harder or ask for help. Exercise 3: Variable scoping Consider the following program (see lab2.zip for the source): public class scopeTest static int num1 40 static int num2 41; static int nun3 e; public static void main(String[] args) int num! add(10, 11); add(nuni, num2); add(num2, nun3)i system out.println(num3 public static void add(int num1, int num2) R9 nun! 1 open the program in jGrasp and nun it. The program will output four numbers. In a text file (scopetest.txt) answer the following questions about this program. The line numbers are there to help refer to specific lines ofthe program.) 1. Each ofthe first three numbers are output by the call to println on line The calls to the add function lines 9 to 12 for these three outputs are found on lines 5 to 7 a) For the call on line 5, what are the values of numland num2 at the beginning ofadd? These are the numl and num2 declared by line 9) the function add? Make sure you explicitly identify which value is num1 and which is c For each of num1, num2, and num3 referenced on lines 6 and 7, which line declared that 2. The variable num3 referenced on line 100 is declared on which line of the program? delete

Solution

1.
a) The value of num1 and num2 are 10 and 11. That\'s because value of num1 and num2 are overwritten by calling add(10,11)

b) For the line 6 the value of num1 and num2 would be 40 and 41. Because num1 and num2 declared as static variable outside the function. So those variable has scope of entire program.

For the line 7 the value of num2 is 41 because num2 is declared with value 41 at line 2 which is not changed yet. The value of num3 would be 2. That\'s because every time the function add is called value of variable num3 gets increased by 1. So num3 was initialized by 0 at line 3 then add function was called 2 times before executing line 7. So the value of num3 also increased by 2 i.e. value of variable num3 is 2

c)
For line 6 num1 declared on line 1 and num2 declared on line 2.
For line 7 num2 declared on line 2 and num3 declared on line 3. Note that there is another declaration of num3 at line 4 but as num3 is declared as static variable on line 3, all other declaration of num3 in same scope of line 3 becomes irrelevant.

2. Again, for line 10 num3 declared on line 3. Though there is another declaration of num3 at line 4 but as num3 is declared as static variable on line 3, all other declaration of num3 in same scope of line 3 becomes irrelevant.

 need to change more than two or three lines in a function, think harder or ask for help. Exercise 3: Variable scoping Consider the following program (see lab2.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site