February 27, 2015

Which two code Fragments correctly create and initialize a Static Array of int elements?

Given :

(Choose two.)

A. static final int[] a = { 100,200 };
B. Line 1:static final int[] a;
   Line 2:static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. Line 1:static final int[] a;
   Line 2:static void init() { a = new int[3]; a[0]=100; a[1]=200; }









Answer: A,B


Explanation :

In general we declare an Array in this form [Access Specifiers][optional modifiers] Datatype [] ref-var = {Initialization} or new Datatype[Size] for one Dimensional Array. 

For example :
public static String[] stringArray = new String[size];

OR

public static String[] stringArray = {"String1","String2","String3"};

Therefore Option A is correct with the Syntax.

Initialization blocks run when the class is first loaded (a static initialization block) or when an instance is created (an instance initialization block).

In Option B ,reference variable declaration and instance is created with initialization is divided 2 parts as shown in Line 1 and Line 2 in Option b .Since Static block can access Static data members and initialize the data values for integer array ,Option is Correct in Syntax.Option A,B.







Related Topics

0 comments:

Post a Comment