Prerequisite : Arrays in Java
Question 1. What is the output of this question?
class Test1 { public static void main(String[] args) { int arr[] = { 11 , 22 , 33 }; for ( int i = 0 ; i < arr.length; i++) System.out.print(arr[i] + " " ); System.out.println(); int arr2[] = new int [ 3 ]; arr2[] = { 11 , 22 , 33 }; for ( int i = 0 ; i < arr2.length; i++) System.out.print(arr2[i] + " " ); } } |
Option
A) 11 22 33
11 22 33
B) Error
C) Exception
D) None
Output: B
Explanation : It’s not a valid Syntax for array declarations. It will give compile time error : not a statement arr2[] = {11, 22, 33}
Question 2. What is the output of this question?
class Test2 { public static void main(String[] args) { String str[] = { "Geeks" , "for" , "Geeks" }; for ( int i = 0 ; i < str.length; i++) System.out.print(str[i]); } } A)GeeksforGeeks B)Error C)Geeks D)GfG |
Option
Output: A
Explanation : It is a simple one dimension string type array.
Question 3. What is the output of this question?
class Test2 { public static void main(String[] args) { String str[] = { "Geeks" , "for" , "Geeks" }; System.out.println(str.length); System.out.println(str[ 0 ].length); } } |
Option
A)Error
B)3
5
C)3
13
D)None
Output: A
Explanation : length is applied only to find the size of array. If we are try get the size of string object, then we will get compile time error : cannot find symbol.
Question 4. What is the output of this question?
class Test4 { public static void main(String[] args) { int number = 11 ; int NUMBER = 22 ; int Number = 33 ; System.out.print(number + " " ); System.out.print(NUMBER + " " ); System.out.println(Number); } } |
Option
A)11 22 33
B)11 11 11
C)33 33 33
D)error
Output:A
Explanation : Java is case sensitive. Therefore, here three different int type variable are there.
Question 5. What is the output of this question?
class Test5 { public static void main(String[] args) { String str[] = { "geeks" , "for" , "geeks" }; System.out.print(str[ 0 ] + str[ 1 ] + str[ 2 ]); } } |
Option
A)geeksforgeeks
B)gfg
C)Error
D)none
Output : A
Explanation : In Java + operator can concatenate the string.
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.