• Post Reply Bookmark Topic Watch Topic
  • New Topic

ME-Q23 Mala Gupta mock exam explanation?  RSS feed

 
Mian Amjad
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Explanation: The code throws java.lang.StackOverflowError at runtime. Line 7
creates an instance of class Emp. Creation of an object of the class Emp requires the creation
of an instance variable mgr and its initialization with an object of the same class.
As you see, the Emp object creation calls itself recursively, resulting in java.lang
.StackOverflowError.

unfortunately, I did not get the explanation. Any help? thanks.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mian Amjad wrote:
unfortunately, I did not get the explanation. Any help? thanks.


When the main() method of the Office class is called, it will instantiate an Emp instance for the "e" local variable.  During this instantiation, it will need to initialize the "mgr" instance variable... which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it. And during this instantiation, for the instance variable, it will also need to initialize the "mgr" instance variable for it, which of course, needs to instantiate an Emp instance variable for it.... stack overflow (or out of memory) exception...

Henry
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic has an excellent explanation about exactly the same question and will probably clear your doubts.
 
Mian Amjad
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"which of course, needs to instantiate an Emp instance variable for it.... stack overflow (or out of memory) exception... 

Henry"

Sorry Henry, I did not phrased my question well. I meant to ask why recursion happened in the program?  Thanks.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mian Amjad wrote:I meant to ask why recursion happened in the program?

If you go through the topic I mentioned in my previous post, you can discover yourself why recursion happened in the program...
 
Mian Amjad
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mian Amjad wrote:I meant to ask why recursion happened in the program?

If you go through the topic I mentioned in my previous post, you can discover yourself why recursion happened in the program...


Yes. I went through the topic and followed the logic. Question remains,  why the constructor, in this case, causing the recursion?  What rule has been violated?  Since constructors always instantiate all the objects. Thanks Roel.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mian Amjad wrote:Yes. I went through the topic and followed the logic. Question remains,  why the constructor, in this case, causing the recursion?  What rule has been violated?

It will be difficult to provide another (better) explanation than the one in the aforementioned topic, but I'll give it a shot.

Let's start with a very simple version of the Emp classIf you create an Emp instance (using Emp emp = new Emp();), here's what happens:
1/ the default constructor of Emp is invoked
2/ the default constructor of Emp finishes => a new Emp instance has been successfully created
3/ the newly created Emp instance is assigned to the variable emp

Now let's add a manager to the Emp classThis code snippet is (functionally) equivalent with this oneIf you create an Emp instance (using new Emp();), here's what happens:
1/ the default constructor of Emp is invoked
2/ the default constructor of Manager is invoked
3/ the default constructor of Manager finishes => a new Manager instance has been successfully created
4/ the newly created Manager instance is assigned to the instance variable mgr
5/ the default constructor of Emp finishes => a new Emp instance has been successfully created
6/ the newly created Emp instance is assigned to the variable emp

If you understand these code snippets, you are ready for the next (and final) step: the original code snippetAs you know from the previous code snippet, this is (functionally) equivalent withIf you create an Emp instance (using new Emp();), here's what happens:
1/ the default constructor of Emp is invoked (to create the Emp instance to assign to variable emp)
2/ the default constructor of Emp is invoked again (to create the Emp instance to assign to variable emp.mgr)
3/ the default constructor of Emp is invoked once more (to create the Emp instance to assign to variable emp.mgr.mgr)
4/ the default constructor of Emp is invoked another time (to create the Emp instance to assign to variable emp.mgr.mgr.mgr)
5/ the default constructor of Emp is invoked for the 5th time (to create the Emp instance to assign to variable emp.mgr.mgr.mgr.mgr)
...

Are you starting to see the pattern here? To successfully create an Emp instance, you have to create another Emp instance (to assign to the instance variable mgr). But to successfully create that Emp instance, you have to create yet another Emp instance because that Emp instance also defines an instance variable mgr which needs to be initialized with an Emp instance. And so on...

So because the default Emp constructor keeps being invoked, at some given moment in time you'll get a StackOverflowError.

Hope it helps!
Kind regards,
Roel
 
Mian Amjad
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:

Hope it helps!
Kind regards,
Roel


Yes. It was very helpful.  Thank you to both you and Henry,  for your time and effort; )

Mian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!