About Question enthuware.ocajp.i.v8.2.1407 :

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

Moderator: admin

Post Reply
ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

About Question enthuware.ocajp.i.v8.2.1407 :

Post by ElizabethCM »

Hi Paul,

This seems an interesting question and I just want to make sure I understand the concepts.

At the end of this line
 List list = new ArrayList();
What happens exactly?
Does it allocate space for the ArrayList since we have the "new" keyword? Or not yet?
Are the array elements initialized to null? I assume not as we don't know how many they are...
Please explain a little bit what happens here. I assume this is an empty array with a potential that each of its elements may point to what we will add inside.

I thought the code compiles as the elements are all null and then it doesn't matter in which order they're set.

What if it would have been List list = new ArrayList(3)?
Would the code work then?

Thanks

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

Re: About Question enthuware.ocajp.i.v8.2.1407 :

Post by admin »

I will try to answer it in brief but these are very basic questions. If you do not know what happens when List list = new ArrayList(); is executed, I sincerely suggest you to go through a good book to get your basics clear. You should not be attempting mock exams at this stage.

1. Whenever you use the new keyword, an object (of that class i.e. ArrayList, in this case) is created and space is allocated on the heap for it.
2. What happens inside the ArrayList object after you create it is explained in its JavaDoc description. You may read that. But your question is again pointing to your lack of basic knowledge about arrays. Whenever you create a new array of non-primitives, all its elements are automatically initialized to null.
3. Try the code with List list = new ArrayList(3) and see what happens.

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

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1407 :

Post by ElizabethCM »

Hi Paul,

I completely understand your answer and I apologize for asking before reading this part.
I learned Chapter 3 this morning and studied the String and String Builder parts. I got all the way to arrays and I said I could try some exercises at this point to check what I've learned so far at this chapter.
In the afternoon I continued learning and got to array and ArrayList. Very clear now why they can change size and how they're implemented and allocated in the memory.
I'll try the Objective tests after I'll finish all current chapter from now on.

Thanks

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1407 :

Post by shambhavi »

I would like to know
code 1 :

Code: Select all

List list = new ArrayList(4);   
      list.add("val1"); //1     
    list.add(2, "val2"); //2   
      list.add(1, "val3"); //3
in this case we have allotted 4 slots in the arraylist , each of reference type Object pointing to null right ? even then, why does it throw an exception at line 2 when we try to add a value at index 2?

if I had done : Object arr=new Object[4]; this would create an Object array of 4 Object references pointing to null. this wouldn't create a problem on arr[2] being assigned.
I don't understand what exactly is happening internally ?

the below code 2 prints null. Then whats wrong in the code 3 if there are null values?
code 2 :

Code: Select all

 List list = new ArrayList(4);  
 list.add("val1"); //1    
 list.add(1, "val2"); //2 
 list.add(2, "val3"); //3  
  list.add(1, null);
 System.out.println(list.get(1)); 
code 3 :

Code: Select all

List list = new ArrayList(4);  
 list.add("val1"); //1    
  System.out.println(list.get(1)); //exception

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

Re: About Question enthuware.ocajp.i.v8.2.1407 :

Post by admin »

shambhavi wrote:I would like to know
code 1 :

Code: Select all

List list = new ArrayList(4);   
      list.add("val1"); //1     
    list.add(2, "val2"); //2   
      list.add(1, "val3"); //3
in this case we have allotted 4 slots in the arraylist , each of reference type Object pointing to null right ?
why do you think so? Did you read what the javadoc says about this constructor?
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1407 :

Post by shambhavi »

i just did ! so sorry !

was misled by some other link found on google. :(

so it creates am empty one with size 0. and the indexes are based on the size an not the capacity right ?

Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests