About Question enthuware.ocajp.i.v7.2.942 :
Moderator: admin
-
- Posts: 14
- Joined: Thu May 31, 2012 7:57 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.942 :
Hi, won't option saveObject(null); cause NullPointerException? If not then why sometimes passing null to a method cause NullPointerException?
Thanks
Thanks
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Passing null in itself never causes NPE. Accessing null always causes NPE. For example:
Look at the stack trace:
You are asking some very basic questions, so it my sincere suggestion that instead of attempting mock exams, you should go through a book and write some test programs. Just attempting mock questions will not help much.
HTH,
Paul.
Code: Select all
public class TestClass{
public static void m1(String s){
int x = s.length(); //NPE is actually thrown at this point (because s is null), which propagates up the call chain and so it looks as if m1(null); threw NPE.
}
public static void main(String[] args){
m1(null); //NPE is not generated here, it comes from inside the m1 method code.
}
}
Code: Select all
C:\temp>java TestClass
Exception in thread "main" java.lang.NullPointerException
at TestClass.m1(TestClass.java:4) <-- This is where the NPE is thrown
at TestClass.main(TestClass.java:8)
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu May 31, 2012 7:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Thanks, I actually learned Java from Java tutorials made by Oracle. I found out that their tutorial is too shallow while doing your mock exams. But I've learned a lot while studying your mock exams, I started to pass mock exams recently. Don't worry I'm not gonna do OCAJP till I'm ready.
Re: About Question enthuware.ocajp.i.v7.2.942 :
Option 3;
Should the variable name (of type List) be/start with a numeric value? Shouldn't it just be _, $, and letters?
Cheers
Should the variable name (of type List) be/start with a numeric value? Shouldn't it just be _, $, and letters?
Cheers
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
It is lower case L not 1.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.942 :
Ah, rookie mistake cost me a mark there...I shall be sure to add 'recognition of correct characters' to my study list:p
Thanks for the response
Thanks for the response
Re: About Question enthuware.ocajp.i.v7.2.942 :
Although in 2nd option c is of type Collection but it is pointing to an arraylist. So why can't this option be a correct answer?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
While passing a variable to a method, it is the declared type of the variable that the compiler checks against the required type of the parameter. In this case, a Collection variable cannot be assigned to List variable. The compiler cannot make sure that at the time of making the call, c will still point to an ArrayList and so it doesn't compile. You have to cast it to List i.e.:Guest wrote:Although in 2nd option c is of type Collection but it is pointing to an arraylist. So why can't this option be a correct answer?
Code: Select all
Collection c = new ArrayList();
saveObject( (List) c );
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Thu Nov 14, 2013 6:35 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
how can "saveObject( new ArrayList() );" work? shouldnt we define a name for the ArrayList?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
No, name is required only if you want to refer to the same ArrayList object later. Here, the JVM passes the reference value directly to the method parameter. In the saveObject method, the object is referred to by the method parameter.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
It is questions like these that novice like me get it wrong because I didn't know Collection is the super class of List. What are the basic class hierarchy that people like me who have not programmed Java in a very long time should know?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
You should know about the hierarchies of the classes that are included in this exam. This means - ArrayList, String, StringBuilder, RuntimeException, ArrayIndexOutOfBoundsException, NullPointerException, ClassCastException, IllegalArgumentException, and Error.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Thank you again Paul. You have a great product and the customer service is unmatched. Let us know what we as customers can do to help improve it. I will try and give suggestion if that helps.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Glad to know that you are happy with our service.
Your feedback of any kind, before and after you take the real exam, is most helpful. Letting us know what you need to make the product match the style and content of the real exam is the best help we can get
Paul.
Your feedback of any kind, before and after you take the real exam, is most helpful. Letting us know what you need to make the product match the style and content of the real exam is the best help we can get
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Sep 15, 2014 8:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Collection is interface.saveObject() cannot accept c because c is declared of type Collection, which is a superclass of List, but the saveObject() method expects a List.
and List is interface
i think more carefully will be "saveObject() cannot accept c because c is declared of type Collection, which is a superinterface of List, but the saveObject() method expects a List.", or i have mistaken?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
You are right. superinterface would be more precise. Although when talking conceptually, it is common to use the word class for class as well as interface.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon Feb 20, 2017 9:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Hi, I agree with the question and the answers but if I change the question related with the code. for example
Consider the following code to count objects and save the most recent object .... Does it fullfill its purpose?
I guess no, because if I pass a null value in fact no new object is created but the counter is incremented and if a real object was created previously the reference to it (prevObject) is lost
I was not sure if I should open a new topic
Regards
Consider the following code to count objects and save the most recent object .... Does it fullfill its purpose?
I guess no, because if I pass a null value in fact no new object is created but the counter is incremented and if a real object was created previously the reference to it (prevObject) is lost
I was not sure if I should open a new topic
Regards
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Code given in test questions is not always necessarily 100% robust or fool proof. There is no detailed specification that tells how exactly the code must function in all situations. So who is to say it does not fulfill the purpose? May be this is how the developer intended it to work. These are not production ready code snippets. Their whole purpose is to illustrate a narrow concept or point.
This is how you should analyze the code in real exam as well. You should just focus on the problem in the question.
HTH,
Paul.
This is how you should analyze the code in real exam as well. You should just focus on the problem in the question.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon Feb 20, 2017 9:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
Thanks , Paul I have not doubt neither the question or answer. I was just thinking "alound" weather this prototype of code could accomplish what it states in real life.
I am new to programming and some silly thoughts may come to my mind
By the way, excelent product and support. More than half of the content of my flashcards comes from the answer of this forum.
I will take the exam this Tuesday, wish me luck...
I am new to programming and some silly thoughts may come to my mind
By the way, excelent product and support. More than half of the content of my flashcards comes from the answer of this forum.
I will take the exam this Tuesday, wish me luck...
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
No problem. I mentioned this point because you will find such questions in the exam as well. We don't want you to read too much into the question and lose marks
Glad to know you liked our product.
Wish you all the best!
Paul.
Glad to know you liked our product.
Wish you all the best!
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
If sub interface existed we could pass it into the function?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
SubInterface of what? and how do you plan to pass it?
Please put it in code so that your question is a bit more clear to answer.
Please put it in code so that your question is a bit more clear to answer.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
aha ok maybe the question was not phrased correctly.
-
- Posts: 21
- Joined: Mon Mar 02, 2020 3:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.942 :
I too thought it was a 1 and went for another option, would be better to change the variable name as this creates a whole 'nother sort of criteria on which the student is tested lol.
I went for the option above (number 2), I knew in the back of my mind that Collection is higher in the hierarchy than List but seeing as option 3 and 5 would definately be false I went for option 2
Who is online
Users browsing this forum: No registered users and 47 guests