Question:
Please Check My Work..?
javachick1
2008-07-13 13:46:37 UTC
Please double check my work. I am unsure about a few of them....

The question asks: Assume that we declare int x=1,y=2; each time before Java executes each of the following statements; fill in the states for these variables after each statement executes (or write NLES for not legal expression statement).

My answers are below (the comments) .....

A) y == x; // not a legal statement

B) x = y += 2; // x is now 4 and y is 2 (doesn't change)

C) y = (2*x)++; // y is now 3, and x is still 1

D)x = x++; // x is now 2, and y does not change

E) y = 1 + (x = ++ y); // illegal expression
Five answers:
sspade30
2008-07-13 17:47:46 UTC
Wow, no, previous poster was very wrong, and so are you. Every single time. Is this a joke?



A) that is a legal statement. It tests the equality, but takes no action. Optimizer might eliminate this since it has no side effects, but it is valid and result will be x=1, y=2



B) Look into the += operator. The += operator has the same precedence as =, so it executes right to left. the += takes place first, increasing the value of y to 4. Then the = is executed, so x is also 4. x=4, y=4



C) This is an illegal statement. (2*x) is not a variable that can be incremented. You will get a compiler error.



D) x = x++; // ugh. Actually, x will be unchanged. First, the value of x is evaluated, then it is incremented. The result of (x++) is 1, although x is then incremented to 2, but THEN the original evaluation of 1 is assigned to x, so x becomes 1 again. x=1, y=2



E) No, that is a legal statement. First, y is incremented (y=3) then assigned to x (x=3). Then the result of that assignment is added to 1 and assigned to y (y=4). x=3, y=4



EDIT: Ok, I give on A. You were right. In C++, that would be legal, but Java is more finicky so it doesn't allow it. Go figure.
Sayvai
2008-07-13 14:49:14 UTC
From the answers above they look look spot on don't they. Mind you I took a module in Java not so long ago (well 2 years ago), and by going through the "sly" questions without reading your presumed answers in your java comments (//), i got the same answers as you! But I had a bad feeling about the incremental operators (++), so I looked it up (view link from source below!) and found there is are likely possible mistakes.



My corrections to the following are:

a) // NLES (I believe you are correct as == is an equality not an assignment operater)

b) // x=2 y=4

c) // x=1 y=2

d) // x=2 y=2 (I believe you are correct as x will eventually get to 2 anyway)

e) // x=3 y=7



Read the link below, and it should make more sense like it just did to me. I'm not going to say my answers are 100% correct but it makes more sense after doing some reading!



Good luck.



PS: Please choose my answer as "best answer" if you agree with me in the end, as it will help others too! (when they search for similar questions/answers)
2017-01-04 22:32:27 UTC
i haven't checked it for some week now. My on the spot at house is all knotted up. provider would be out to my domicile on Thursday to repair it. i'm on Y!A at artwork ideal now. My employer blocks the means to envision own email and myspace
IT guru
2008-07-13 13:55:57 UTC
Oracle tutorials-

http://oracletutorials.info/
2008-07-13 13:55:46 UTC
no thx =P


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...