Given the following method declaration, public static int doThis( int a, double
ID: 3765580 • Letter: G
Question
Given the following method declaration, public static int doThis( int a, double b, int[] c, boolean d),
Identify which of the calling statements are valid? If they are valid, identify the correspondence between parameters and arguments. If not valid, provide reason(s) for the same. Provide also the possible declarations that need to be in the calling method.
1) doThis( one, two, three, four);
2) what = doThis( one, two, three, four);
3) which = doThis(20, 2.5, 43, 1);
4) which = doThis(20, 2* a, 43, false);
Explanation / Answer
1) doThis( one, two, three, four);
Given the four variables, one two three and four, this function call input validation, is dependent on the type of the variables passed as input to this function.
If one is a variable of type int, two is a variable of type double, and three is an integer array, and four is a variable of type boolean, this function call can be considered valid, and invalid otherwise.
2) what = doThis( one, two, three, four);
Everything remains the same as in the problem 1. The only difference is, the returned value is not stored into the variable in problem 1). Here you are storing the returned value in to the variable what. Note that, as the return type of this function is int, the variable what should be of type int, unless otherwise, either invalid data will be stored, or loss of precision will happen.
3) which = doThis(20, 2.5, 43, 1);
This leads to an error message, as both the third and fourth parameter doesn't match with the variables in the calling function.
4) which = doThis(20, 2* a, 43, false);
This also leads to an error. If the variable a is of type int or float, there is no problem with the second variable but the third variable should be an array, where as an integer constant has been passed, which leads to an error.
If you have any further queries, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.