KnowledgeBoat Logo
|

Computer Applications

Which of the following contain error ?

  1. int x[ ] = int[10];
  2. int[ ] y = new int[5];
  3. float d[ ] = {1, 2, 3};
  4. x = y = new int[10];
  5. int a[ ] = {1, 2}; int b[ ]; b = a;
  6. int i = new int(10);

Java Arrays

10 Likes

Answer

int x[ ] = int[10];
x = y = new int[10];
int i = new int(10);

Reason — int x[] = int[10]; doesn't contain the 'new' keyword used to initialize an array. The correct statement will be int x[] = new int[10];

In the statement x = y = new int[10];, 'x' and 'y' are not declared as array variables. The correct statement will be :

int x[] = new int[10]; 
int y[] = new int[10];
 

In the statement int i = new int(10);, 'i' is not declared as array variable and small brackets () are used instead of square brackets []. The correct statement will be int i[] = new int[10];

The remaining statements follow the syntax used for declaring and initializing an array, thus they do not have any error.

Answered By

5 Likes


Related Questions