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?

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44

posted

0

Here's another one, I have no idea what am I suppose to do, appreciate if someone can explain to me

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;

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 × m times, but only if both numbers are positive; if either number is negative, you get an output of 0.

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44

posted

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 × m times, 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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 46352

51

posted

0

I think I agree with you about those questions. There are good reasons for using pencil and paper (usually accompanied by a large eraser ) in programming, for example when you are designing pseudo-code to implement algorithms, or similar. Those questions do test your understanding of a program, but I think having to create an application would be a far better test of your abilities. Round here, the undergraduates usually get an application to create and two months to create it in. During that time, they are allowed to ask for help, within certain limits.

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44

posted

0

Harsha Smith wrote:second problem, say the value of x = 1 and y =1

now y = y+x // y =2
again y = y+x// 2 +1

System.out.println(y)//prints 3

similary y = y+2*x //1+2*1 = 3

System.out.println(y)//prints 3

Got it?

Hi, it seems that y = x + 2*y yield the same answer?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 46352

51

posted

0

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 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.

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287

posted

0

put x = 3 and y = 2 and check what happens? y= y+x; y= y+x & y = y+ 2 *x consistently produce the same results for different values of x and y;

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 46352

51

posted

0

You are now arguing from the general to the particular, so that argument is valid.

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44

posted

0

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 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.