Consider the code block below. What is the value of amount when this method is called twice, the first time using cookieJar(7) and then using cookieJar(22)?

public static int cookieJar(int addCookies) {

int amount = 0;
amount += addCookies;
return amount;
}

a. 7
b. 15
c. 22
d. 29
e. An error occurs

Respuesta :

fichoh

Answer: 29

Explanation:

The variable amount is given a initial value of 0

Therefore, the first method call:

cookieJar(7)

amount + cookieJar

amount = 0 + 7 = 7

And the value of 7 is returned for amount at the start of the block

Thus, when cookieJar is called the second time ;

cookieJar(22)

amount + cookieJar

amount = 7 + 22 = 29

And the value of 29 is returned for amount at the start of the block