Assume that m and n are declared as valid int variables. What will be the output of the program segment below?

A. m +n

B. m * n (correct answer)

C. m

D. n

Hi guys, I can't figure out how can I work this problem, I get very confused when questions like this comes without any brace. Is there a strategy on working on these type of questions?

Assuming x and y are int variables, the statements

y = y + x;

y = y + x;

can be replaced with the assignment statement

A) y = 2*x + 2*y;

B) y = (x + y)/2;

C) y = y + 2*x; (correct answer)

D) y = x + 2*y;

`n × m`times, but only if both numbers are positive; if either number is negative, you get an output of 0.

Campbell Ritchie wrote:Where on earth did you find those questions? They look designed to confuse. You would have to go through the execution of the code with a pencil and paper, and you can write down the values after each operation. With the two nested loops, you can tell that the loops are executed

n × mtimes, but only if both numbers are positive; if either number is negative, you get an output of 0.

these are past year exams questions from my uni, i freak out when I have to work on these kind of questions under exam condition, trying to practise as much as possible. I'm actually quite unhappy that during this age and time, i have to work on programming exams using pencil and paper. I wish we are instead required to complete a small programming project within 2 - 3 hours with an IDE instead.

Let’s work out its weakest-precondition transformer effect, to establish

*z*= y, which means the value of y at the end of the program.

[y = y + x; y = y + x]

*z*= y ≡ by the Sequence Rule

[y = y + x][y = y + x]

*z*= y ≡ by the assignment rule on the right

[y = y + x]

*z*= y 〈y \ x + y〉 ≡ by substitution

[y = y + x]

*z*= x + y ≡ by the assignment rule on the left

*z*= x + y 〈y \ x + y〉 ≡ by substitution

*z*= x + y + y ≡ by algebra

*z*= x + 2y QED

You can now substitute 1 for x and 1 for y and get 3.

Campbell Ritchie wrote:You are arguing from the particular to the general; that is not valid. You need to go from the general to the particular.

Let’s work out its weakest-precondition transformer effect, to establishz= y, which means the value of y at the end of the program.

[y = y + x; y = y + x]z= y ≡ by the Sequence Rule

[y = y + x][y = y + x]z= y ≡ by the assignment rule on the right

[y = y + x]z= y 〈y \ x + y〉 ≡ by substitution

[y = y + x]z= x + y ≡ by the assignment rule on the left

z= x + y 〈y \ x + y〉 ≡ by substitution

z= x + y + y ≡ by algebra

z= x + 2y QED

You can now substitute 1 for x and 1 for y and get 3.

I think I have solved it.

y = y + x;

y = y + x; <-assign new value

y = (y + x) + x

y = y + 2x;

Yayyyy!