Java OCA OCP Practice Question 575

Question

How many of the following are legal declarations?

String lion [] = new String[] {"lion"}; 
String tiger [] = new String[1] {"tiger"}; 
String bear [] = new String[] {}; 
String my [] = new String[0] {}; 
  • A. None
  • B. One
  • C. Two
  • D. Three


C.

Note

When using an array initializer, you are not allowed to specify the size separately.

The size is inferred from the number of elements listed.

Therefore, tiger and my are incorrect.

When you're not using an array initializer, the size is required.

An empty array initializer is allowed.

Option C is correct because lion and bear are legal.




PreviousNext

Related