Output of Java Programs | Set 32
Prerequisite : Arrays in Java
Question 1. What is the output of following program?
class ArrayDemo { public static void main(String[] args) { int arr1[] = { 1 , 2 , 3 , 4 , 5 }; int arr2[ 5 ] = { 1 , 2 , 3 , 4 , 5 }; for ( int i = 0 ; i < 5 ; i++) System.out.print(arr1[i] + " " ); System.out.println(); for ( int i = 0 ; i < 5 ; i++) System.out.print(arr2[i] + " " ); } } |
Option
A) 1 2 3 4 5
1 2 3 4 5
B) 1 2 3 4 5
1 2 3 4
C) Error
D)Exception
Output: C
Explanation : In Java at the time of declaration, we can not specify the size otherwise we will get compile time Error : 1-‘]’ expected, 2- illegal start of expression
Question 2. What is the output of following program?
class ArrayDemo1 { public static void main(String[] args) { int arr1[], arr2[]; int [] arr3, [] arr4; } } |
Option
A) Compile time Error
B) Run time Exception
C) 4 empty size Array are initialized
D) none
Output: A
Explanation : In java, if we want to specify dimension before the variable that facility is applicable only for first variable in a declaration. otherwise we will get compile time error –
1- expected. 2- ';' expected.
Question 3. What is the output of following program?
class ArrayDemo1 { public static void main(String[] args) { int arr1[] = new int [ 0 ]; int arr2[] = new int [- 1 ]; System.out.print(arr1.length + " : " + arr2.length); } } |
Option
A) 0 : 0
B) 0 : -1
C) Compiler Error
D) Run time Exception
Output: D
Explanation : In java, if we are trying to specify Array size with some negative int value then we will get run time exception – NegativeArraySizeException.
Question 4. What is the output of following program?
class ArrayDemo1 { public static void main(String[] args) { int arr1[] = new int [ 2147483647 ]; int arr2[] = new int [ 2147483648 ]; System.out.println(arr1.length); System.out.println(arr2.length); } } |
Option
A) 2147483647
2147483648
B) Error
C) 2147483647
-1
D) 2147483647
2147483646
Output: B
Explanation :In java, maximum allowed array size is 2147483647 which is the maximum value of int.if you will give more than this range then we will get compile time error – integer number too large.
Question 5. What is the output of following program?
class ArrayDemo1 { public static void main(String[] args) { short s = 45 ; int arr1[] = new int [s]; char ch = 'A' ; int arr2[] = new int [ch]; long l = 10 ; int arr3[] = new int [l]; System.out.println(arr1.length); System.out.println(arr2.length); System.out.println(arr3.length); } } |
Option
A)45
65
10
B) 45
A
10
C)Error
D)no output
Output: C
Explanation : In java, we can specify the array size with char, sort, int, byte but we can not with long, double, string and float size. Otherwise we will get compile time error – incompatible types: possible lossy conversion.
This article is contributed by Shivakant Jaiswal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...