Java Arrays: 50 Multiple Choice Questions (MCQs)
1. What is the default value of an int array element in Java? a) 0 b) null c) undefined d) garbage value
(Answer: a)
2. Which of the following correctly declares an array in Java? a) int[] arr; b) int arr[]; c) Both a and b d) None
(Answer: c)
3. Output? int[] arr={1,2,3}; System.out.println(arr.length); a)1 b)2 c)3 d)Error (Answer: c)
4. Identify the error: int arr[5]={1,2,3,4,5}; a)No error b)Array size must be declared with new c)Syntax error
d)Cannot initialize like this (Answer: c)
5. Arrays.binarySearch(arr,key) requires: a)Unsorted array b)Sorted array c)Works both ways d)None
(Answer: b)
6. What is the output? int[] arr=new int[3]; System.out.println(arr[1]); a)1 b)0 c)null d)Error (Answer: b)
7. In Java, arrays are: a)Primitive b)Objects c)Pointers d)Methods (Answer: b)
8. Output? int[][] m={{1,2},{3,4}}; System.out.println(m[1][0]); a)1 b)3 c)4 d)Error (Answer: b)
9. Which package provides Arrays utility methods? a)Collections b)Arrays c)Math d)Object (Answer: b)
10. Array index starts from: a)-1 b)0 c)1 d)Depends on JVM (Answer: b)
11. Output? int[] nums={5,10,15}; System.out.println(nums[nums.length-1]); a)5 b)10 c)15 d)Error (Answer:
c)
12. Jagged array declaration int[][] arr=new int[3][]; is: a)Valid b)Invalid c)Compiler Error d)Runtime Error
(Answer: a)
13. Output? int[][] a={{1,2,3},{4,5}}; System.out.println(a[1].length); a)2 b)3 c)5 d)Error (Answer: a)
14. Arrays are: a)Resizable b)Fixed-size c)Dynamic d)Flexible (Answer: b)
15. Output? int[] arr={1,2}; arr=null; System.out.println(arr.length); a)2 b)0 c)NullPointerException d)Error
(Answer: c)
16. Output? int arr[]={1,2,3}; for(int i=0;i<arr.length;i++) System.out.print(arr[i]); a)123 b)321 c)Error d)0123
(Answer: a)
17. Identify error: int[] arr=new int[-5]; a)Valid b)NegativeArraySizeException c)Syntax Error d)0 length
(Answer: b)
18. Which copies an array? a)arr2=arr1 b)arr1.clone() c)Arrays.copyOf(arr1,arr1.length) d)Both b,c (Answer:
d)
19. Arrays.sort(arr) sorts: a)Ascending b)Descending c)Random d)None (Answer: a)
20. System.arraycopy copies: a)Deep copy b)Shallow copy c)Elements d)Clone (Answer: c)
21. Output? int[] arr=new int[5]; System.out.println(arr[4]); a)4 b)5 c)0 d)Error (Answer: c)
22. 2D arrays in Java are: a)Matrix b)Array of arrays c)Pointers d)Lists (Answer: b)
23. Correct initialization: a)int[] a={1,2,3}; b)int a[]={1,2,3}; c)Both d)None (Answer: c)
24. Output? int[][] mat=new int[2][2]; System.out.println(mat.length); a)4 b)2 c)Error d)0 (Answer: b)
25. Arrays.equals(arr1,arr2) checks: a)Ref equality b)Value equality c)Memory address d)Size only
(Answer: b)
26. Output? int arr[]=new int[3]; arr[0]=10; System.out.println(arr[2]); a)10 b)0 c)Error d)null (Answer: b)
27. int arr[]={}; System.out.println(arr.length); a)0 b)1 c)null d)Error (Answer: a)
28. Arrays in Java are: a)Always heap objects b)Stack objects c)Method locals d)JVM Dependent (Answer:
a)
29. Which is true? a)Array size fixed at runtime b)Array size fixed at compile-time c)Dynamic resizing
possible d)None (Answer: a)
30. What does arr.length give? a)Last index b)Total elements c)Size in bytes d)None (Answer: b)
31. Find output: int[] a={2,4,6}; int sum=0; for(int i:a) sum+=i; System.out.print(sum); a)12 b)246 c)Error d)0
(Answer: a)
32. Identify error: int[] arr; System.out.println(arr[0]); a)0 b)null c)Error: arr not initialized d)Garbage (Answer:
c)
33. Arrays.binarySearch(new int[]{1,2,3},3); returns: a)3 b)2 c)Index of 3 d)-1 (Answer: c)
34. Which throws ArrayStoreException? a)Assigning wrong type b)Assigning same type c)OutOfBounds
d)Null array (Answer: a)
35. Output? int[][] x=new int[2][]; x[0]=new int[2]; System.out.println(x[0].length); a)2 b)0 c)Error d)null
(Answer: a)
36. Arrays.deepToString prints: a)1D only b)Nested arrays c)Error d)None (Answer: b)
37. int arr[]=new int[]{1,2,3}; System.out.println(arr[1]); a)1 b)2 c)3 d)Error (Answer: b)
38. Jagged array row sizes: a)Equal b)Can differ c)Fixed d)Always 0 (Answer: b)
39. Arrays.fill(arr,5); does: a)Fills array with 5 b)Sets size to 5 c)Error d)None (Answer: a)
40. Which is correct? a)Arrays.sort for objects needs Comparator b)Always ascending c)Only ints allowed
d)None (Answer: a)
41. Identify error: int[][] arr=new int[][3]; a)Row size missing b)Column size missing c)No error d)Runtime
error (Answer: a)
42. ArrayIndexOutOfBoundsException occurs: a)Compile-time b)Runtime c)Never d)JVM startup (Answer:
b)
43. Which allows array printing nicely? a)Arrays.toString b)System.printArray c)arr.toString d)None
(Answer: a)
44. Output? int arr[]={5}; arr[0]=10; System.out.print(arr[0]); a)5 b)10 c)0 d)Error (Answer: b)
45. Arrays.copyOf(arr,5) creates: a)New array b)Same reference c)No copy d)Error (Answer: a)
46. Output? int[] a=new int[2]; a[0]=1; a[1]=2; System.out.print(a[0]+a[1]); a)3 b)12 c)Error d)0 (Answer: a)
47. What is true about clone()? a)Shallow for primitives b)Deep always c)Both d)None (Answer: a)
48. Identify output: int[] a=null; System.out.println(a); a)null b)Error c)NullPointerException d)Address
(Answer: a)
49. Arrays.stream(arr) is used for: a)Convert array to stream b)Clone c)Copy d)Sort (Answer: a)
50. Output? int[] arr=new int[]{1}; System.out.println(arr.length); a)0 b)1 c)Error d)null (Answer: b)