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

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 » Fri Sep 21, 2018 11:55 pm

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?

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

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

Post by admin » Sat Sep 22, 2018 12:45 am

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 » Sat Sep 22, 2018 1:37 am

@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".

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

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

Post by admin » Sat Sep 22, 2018 1:45 am

>@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 » Sat Sep 22, 2018 1:47 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests