Page 1 of 1

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

Posted: Tue Jan 13, 2015 12:18 pm
by Alina_Lapina
How come that 4 objects are created, when the execution stops after the first MyException it thrown? I thowhgt it was only 1 object created, because the first object named 'a' of Noobs type was not finished its creating process because of the exception.

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

Posted: Tue Jan 13, 2015 8:23 pm
by admin
Execution does not stop after the first MyException is thrown. Why do you think so?
-Paul.

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

Posted: Wed Jan 14, 2015 4:17 am
by Alina_Lapina
I was wrong. Executions of all code examples in my textbook was stopped after en exception was thrown. I mean every time it was the last thing that happened. Now I understand. Thank you!

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

Posted: Fri Apr 10, 2015 6:07 am
by pillphil
Why is String[] args not counted as an object?

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

Posted: Fri Apr 10, 2015 6:52 am
by admin
You are right, you may count the String[] object referred to by args as well (although it is not created by the given code). Explanation has been updated to include this as well.

This is why the explanation notes that this question is ambiguous as it does not state precisely what objects should be included in the count.

HTH,
Paul.

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

Posted: Sat Jun 27, 2015 6:35 am
by jatin2086
1. new MyException();
2.Noobs a = new Noobs();        3. Noobs b = new Noobs();

3 objects are created ? where does the fourth come from ?

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

Posted: Sat Jun 27, 2015 9:38 am
by admin
Did you read the explanation? The first line explains why 4 objects are created. Two MyException objects are created.

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

Posted: Fri Jul 17, 2015 6:39 pm
by Vermeulen
Like pillphill I counted the args array as well and answered 5. Indeed I was also wondering about Class objects, but forgot about Thread :).

Oh well, if such a question occurs on the exam I know I should focus on the objects created by the code itself!

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

Posted: Thu Aug 13, 2015 4:31 pm
by NickWoodward
admin wrote:You are right, you may count the String[] object referred to by args as well (although it is not created by the given code). Explanation has been updated to include this as well.

This is why the explanation notes that this question is ambiguous as it does not state precisely what objects should be included in the count.

HTH,
Paul.
yeah, i included the args array too. do you think we should if this ever came up in the exam?

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

Posted: Thu Aug 13, 2015 8:27 pm
by admin
No, we do not think that the array object should be included here.

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

Posted: Sun May 15, 2016 12:19 pm
by jihedamine
The solution states "If you consider the String[] object referred to by args, the answer would then be 8."
Shouldn't it be 7 (6 +1) and not (6+2) as public static void main(String[] args) is created only once for the Noobs class?

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

Posted: Sun May 15, 2016 8:36 pm
by admin
One is the empty String array object and one is the class object for String[]. But again, you should ignore such objects because we do not believe that is the intention of the question.

HTH,
Paul.

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

Posted: Sun Jun 19, 2016 3:36 am
by wuqing1450
My answer is 6: 2 Noobs; 2 MyException; 2 Exception objects?

My understanding of this case is that a default constructor will be written by JVM as below, since MyException doesn't have a constructor:

Code: Select all

class MyException extends Exception {
        MyException() {
                 super();
        }
}
When new MyException is invoked, are there 2 objects created: MyException and Exception?

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

Posted: Sun Jun 19, 2016 10:23 am
by admin
wuqing1450 wrote:My answer is 6: 2 Noobs; 2 MyException; 2 Exception objects?

My understanding of this case is that a default constructor will be written by JVM as below, since MyException doesn't have a constructor:

Code: Select all

class MyException extends Exception {
        MyException() {
                 super();
        }
}
When new MyException is invoked, are there 2 objects created: MyException and Exception?
That is not correct. Think of it like this. Let us says Apple extends Fruit. When you create an Apple object, do you create two objects? No. Apple is-a Fruit but that doesn't mean there are two independent objects created. The same Apple object is counted as an Apple object as well as Fruit object.

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

Posted: Thu Jun 23, 2016 9:26 am
by wuqing1450
Thank you for your help. :)

Now I understand.

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

Posted: Sun Jun 26, 2016 1:20 pm
by Sai Divya sree
Hi,
i understood how four objects are created.i.e one noobs and its Myexception and again one noobs and its Myexception are created.But in the explanation it says two class objects(one for noobs and one for Myexception are created.that is totally (4+2) 6 are created.I couldn't understand how those other two objects come from?

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

Posted: Sun Jun 26, 2016 1:55 pm
by admin
For every class that is loaded by the JVM, an object of a class named Class is created. You can read about it here: https://docs.oracle.com/javase/7/docs/a ... Class.html

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

Posted: Wed Jun 29, 2016 12:53 pm
by Sai Divya sree
Thanks a lot.I understood it clearly and the link was very helpful.

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

Posted: Tue May 15, 2018 6:48 am
by naziat
Hello admin
Why do you think String[] args shouldn't be considered?

secondly
What does this line in explanation of the answer means?
The question is ambiguous because two Class objects (one for Noobs and one for MyException) are also created. If you consider those, then the answer would be 6.
My question is, how can it be 6? What I have observed is Whenever Noobs object is created, it creates the MyException object as well, since there are 2 Noobs objects created explicitly, so there should be 4 objects created. But explanation says 6(if we consider Noobs+MyException), please advise.

thanks
Nazia

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

Posted: Tue May 15, 2018 10:18 am
by admin
1. Yes, the String array object pointed to by args can also be considered. That is why the explanation states that it is an ambiguous question because the problem statement does not clearly mention whether it is talking only about the objects created by the code in the main method or other objects as well. It also says that in our opinion only the objects directly created by the given code should be considered to arrive at the answer. From this perspective, the answer should be 4.

2. Answer will be 6 if you consider the two Class objects also. Please read my reply above. If you consider the String[] object, then the answer will be 7.

HTH,
Paul.

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

Posted: Tue May 15, 2018 10:30 am
by naziat
Again my question remains unanswered.

According to my understanding, there will be 4 objects (excluding String[] args)
1) One object created for first Noobs object
2) Second object created of MyException by above first Noobs object
3) Third object created for second Noobs object
4) Fourth object created of MyException by above second Noobs object

But what makes you say that its 6 objects actually?

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

Posted: Tue May 15, 2018 10:38 am
by admin
That is what the first line of the explanation also says, "When a Noobs object is created, a MyException object is also created. Therefore a total of 4 objects are created. "

Regarding 6 objects, did you read my reply above (to Sai Diyva Sri)? It contains a link. You need to read that article to understand that 2 class objects are also created. That makes it 4+2 = 6 objects. But that is not required for the OCAJP exam.

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

Posted: Thu Jan 02, 2020 5:49 am
by patricklm
Debugging this question on IntelliJ shows the following objects being created in the following order:
1) Noobs@792
2) MyException@7954
3) String[0]@795
4) Noobs@796
5) MyException@797

Does this question exclude the String[], the main method argument?

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

Posted: Fri Jan 03, 2020 12:21 am
by admin
Please check the explanation shown for this question. It explains the point that you have raised:
Note: Some candidates have reported getting a similar question.
The question is ambiguous because two Class objects (one for Noobs and one for MyException) are also created. If you consider those, then the answer would be 6. A String array object pointed to by args is also created and if you consider that, the answer would by 7. Further, several Thread objects are also created (although not directly by this code.) Since this is out of scope for the exam, it is best to ignore these kind of objects and consider only the objects created directly by the code.