Output of Java Programs | Set 32 - GeeksforGeeks

11 Sep, 2017
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] + " ");    }}
OptionA) 1 2 3 4 51 2 3 4 5B) 1 2 3 4 51 2 3 4C) ErrorD)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;    }}
OptionA) Compile time ErrorB) Run time ExceptionC) 4 empty size Array are initializedD) 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);    }}
OptionA) 0 : 0B) 0 : -1C) Compiler ErrorD) 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);    }}
OptionA) 21474836472147483648B) ErrorC) 2147483647-1D) 21474836472147483646
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);    }}
OptionA)456510B) 45A10C)ErrorD)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.
Java-Arrays
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways to copy a string in C/C++
Runtime Errors
Output of Python Program | Set 1
C++ Programming Multiple Choice Questions
Output of C Programs | Set 2
unsigned specifier (%u) in C with Examples
Output of C++ programs | Set 50
Output of C++ Program | Set 1
Output of C Programs | Set 3
How to show full column content in a PySpark Dataframe ?