Java Programming - Declarations and Access Control - Discussion

4. 

Which three form part of correct array declarations?

  1. public int a [ ]
  2. static int [ ] a
  3. public [ ] int a
  4. private int a [3]
  5. private int [3] a [ ]
  6. public final int [ ] a

[A]. 1, 3, 4
[B]. 2, 4, 5
[C]. 1, 2, 6
[D]. 2, 5, 6

Answer: Option C

Explanation:

(1), (2) and (6) are valid array declarations.

Option (3) is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a

Option (4) is not a correct array declaration. The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a []

Option (5) is not a correct array declaration. The compiler complains with 2 errors:

']' expected. A closing bracket is expected in place of the 3 and

<identifier> expected A variable name is expected after a[ ] .


Asdfgh said: (May 15, 2013)  
In the option 5, if the error, "<identifier> expected A variable name is expected after a[ ]" occurs, they why not the same error arise for option 1 also?

Devendra said: (Jun 29, 2013)  
Please first mentioned where you want to declare this array either inside main() or at class level.

Because,

"static int [ ] a","public int a [ ]","public final int [ ] a" declaration will fail inside main().

Because there's no need of access specifier for local declaration. This is confusing language.

Ahmet said: (Mar 21, 2015)  
Option 6: The blank final field a may not have been initialized.

Antonio said: (Mar 24, 2017)  
The option 6 is wrong because you need to initialize it if you use final.

Abhishek Prakash said: (Feb 10, 2018)  
Option c having 6 statement and 6 statement is wrong. So please explain it with the correct answer.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.