Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - General Questions (Q.No. 4)
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
1, 3, 4
2, 4, 5
1, 2, 6
2, 5, 6
Answer: Option
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[ ] .

Discussion:
5 comments Page 1 of 1.

Abhishek prakash said:   8 years ago
Option c having 6 statement and 6 statement is wrong. So please explain it with the correct answer.

Antonio said:   8 years ago
The option 6 is wrong because you need to initialize it if you use final.

Ahmet said:   1 decade ago
Option 6: The blank final field a may not have been initialized.

Devendra said:   1 decade ago
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.

Asdfgh said:   1 decade ago
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?

Post your comments here:

Your comments will be displayed after verification.