About Question enthuware.ocajp.i.v7.2.974 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
georgeferreira
Posts: 2
Joined: Fri Jun 22, 2012 8:43 am
Contact:

About Question enthuware.ocajp.i.v7.2.974 :

Post by georgeferreira »

The code on the question says:

Code: Select all

import java.util .*;
public class TestClass{
   public static void main(String args[]){
      Stack s1 = new Stack ();
      Stack s2 = new Stack ();
      processStacks (s1,s2);
      System.out.println (s1 + "    "+ s2);
   }
   public static void processStacks(Stack x1, Stack x2){
      x1.push (new Integer ("100")); //assume that the method push adds the passed object to the stack.
      x2 = x1;
   }
 }
A space is set between "util" and ".". It will compile? I marked "None of above".

admin
Site Admin
Posts: 10114
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by admin »

Although space is not needed (and not recommended either) here, it will not cause any compilation issue. It will compile fine.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by dtchky »

Output after compilation:
Stack@424c2849 Stack@15e8f2a0
The code is correctly printing out the references to both stack objects.
Question Bank: 1/15

georgeferreira is correct, none of the above, but for a different reason, at least on my machine anyway :-)

Code: Select all

java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

Code: Select all

javac -version
javac 1.7.0_05

admin
Site Admin
Posts: 10114
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by admin »

It seems the toString() method of Stack implementation has changed. The question statement has now been changed to "What will be the contents of s1 and s2 at the time of the println statement in the main method of the following program?" to avoid this issue.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

javanaut

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by javanaut »

If x1 and s1 point to the same memory location and are modified when x1.push() is called why is s2 not modified when local variable x2, has same memory address as s2, is assigned to local variable x1, which has the same memory address as s1?

Is it because x2 would need to explicitly call a method such as push from the Stack class in order to modify the object that both x2 and s2 point to?

admin
Site Admin
Posts: 10114
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by admin »

javanaut wrote:If x1 and s1 point to the same memory location and are modified when x1.push() is called why is s2 not modified when local variable x2, has same memory address as s2, is assigned to local variable x1, which has the same memory address as s1?
I am not sure what you mean. x2 is not assigned to x1 in the given code. x1 is being assigned to x2 and that means, x2 starts pointing to the same object as x1, which is the same object as being pointed to be s1.

So why would the object pointed to by s2 change?

I suggest you to read this article thoroughly: http://www.javaranch.com/campfire/StoryPassBy.jsp

Also, try to think of variables and objects as separate. "Changing x1" and changing "the object pointed to by x1" are two different things. Further, variables don't have methods. The You seem to be mixing that up.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javanaut

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by javanaut »

Hello Paul,

Thank-you sir for the response. You are right, x1 is assigned to x2 in the program in question. I mixed these up when writing my reply.

I understand that object references are passed by copy to an extent but I do not understand why x1 can change s1 but x2 can not change s2. The only different is that x1 calls a method from Stack's API.

If x1 can point to s1 and change its value why does x2 pointing to x1 and s1 not change s2's value?

This is interesting to think about in your response, "...variables don't have methods".

I guess Ill read the JavaRanch article again. Thank-you for the help. This program does not seem logical to me and programming is all about logic.

Respectfully,

javanaut

ksnortum
Posts: 30
Joined: Fri Dec 07, 2012 6:09 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by ksnortum »

x1 isn't "changing" s1. x1 and s1 both point to the same object reference. When you execute "x2 = x1" you are not saying "s2 is now the same as s1", you are saying "x2 now points to the same object as x1." The local variable x2 goes out of scope when you exit prosessStacks() and s2 remains unchanged.

(BTW, I got this question wrong too and from what I've read, even seasoned Java programmers still get confused about this.)

guest

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by guest »

small typo error..
In the explanation .."You created two objects in main method: s1 ------------> [ EMPTY ] STACK 1 OBJECT s1 actually contains 15000 (say) s2 ------------> [ EMPTY ] STACK 2 OBJECT s1 actually contains 25000 (say)"

the last line shld be "s2 actually contains 25000 (say)."
Thanks.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by The_Nick »

dtchky wrote:Output after compilation:
Stack@424c2849 Stack@15e8f2a0
The code is correctly printing out the references to both stack objects.
Question Bank: 1/15

georgeferreira is correct, none of the above, but for a different reason, at least on my machine anyway :-)

Code: Select all

java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

Code: Select all

javac -version
javac 1.7.0_05

Hi,
Just to update things a bit. As of now, running on a java version "1.7.0_17", it prints out the actual content of the Stack objects, hence: [100] []

The_Nick

bptoth
Posts: 33
Joined: Mon May 06, 2013 9:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by bptoth »

guest wrote:small typo error..
In the explanation .."You created two objects in main method: s1 ------------> [ EMPTY ] STACK 1 OBJECT s1 actually contains 15000 (say) s2 ------------> [ EMPTY ] STACK 2 OBJECT s1 actually contains 25000 (say)"

the last line shld be "s2 actually contains 25000 (say)."
Thanks.
This one still seems to need correction

admin
Site Admin
Posts: 10114
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.974 :

Post by admin »

Fixed.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests