3) Run the following code. The sum method in this program violates a design prin
ID: 3913639 • Letter: 3
Question
3) Run the following code. The sum method in this program violates a design principle of the book (that methods should not try to modify an argument) when it assigns 5 to a, and 6 to b. Certainly these values are changed; this can be seen by examining the value the sum method returns. But what about arguments x and y? Are their values changed, too? In other words: Do the assignments made in the method body have side effects in the main program? public class Area public static void main(String[] args) int x 2 int y 3: System.out.println ("x: " +x + " y: "+ y+ " Sum: " + sum (x, y)); Computes the sum of two arguments. @param a an int operand to be added eparam b another int operand @return the sum of a and b public static int sum (int a, int b) a5 return abiExplanation / Answer
No, there is no effect in the main() part of the program. The arguments are passed by value in sum() function. So, any change to them inside the function will not change the original values of the variables passed as arguments. But the values changes inside the function.
So, inside the main() function, the output is
x : 2 y : 3 Sum : 11
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.