Method Calls in Expressions 🌶️🌶️
A method call inside a larger expression runs first, and its return value takes its place in the rest of the expression -- same as any other value. Trace each one the same way: work out the innermost call first. For questions 1-4, assume square(n) returns n n. For questions 5-6, assume addAmount(n, amount) returns n + amount and doubleIt(n) returns n 2 (the same two methods from comboBonusReport).
1. Trace n and result after each line executes.
int n = 3;int result = square(n) + 1;n = n + 1;result = square(n) + 1;
If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.
2. Trace base and total after each line executes.
int base = 4;int total = doubleIt(addAmount(base, 3));base = base + 2;total = addAmount(doubleIt(base), 1);
If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.
3. Trace x and y after each line executes. (These calls use the real Math class.)
double x = -9.0;double y = Math.sqrt(Math.abs(x));x = Math.pow(y, 2);y = Math.abs(x - 10);
If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.
4. What is the value of a at the end?
int a = 2;int b = square(a) + square(a + 1);a = b - 5;
8
5. What is the value of x at the end?
int x = 3;int y = addAmount(doubleIt(x), doubleIt(x));x = y / 2;
6
6. What is the value of d at the end? (Real Math class calls.)
double d = 16.0;double r = Math.sqrt(d) + Math.abs(-3.0);d = Math.pow(r, 2);
49.0
Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.