1. Which two code fragments correctly create and initialize a Static Array of int element?(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;}
Correct Answer. (A,B)
Explanation.
Syntax के अनुसार अगर देखे तोह option A सही है,क्योकि हम जानते है की Array का syntax क्या होता है|
[Access modifiers] Datatype [] ref _var = {Initialization} or
[Access modifiers] Datatype [] ref_var =new Datatype[Size] इसलिए option A सही तरीके से create and initialize किया हुआ है |
Option B के फर्स्ट लाइन में correctly integer array create किया हुआ है, और दूसरे लाइन में static block के अंदर integer array के static data members को initialize किया हुआ है जो की सही है | जैसा की हम जानते है की static block हमेशा JVM(java virtual machine) के द्वारा class loading के समय recognize and initialize होती है | तो ये option भी करेक्ट है |
Option c में curly bracket के अंदर dimension expression define नहीं है , इसलिए ये option wrong है |
Option d में कोई gurranty नहीं है की a variable initialize हो सकता है | और हम init method के अंदर final variable को initialize नहीं कर सकते है |
आपलोगो अगर ये पोस्ट पसंद आया तो , please comment और share करे |
Thanks !!!!