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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

Post by Jix »

Hi, won't option saveObject(null); cause NullPointerException? If not then why sometimes passing null to a method cause NullPointerException?

Thanks

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

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

AnotherGuest

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

Post 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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It is lower case L not 1.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

AnotherGuest

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

Post 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 :)

Guest

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

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

Ok thanks, got it.

Ghost23
Posts: 4
Joined: Thu Nov 14, 2013 6:35 am
Contact:

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

Post by Ghost23 »

how can "saveObject( new ArrayList() );" work? shouldnt we define a name for the ArrayList?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Smoljanov Sergej
Posts: 4
Joined: Mon Sep 15, 2014 8:24 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

carlosrgc
Posts: 3
Joined: Mon Feb 20, 2017 9:37 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

carlosrgc
Posts: 3
Joined: Mon Feb 20, 2017 9:37 am
Contact:

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

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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

If sub interface existed we could pass it into the function?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

aha ok maybe the question was not phrased correctly.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests