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?
- public int a [ ]
- static int [ ] a
- public [ ] int a
- private int a [3]
- private int [3] a [ ]
- public final int [ ] a
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.
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers