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

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

Moderator: admin

Post Reply
rdheepan
Posts: 3
Joined: Fri Sep 21, 2018 11:45 pm
Contact:

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

Post by rdheepan »

The question asks for the options that will correctly create and initializean array of Strings.
The evaluation results shows that the option 3 is a valid answer,

Code: Select all

String[] sA = new String[1] ; sA[0] = "aaa";
As I agree that this statement correctly creates a string array, but it never initialize that arrray. It creates an array first and then assigns value later. How it could be the valid answer then?

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

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

Post by admin »

1. The question doesn't mention that it has to be in a single statement. So, you have to consider the whole option (which means both the statements together) as one.
2. Even if you consider just the first statement i.e. String[] sA = new String[1]; sA is actually being declared as well as initialized. It is being initialized to new String[1] and its element sa[0] is initialized to null.

So both ways, it is a correct option.
If you like our products and services, please help us by posting your review here.

rdheepan
Posts: 3
Joined: Fri Sep 21, 2018 11:45 pm
Contact:

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

Post by rdheepan »

@The question doesn't mention that it has to be in a single statement... Initialization cannot happen in multiple statements. If we assign values at the same time when we declare is called initialization right? So even though the question does not explicitly mention that it has to be a single statement, the word initialization mandates it to be single statement.

@Even if you consider just the first statement ... It initialize the array with null string. but the question explicitly says, "initialize with non null elements".

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

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

Post by admin »

>@The question doesn't mention that it has to be in a single statement... Initialization cannot happen in multiple statements. If we assign values at the same time when we declare is called initialization right?

No! Where did you read that? Initialization can happen anywhere anytime. It just means assigning a value for the first time before the variable/reference is used.

>@Even if you consider just the first statement ... It initialize the array with null string. but the question explicitly says, "initialize with non null elements".
Yes, you are right here. The question does say non-null. But the option is still correct :)
If you like our products and services, please help us by posting your review here.

rdheepan
Posts: 3
Joined: Fri Sep 21, 2018 11:45 pm
Contact:

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

Post by rdheepan »

Okay. May by i am confused a bit. Will read through it again. Thanks for your help.

niiels93
Posts: 5
Joined: Thu Aug 06, 2020 3:41 pm
Contact:

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

Post by niiels93 »

Hello admin,



The question is: Which of the following statements will correctly create and initialize an array of Strings to non null elements?

I've always learned that an array has a fixed size and that you have specify the length with the declaration. When im looking at the answers i dont see the length specification in the declaration. Could you explain why the following answers dont have that and what the use is of the curly brackets?

Code: Select all

String[] sA = new String[] { "aaa"};

Code: Select all

String[] sA = {new String( "aaa")};
Thnx!

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

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

Post by admin »

niiels93 wrote:
Thu Aug 06, 2020 3:58 pm
I've always learned that an array has a fixed size and that you have specify the length with the declaration.
It looks like the source of your information is not good because the second part of your statement is incorrect. In fact, you can't specify the size in array declaration. You specify the size while instantiating the array. The size can be specified directly or indirectly.
When im looking at the answers i dont see the length specification in the declaration. Could you explain why the following answers dont have that and what the use is of the curly brackets?

Code: Select all

String[] sA = new String[] { "aaa"};

Code: Select all

String[] sA = {new String( "aaa")};
The part within curly braces is one of the ways of instantiating and initializing an array. Since the compiler can easily count the number of elements that are present in that array, it creates an array of that length. That is why, we don't specify the length explicitly in this style.

Arrays are an important topic for the exam and I suggest you to go through a good book before attempting mock exams.
I recommend Chapter 4 of OCA Java 8 Fundamentals (if you are studying for 1z0-808) or Chapter 9 of OCP Java 11 Part 1 Fundamentals (if you are studying for 1z0-815).

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

niiels93
Posts: 5
Joined: Thu Aug 06, 2020 3:41 pm
Contact:

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

Post by niiels93 »

thanks!

Post Reply

Who is online

Users browsing this forum: admin, Google [Bot] and 53 guests