Page 1 of 2

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

Posted: Sat Jun 09, 2012 12:17 pm
by Jix
Hi, won't option saveObject(null); cause NullPointerException? If not then why sometimes passing null to a method cause NullPointerException?

Thanks

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

Posted: Sat Jun 09, 2012 12:38 pm
by admin
Passing null in itself never causes NPE. Accessing null always causes NPE. For example:

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.
	}

}
Look at the stack trace:

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)  
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.

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

Posted: Sat Jun 09, 2012 8:31 pm
by Jix
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 :

Posted: Thu Aug 16, 2012 11:41 am
by AnotherGuest
Option 3;

Should the variable name (of type List) be/start with a numeric value? Shouldn't it just be _, $, and letters?

Cheers

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

Posted: Thu Aug 16, 2012 8:35 pm
by admin
It is lower case L not 1.

HTH,
Paul.

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

Posted: Mon Aug 20, 2012 9:43 am
by AnotherGuest
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 :)

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

Posted: Thu Oct 04, 2012 12:55 pm
by Guest
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?

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

Posted: Thu Oct 04, 2012 2:12 pm
by admin
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?
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.:

Code: Select all

Collection c = new ArrayList();
saveObject( (List) c );
The compiler doesn't really know even after the cast, but it accepts the programmer's word for it :)

HTH,
Paul.

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

Posted: Thu Oct 04, 2012 3:22 pm
by Guest
Ok thanks, got it.

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

Posted: Fri Nov 15, 2013 4:56 am
by Ghost23
how can "saveObject( new ArrayList() );" work? shouldnt we define a name for the ArrayList?

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

Posted: Fri Nov 15, 2013 6:28 am
by admin
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.

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

Posted: Thu Aug 14, 2014 8:58 am
by vchhang
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?

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

Posted: Thu Aug 14, 2014 9:18 am
by admin
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.

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

Posted: Thu Aug 14, 2014 9:21 am
by vchhang
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.

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

Posted: Thu Aug 14, 2014 9:42 am
by admin
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.

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

Posted: Mon Sep 15, 2014 8:51 am
by Smoljanov Sergej
saveObject() cannot accept c because c is declared of type Collection, which is a superclass of List, but the saveObject() method expects a List.
Collection is interface.
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?

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

Posted: Mon Sep 15, 2014 11:08 am
by admin
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.

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

Posted: Sat Jun 10, 2017 12:34 am
by carlosrgc
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

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

Posted: Sat Jun 10, 2017 1:09 am
by admin
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.

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

Posted: Sat Jun 10, 2017 6:57 am
by carlosrgc
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...

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

Posted: Sat Jun 10, 2017 11:20 pm
by admin
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.

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

Posted: Fri Feb 01, 2019 1:31 pm
by flex567
If sub interface existed we could pass it into the function?

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

Posted: Sat Feb 02, 2019 10:00 am
by admin
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.

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

Posted: Sat Feb 02, 2019 11:47 am
by flex567
aha ok maybe the question was not phrased correctly.

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

Posted: Fri Jul 24, 2020 5:51 am
by Sieusc
admin wrote:
Thu Aug 16, 2012 8:35 pm
It is lower case L not 1.

HTH,
Paul.
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