Post-Conditions

Help Questions

AP Computer Science A › Post-Conditions

Questions 1 - 3
1

double square(double n){

return n*n;

}

What MUST be true immediately after the above code snippet has run?

The result will be a positive number.

The result will be a negative number.

The result will be stored in a new variable.

The value of the input parameter changes.

It is impossible to tell.

Explanation

Squaring a real number will always produce a positive number. The result does not have to be stored in a new variable; it could be a value that is only needed for a one-off expression, thus, not worthy to be stored in memory. Lastly, since the input was passed by value and not by reference, its initial value will stay the same.

2

double square(double n){

return n*n;

}

What MUST be true immediately after the above code snippet has run?

The result will be a positive number.

The result will be a negative number.

The result will be stored in a new variable.

The value of the input parameter changes.

It is impossible to tell.

Explanation

Squaring a real number will always produce a positive number. The result does not have to be stored in a new variable; it could be a value that is only needed for a one-off expression, thus, not worthy to be stored in memory. Lastly, since the input was passed by value and not by reference, its initial value will stay the same.

3

double square(double n){

return n*n;

}

What MUST be true immediately after the above code snippet has run?

The result will be a positive number.

The result will be a negative number.

The result will be stored in a new variable.

The value of the input parameter changes.

It is impossible to tell.

Explanation

Squaring a real number will always produce a positive number. The result does not have to be stored in a new variable; it could be a value that is only needed for a one-off expression, thus, not worthy to be stored in memory. Lastly, since the input was passed by value and not by reference, its initial value will stay the same.