Which one of the following is a valid statement?
A. char c = new char();
B. char c = new char;
C. char c = new char(4);
D. char c = new char;
Answer: Option B
Solution(By Examveda Team)
The syntax for declaring and creating an array variable in java is:
dataType arrayRefVar = new dataType[arraySize];
Thus, option (A) and option (C) is syntactically wrong as parentheses( ( ) ) is used instead of square brackets( [ ] ).
Option (D) is incorrect as the size of the array is missing.
B. Compilation error, arrays cannot be initialized to zero size.
C. Compilation error, it is a.length() not a.length
D. None of the above
A. The program has a compile error because the size of the array wasn't specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x is 0.
D. The program has a runtime error because the array element x is not defined.