Which two code fragments correctly create and initialize a static array of int elements?

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

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; }



Leave a Reply 2

Your email address will not be published. Required fields are marked *


madhu

madhu

ans:
A AND B

BECZ STATIC ARRAY SHOULD ME INITIALIZE UNDER STATIC BLOCK

C: NOT INITIALIZE UNDER STATIC BLOCK

D:IN THIS OPTION NO GUARENTEE TO CALL INIT() METHOD IT SHOULD ME INITIALIZED.

Matti

Matti

A and B, good question!
C is syntax error for an assignment!
D is not allowed to initialize a constant in a method!