Forums Register Login

Pag 213 Chapter 3 - question 4 - wrong answer - K&B OCA/OCP Java SE 7

+Pie Number of slices to send: Send
What is the response:


In the book response is : hi hi followed by runtime exception.
Wrong!!!
+Pie Number of slices to send: Send
Wrong how?  Have you run it?

I have and this is what I get:


The question is, do you know why?
+Pie Number of slices to send: Send
 

Julian West wrote:The question is, do you know why?


The response in the book is wrong. I completed the problem to errata of this book.
+Pie Number of slices to send: Send
 

Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.

I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.
+Pie Number of slices to send: Send
 

Ganesh Patekar wrote:

Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.

I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.



I run my code in Net Beans and I didn't see strings hi hi.
Text of exception was on the first line.
If I modified System.out.print to System.out.println I saw the string hi, hi.

Thank you.
+Pie Number of slices to send: Send
 

Ganesh Patekar wrote:

Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.

I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.



How can I modified my code to run withot exception?

1
+Pie Number of slices to send: Send
 

Anda Cristea wrote:How can I modified my code to run withot exception?

Did you understand why that program gives runtime exception?
1
+Pie Number of slices to send: Send
 

Anda Cristea wrote:

Ganesh Patekar wrote:

Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.

I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.



I run my code in Net Beans and I didn't see strings hi hi.
Text of exception was on the first line.
If I modified System.out.print to System.out.println I saw the string hi, hi.

Thank you.


That's a side effect of Netbeans that it isn't outputting the statements fast enough.

You don't need to know this for the exam, but System.out and System.err are different streams. They get written out independently and order isn't guaranteed. That's why you aren't seeing it. The book isn't wrong. The program is printing hi twice even if you don't see it.
1
+Pie Number of slices to send: Send
Each object of Chapter3_4 you create will have an instance field named m1 of type Chapter3_4 whose default value is null.

1. m2 refers to an object of Chapter3_4 who has m1 = null Or we can show m2.m1 = null ( Because while creating this object we passed nothing so no-argument constructor is invoked and m1 is not assigned any reference so It has default initial value null)

2. m3 refers to an object of Chapter3_4 who has m1 = m2 Or we can show m3.m1 = m2 ( Because while creating this object we passed m2 as parameter to parameterized constructor)

3. m3.go invokes go method on object of Chapter3_4  referred by m3 so you know this which prints hi

4. m4 = m3.m1 as we know m3.m1 ( Means m1 field of object referred by m3 ) refers to the object referred by m2 ( See step no 2 ) so now m4 and m2 refer to the same object of Chapter3_4

5. m4.go(); invokes go method on object of Chapter3_4  referred by m4 so you know this also, which prints hi

6. m5 = m2.m1; as we know m2.m1 = null (See step 1) so m5 is also assigned to null.

7. m5.go(); here we know m5 is null ( See step 6 ) and we are trying to invoke this instance method go(); on null which generates runtime exception NullPointerException

Now you know what you can do so NullPointerException will not occur.
+Pie Number of slices to send: Send
 

Ganesh Patekar wrote:Each object of Chapter3_4 you create will have an instance field named m1 of type Chapter3_4 whose default value is null.

1. m2 refers to an object of Chapter3_4 who has m1 = null Or we can show m2.m1 = null ( Because while creating this object we passed nothing so no-argument constructor is invoked and m1 is not assigned any reference so It has default initial value null)

2. m3 refers to an object of Chapter3_4 who has m1 = m2 Or we can show m3.m1 = m2 ( Because while creating this object we passed m2 as parameter to parameterized constructor)

3. m3.go invokes go method on object of Chapter3_4  referred by m3 so you know this which prints hi

4. m4 = m3.m1 as we know m3.m1 ( Means m1 field of object referred by m3 ) refers to the object referred by m2 ( See step no 2 ) so now m4 and m2 refer to the same object of Chapter3_4

5. m4.go(); invokes go method on object of Chapter3_4  referred by m4 so you know this also, which prints hi

6. m5 = m2.m1; as we know m2.m1 = null (See step 1) so m5 is also assigned to null.

7. m5.go(); here we know m5 is null ( See step 6 ) and we are trying to invoke this instance method go(); on null which generates runtime exception NullPointerException

Now you know what you can do so NullPointerException will not occur.



Perfect!!!
Correct explanation!!!
+Pie Number of slices to send: Send
You're welcome
+Pie Number of slices to send: Send
 

Jeanne Boyarsky wrote:

Anda Cristea wrote:

Ganesh Patekar wrote:

Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.

I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.



I run my code in Net Beans and I didn't see strings hi hi.
Text of exception was on the first line.
If I modified System.out.print to System.out.println I saw the string hi, hi.

Thank you.


That's a side effect of Netbeans that it isn't outputting the statements fast enough.

You don't need to know this for the exam, but System.out and System.err are different streams. They get written out independently and order isn't guaranteed. That's why you aren't seeing it. The book isn't wrong. The program is printing hi twice even if you don't see it.



In this situation how is it correct to use System.Err?

+Pie Number of slices to send: Send
 

Anda Cristea wrote:In this situation how is it correct to use System.Err?

This output stream is used to display error messages. Read more here ---> PrintStream err
+Pie Number of slices to send: Send
 

Anda Cristea wrote:In the book response is : hi hi followed by runtime exception.
Wrong!!!


Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.


No, that's not wrong! The study guide is spot-on. And therefore this is not an errata item.

In this topic you'll find an excellent explanation (with additional code snippets) about exactly the same code snippet. Definitely worth reading!

Hope it helps!
Kind regards,
Roel

PS. As mentioned in another of your topics as well, you should not change the class name(s) when you post such a code snippet. And the reason why is pretty simple: using the class name of a code snippet ranchers can easily search (using the excellent search function) for topics about the same code snippet they have doubts/questions about. If you change the class name to something different, they might miss out on valuable information.

+Pie Number of slices to send: Send
 

Anda Cristea wrote:I run my code in Net Beans and I didn't see strings hi hi.


That's another reason to quit using NetBeans (or any other IDE) during your preparation. In this topic you'll find a few other reasons about why you should not use an IDE during preparation.
I'm not sure if I approve of this interruption. But this tiny ad checks out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 617 times.
Similar Threads
understand me this code(m3.m1 and m2.m1)
Doubt in assigning Objects
StackOverflowError
Please Explain
Doubts in Assignments
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:22:28.