Given the code fragment:
What is the result?
A. 1 2 3 4followed by an ArrayIndexOutOfBoundsException
B. 1 2 3
C. 1 2 3 4
D. Compilation fails.
Given the code fragment:
What is the result?
A. 1 2 3 4followed by an ArrayIndexOutOfBoundsException
B. 1 2 3
C. 1 2 3 4
D. Compilation fails.
B
Option is B if curly brace is there means
1
2
3
instead of int[] arr=(1,2,3,4);
it can be int [] arr={1,2,3,4};
then the answer should be option B
D:
# javac Test11.java
Test11.java:3: error: ‘)’ expected
int[] arr = (1, 2, 3, 4);
^
Test11.java:3: error: expected
int[] arr = (1, 2, 3, 4);
^
Test11.java:3: error: illegal start of expression
int[] arr = (1, 2, 3, 4);
^
Test11.java:3: error: ‘;’ expected
int[] arr = (1, 2, 3, 4);
^
Test11.java:3: error: illegal start of expression
int[] arr = (1, 2, 3, 4);
^
Test11.java:3: error: ‘;’ expected
int[] arr = (1, 2, 3, 4);
^
Test11.java:3: error: illegal start of expression
int[] arr = (1, 2, 3, 4);
————————————————————-
its needed to use curly braces {} for array initialization then it will work with following result:
# javac Test11.java
# java Test11
1 2 3
Answer D.
It will fail to compile because of bad syntax of declaring and initializing the array (in the second line of the code).
instead of int[] arr=(1,2,3,4);
it can be int [] arr={1,2,3,4};
then the answer should be option B
Answer is B
The answer should be D
Compilation fails because the array is not declared properly
it should be
int[] arr = new int[]{1, 2, 3, 4};
In case the array eas declared properly, this code should print 1 2 3 (answer B)