QuizzesJava Arrays
beginner
java

Java Arrays

Learn Java arrays — declaration, initialization, traversal, sorting, and multi-dimensional arrays.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Arrays. Work through them here, then start the quiz to answer them and see your score.

  1. 1.How do you declare an integer array of size 5 in Java?

    • Aint array[5]
    • Bint array = new int[5]
    • Cint[] array = new int[5]
    • Darray int[5]
  2. 2.What is the index of the first element in a Java array?

    • ADepends on the array size
    • B1
    • C0
    • D-1
  3. 3.What is the output?

    int[] nums = {10, 20, 30, 40, 50};
    System.out.println(nums[2]);
    • A30
    • B20
    • C10
    • DCompilation error
  4. 4.What happens if you access an array with an index that is out of range?

    • AWraps around to the beginning
    • BThrows ArrayIndexOutOfBoundsException
    • CReturns 0
    • DReturns null
  5. 5.What is the output?

    int[] arr = new int[3];
    System.out.println(arr[0]);
    • Anull
    • BCompilation error
    • CUndefined
    • D0
  6. 6.How do you find the length of an array?

    • Aarr.length
    • Barr.length()
    • Carr.count
    • Darr.size()
  7. 7.What does this code do?

    int[] arr = {5, 3, 1, 4, 2};
    java.util.Arrays.sort(arr);
    System.out.println(arr[0]);
    • A2
    • B3
    • C1
    • D5
  8. 8.How do you correctly copy an array?

    • Aint[] copy = original
    • Bint[] copy = Arrays.copyOf(original, original.length)
    • Cint[] copy = original.clone() only
    • DBoth B and C work
  9. 9.What is a 2D array in Java?

    • AAn array that can hold two data types
    • BAn array with two elements
    • CA sorted array
    • DAn array of arrays
  10. 10.What is the output?

    int[][] grid = {{1,2},{3,4},{5,6}};
    System.out.println(grid[1][1]);
    • A2
    • B3
    • C1
    • D4
  11. 11.What is the enhanced for loop (for-each) used for with arrays?

    • AFor searching arrays
    • BFor iterating over every element without needing an index
    • COnly for modifying array elements
    • DFor sorting arrays
  12. 12.What is the output?

    int[] nums = {1, 2, 3, 4, 5};
    int sum = 0;
    for (int n : nums) {
        sum += n;
    }
    System.out.println(sum);
    • A5
    • B12
    • C10
    • D15
  13. 13.What does Arrays.toString() do?

    • AConverts an array to a String for easy printing
    • BReturns the first String in the array
    • CConverts a String to an array
    • DSorts an array as Strings
  14. 14.Can an array store elements of different types?

    • AYes, always
    • BNo, all elements must be the same type
    • COnly in 2D arrays
    • DYes, if you use Object[]
  15. 15.What is the output?

    String[] fruits = {"apple", "banana", "cherry"};
    System.out.println(fruits.length);
    • A3
    • B4
    • C5
    • D0
  16. 16.What happens when you declare int[] arr; without initializing it?

    • ACompilation error
    • Barr is null
    • Carr contains garbage values
    • Darr is automatically set to an empty array
  17. 17.How do you search for a value in a sorted array efficiently?

    • AArrays.search()
    • BArrays.binarySearch()
    • CArrays.indexOf()
    • DArrays.find()
  18. 18.What is the output?

    int[] a = {1, 2, 3};
    int[] b = a;
    b[0] = 99;
    System.out.println(a[0]);
    • A0
    • B99
    • CCompilation error
    • D1
  19. 19.What does Arrays.fill() do?

    • AFills the array to its maximum capacity
    • BFills the array with sorted values
    • CFills all elements of an array with a specified value
    • DFills an array with random values
  20. 20.What is the output?

    int[] arr = {3, 1, 4, 1, 5};
    java.util.Arrays.sort(arr);
    System.out.println(java.util.Arrays.toString(arr));
    • A[5, 4, 3, 1, 1]
    • B[1, 1, 3, 4, 5]
    • C[1, 3, 4, 1, 5]
    • D[3, 1, 4, 1, 5]