20

Which two code fragments correctly create and initialize a static array of int elements? (Choose two.)

A.

static final int[] a = { 100,200 };

B.

static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }

C.

static final int[] a = new int[2]{ 100,200 };

D.

static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }

Answer: A, B

here even D seems true, can anyone let me know why D is false.

2
  • 1
    This is not valid Scheme, Ruby, Python or LISP code. Please format your code and specify the language/framework you are using. Commented Dec 15, 2010 at 12:39
  • 2
    You might need to change the title so that it actually summarises the question .. Commented Dec 15, 2010 at 12:39

5 Answers 5

27

The correct answers are 1 and 2 (or A and B with your notation), and an also correct solution would be:

static final int[] a = new int[]{ 100,200 };

Solution D doesn't initalize the array automatically, as the class gets loaded by the runtime. It just defines a static method (init), which you have to call before using the array field.

Sign up to request clarification or add additional context in comments.

Comments

6

D defines a static method for initialising a but does not actually call it. Thus, a remains uninitialised unless someone explicitly calls the init method.

As other answers have pointed out: D shouldn't even compile because it attempts to assign a value to the final variable a. I guess that's a much more correct explanation. Nevertheless, even if a was not final D would still not work without extra code.

I assume the new int[3] in D is a typo? The other three all attempt to create an array of length 2.

1 Comment

ya u r really correct.I found the answer i Scjp .ur explanation is correct.dont mind about the votes
3

D (4) is false, because a) a is final and you cannot assign it in init; b) there is no guarantee that init will be called; c) init doesn't set the third element;

4 Comments

Why in the world was this down-voted? This answer is entirely correct.
I'm not the downvoter, but I think it is not entirely correct. c) is incorrect because new int[3] initializes all the elements to zero as far as I can remember. Correct me if I'm wrong.
@Sergey you aren't wrong, I mentioned it as aside comment, since I think (as mentioned somewhere) that new int[3]; should be int[2];. The main reason that makes D false is that a is final.
sergey c is incorrect.But not because of ur reason.The actual reason u can either inialise an array and dnt mention the dimension,or u can create a specific dimension for an array and inialise it later.But u can not initialise an array and simultaneously initialise it.Anyhow thanks for ur participation.The same was mentioned was by Ratna
1

for snippet C You cannot give dimensions ( Size ) while initializing for snippet D you should initialize final variable. It cannot be initialized later.

Comments

0

final variables should be initialized before constructor call completes. Since "static void init()" is a method & it will not run before constructor, final variables won't be initialized. Hence it is an compile time error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.